SlideShare une entreprise Scribd logo
1  sur  41
Unit- 4
Structures and Classes
Structures
• One of the unique facilities provided by C
language is structure
• It is used to group logically related data items
• It is user defined data-type and once structure
type is defined, variables of that type can be
created
2
Limitations of Structures
• They don’t allow data hiding
• Structure members can be accessed using
structure variable by any function any where
in the scope
• C does not allow the structure data type to be
treated like built-in data type
3
Introduction to Class
• C++ supports all the features of structures as
defined in C.
• But C++ has expanded its capabilities further to
suit its OOP philosophy and provide data hiding
facility
• In C++, a structure can have both variables and
functions as members.
• Also, some of the members can be declared as
private so that they cannot be accessed directly
by external functions.
4
Contd…
• The keyword struct can be omitted in the
declaration of structure variable
• Eg: student ram;
• C++ incorporates all these extensions in
another user-defined type known as class.
• There is very little syntactical difference
between structures and classes in C++
• They can be used interchangeably with minor
modifications
5
Contd…
• Most of the C++ programmers tend to use
structure for holding only data, and classes to
hold both the data and functions
• The only difference between a structure and a
class in C++ is that, by default, the members
of a class are private, while by default the
members of a structure are public
6
Class
• Class is a collection of logically related data
items and the associated functions which
operate and manipulate those data
• The entire collection can be called a new
datatype
• So, classes are user defined data types and
behave like a built in types of a programming
language
7
Contd…
• Classes allow the data and functions to be
hidden, if necessary, from external use.
• Being a user defined data type, any number of
variables for that class can be declared
• The variables are called objects
8
Specifying a class
• Specification of a class consists of two parts:
– Class declaration
– Function definition
Syntax:
class class_name
{
private:
variable declarations
function declarations
public:
variable declarations
function declarations
}; 9
Contd…
• The class specification starts with a keyword
“class”, followed by a class-name.
• The class-name is an identifier
• The body of class is enclosed within braces
and terminated by a semicolon
• The functions and variables are collectively
called class members.
10
Contd…
• The variables are called data members while
the functions are called member functions
• The two keywords private and public are
termed as access-specifiers or visibility labels.
• They are followed by colons
• The class members that have been declared as
private can be accessed only from within the
class i.e. the member functions
11
Contd…
• Public members can be accessed from
anywhere outside the class (using object and
dot operator)
• All the class members are private by
default, so, keyword “private” is optional
• If both the labels are missing then, by
default, all the members will be private and
the class will be completely inaccessible by
the outsiders ( hence it wont be useful at all).
12
Contd…
• The key feature of OOP is data hiding.
• Generally, data within a class is made private
and the functions are public.
• So, the data will be safe from accidental
manipulations, while the functions can be
accessed from outside the class
• However, it is not necessary that the data
must be private and functions public
13
Example of class
class test
{
int x, y;
public:
void get_data()
{
cin>>x>>y;
}
void put_data()
{
cout<<x<<y;
}
}; 14
Creating objects
• Once a class has been declared, variables of
that type can be created by using the class
name as data-type
Test t1; //memory for t1 is allocated
• This statement creates a variable t1 of type
test.
• The class variables are known as objects
• t1 is called object of class test.
• More than one object of a class can be created
15
Contd…
• Objects are also called instances of class
• Objects can be created as follows:
class employee
{
int id;
char name[20];
public:
void getname();
void putname();
}e1, e2, e3;
16
Accessing class members
• The class members are accessed using dot operator
• However it works only for the public members
• The dot operator is called member access operator
• The general format is:
class-object.class-member;
• Eg: e1.getdata();
• The private data and functions of a class can be
accessed only through the member functions of that
class
17
Eg:
class A
{
int x, y;
void fu1();
public:
int z;
void fu2();
};
---------------------------
---------------------------
A obj1;
obj1.x=0; //generates error since x is private and can be
accessed only though member functions
obj1.z=0; //valid
obj1.fu1(); //generates error since fu1() is private
obj1.fu2(); //valid 18
Defining Member Functions
• Member function can be defined in two ways
• Outside the class
• Inside the class
• The code for the function body would be
identical in both the cases i.e perform same
task irrespective of the place of definition
19
Outside the Class
• In this approach, the member functions are
only declared inside the class, whereas its
definition is written outside the class
• General form:
Return-type class-name::function-name(argument-list)
{
--------------
-------------- // function body
--------------
}
20
Contd…
• The function is, generally defined immediately
after the class-specifier
• The function name is proceeded by:
– Return-type of the function
– Class-name to which the function belongs
– Symbol with double colons(::) ie. Scope resolution
operator
– This operator tells the compiler that the function
belongs to the class class-name
21
Contd…
• Eg:
class A
{
int x, y;
public:
void getdata (); // function declaration inside the class
}
void A :: getdata()
{
cin>>x>>y; // function body
}
22
Inside the class
• Function body can be included in the class
itself by replacing function declaration by
function definition
• If it is done, the function is treated as an inline
function
• Hence, all the restrictions that apply to inline
function, will also apply here
23
• Eg:
class A
{
int a, b;
public:
void getdata()
{
cin>>a>>b;
}
};
• Here, getdata() is defined inside the class. So, it
will act like an inline function
• A function defined outside the class can also be
made ‘inline’ by using the qualifier ‘inline’ in the
header line of a function definition
24
Contd…
• The member functions have some special
characteristics:
• A program may have several different classes
which may use same function name
• The scope resolution operator will resolve
which function belongs to which class
25
Contd…
• Member functions can directly access private
data of that class while non member function
cannot do so unless it is a friend function
• A member function can call another member
function directly, without using the dot
operator
26
Nested Member Functions
• An object of the class using dot
operator, generally, calls a member function of
a class
• However, a member function can be called by
using its name inside another member
function of the same class.
• This is known as “Nesting Of Member
Functions”
27
• Eg:
#include<iostream.h>
class addition
{
int a, b, sum;
public:
void read();
void show();
int add();
};
void addition::read()
{
cout<<“enter a, and b”;
cin>>a>>b;
}
void addition::add()
{
return(a+b);
}
void addition ::show()
{
sum=add(); // nesting of function
cout<<endl<<“sum of a and
b is :” <<sum;
}
main()
{
addition a1;
a1.read();
a1.show();
return(0);
}
Output:
Enter a and b: 2 3
Sum of a and b is: 5
28
Private Member Functions
• Member functions are in general made public
• But in some cases, we may need to make a
function a private to hide them from outside
world
• Private member functions can only be called
by another function that is a member of its
class
• Objects of the class cannot invoke it using dot
operator
29
• Eg:
class A
{
int a, b;
void read(); //private member function
public:
void update();
void write();
};
void A :: update()
{
read(); // called from update() function.
no object used
}
• If a1 is an object of A, then the following statement is not valid
a1.read();
• This is because, read is a private member function which cannot be
called using object and dot operator 30
Memory allocation for objects
• Memory space for object is allocated when they
are declared and not when the class is specified
• The member functions are created and placed in
the memory only once, when they are defined as a
part of a class specification
• All the objects belonging to the particular class will
use same member functions when objects are
created
• However, the data members will hold different
values for different object, so, space for data
member is allocated separately for each object
31
Array of objects
• Array can be created of any datatype
• Since a class is also a user defined data-
type, array of objects can be created
• Eg: a class Employee is specified. If we have to
keep records of 20 employees in an organization
having two departments, then instead of creating
20 separate variables, we can create array of
objects as follows:
Employee dept1[10];
Employee dept2[10];
32
Objects As Function Arguments
• Like any other variable, objects can also be
passed to the function, as an argument
• There are two ways of doing this
– Pass by value
– Pass by reference
• In the pass by value, the copy of the object is
passed to the function
• So, the changes made to the object inside the
function do not affect the actual object
33
Contd…..
• On the other hand, address of the object is
passed in the case of pass by reference
• So the changes made to the object inside the
function are reflected in the actual object
• This method is considered more efficient
34
• Eg:
class height
{
int feet, inches;
public:
void get_height()
{
cout<<“Enter height in feet and
inches”;
cin>>feet>>inches;
}
void put_height()
{
cout<<“Feet:”<<feet;
cout<<and inches:”<<inches;
}
void sum(height, height);
};
void height:: sum(height h1, height h2)
{
inches= h1.inches+h2.inches;
feet=inches/12;
inches=inches%12;
feet=feet+h1.feet+h2.feet;
}
int main()
{
height h1, h2, h3;
h1.get_height();
h2.get_height();
h3.sum(h1, h2);
cout<<“Height 1: ” ; h1.put_height();
cout<<“Height 2: ” ; h2.put_height();
cout<<“Height 3: ” ; h3.put_height();
return(0);
}
35
Static Data Members
• Each object of a class maintain their own copy
of data member
• However, in some cases, it may be necessary
that all objects of a class have access to the
same copy of a single variable.
• This can be made possible by using static
variable
• A static variable is declared using the “ static”
keyword
36
Contd…
• Only one copy of static member is created for
the entire class and is shared by all the objects
of that class, no matter how many objects are
created
• It is visible only within the class, but its
lifetime is the entire program
• It is initialized to zero when the first object of
its class is created
• It is also known as class variable
37
• Eg:
class A
{
static int count;
int variable;
public:
A()
{
count++;
}
void get_var()
{
cin>>variable;
}
void put_var()
{
cout<<variable;
}
void put_count()
{
cout<<count;
}
};
int A:: count; // the type and scope
of each static member variable
is defined outside class
definition
main()
{
A a, b, c;
a.put_count();
b.put_count();
c.put_count();
return(0);
}
Output:
1 2 3
38
Static Member Function
• In a class, functions can also be declared as
static
• Properties of static functions are:
– They can access only other STATIC
members(functions or variables) declared in the
same class
– They can be called using class name ( instead of its
object)
– Eg: class_name::function_name
39
class A
{
int no;
static int count; //static member
public:
void set_no()
{
count++;
no=count;
}
void put_no()
{
cout<<“ No is:” <<no;
}
static void put_count() //static member
function accessing static member
{
cout<<endl<<“Count:” <<count;
}
};
Int A::count;
main()
{
A a1, a2;
a1.set_no();
a2.set_no();
A::put_count();
a1.set_no();
a2.set_no();
A::put_count();
a1.put_no();
a2.put_no();
return(0);
} 40
THE END
41

Contenu connexe

Tendances

CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCECLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCEVenugopalavarma Raja
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++rprajat007
 
Classes cpp intro thomson bayan college
Classes cpp  intro thomson bayan collegeClasses cpp  intro thomson bayan college
Classes cpp intro thomson bayan collegeahmed hmed
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++Muhammad Waqas
 
Java class,object,method introduction
Java class,object,method introductionJava class,object,method introduction
Java class,object,method introductionSohanur63
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++HalaiHansaika
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functionsMarlom46
 
How to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingHow to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingSyed Faizan Hassan
 
ITFT-Classes and object in java
ITFT-Classes and object in javaITFT-Classes and object in java
ITFT-Classes and object in javaAtul Sehdev
 
Object oriented programming using c++
Object oriented programming using c++Object oriented programming using c++
Object oriented programming using c++Hoang Nguyen
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteTushar B Kute
 
Class and object in c++
Class and object in c++Class and object in c++
Class and object in c++NainaKhan28
 
Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)Majid Saeed
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functionsHarsh Patel
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphismmcollison
 

Tendances (20)

C++ And Object in lecture3
C++  And Object in lecture3C++  And Object in lecture3
C++ And Object in lecture3
 
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCECLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Classes cpp intro thomson bayan college
Classes cpp  intro thomson bayan collegeClasses cpp  intro thomson bayan college
Classes cpp intro thomson bayan college
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
Java class,object,method introduction
Java class,object,method introductionJava class,object,method introduction
Java class,object,method introduction
 
Class or Object
Class or ObjectClass or Object
Class or Object
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Class and object in C++ By Pawan Thakur
Class and object in C++ By Pawan ThakurClass and object in C++ By Pawan Thakur
Class and object in C++ By Pawan Thakur
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
 
Object and class
Object and classObject and class
Object and class
 
How to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingHow to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programming
 
ITFT-Classes and object in java
ITFT-Classes and object in javaITFT-Classes and object in java
ITFT-Classes and object in java
 
Object oriented programming using c++
Object oriented programming using c++Object oriented programming using c++
Object oriented programming using c++
 
Classes and objects in java
Classes and objects in javaClasses and objects in java
Classes and objects in java
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
 
Class and object in c++
Class and object in c++Class and object in c++
Class and object in c++
 
Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
 

En vedette

class and objects
class and objectsclass and objects
class and objectsPayel Guria
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).pptAlok Kumar
 
Object and Classes in Java
Object and Classes in JavaObject and Classes in Java
Object and Classes in Javabackdoor
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVAAbhilash Nair
 
When to use a structure vs classes in c++
When to use a structure vs classes in c++When to use a structure vs classes in c++
When to use a structure vs classes in c++Naman Kumar
 
Classes and objects
Classes and objectsClasses and objects
Classes and objectsAnil Kumar
 
Java: Objects and Object References
Java: Objects and Object ReferencesJava: Objects and Object References
Java: Objects and Object ReferencesTareq Hasan
 
Basic structure of C++ program
Basic structure of C++ programBasic structure of C++ program
Basic structure of C++ programmatiur rahman
 
Programming In C++
Programming In C++ Programming In C++
Programming In C++ shammi mehra
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member FunctionsMuhammad Hammad Waseem
 
Classes And Objects
Classes And ObjectsClasses And Objects
Classes And Objectsrahulsahay19
 
c++ programming Unit 2 basic structure of a c++ program
c++ programming Unit 2 basic structure of a c++ programc++ programming Unit 2 basic structure of a c++ program
c++ programming Unit 2 basic structure of a c++ programAAKASH KUMAR
 
1. introduction to semantics
1. introduction to semantics1. introduction to semantics
1. introduction to semanticsAsmaa Alzelibany
 
Introduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingIntroduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingMoutaz Haddara
 
Basic concepts of object oriented programming
Basic concepts of object oriented programmingBasic concepts of object oriented programming
Basic concepts of object oriented programmingSachin Sharma
 

En vedette (20)

class and objects
class and objectsclass and objects
class and objects
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
 
Object and Classes in Java
Object and Classes in JavaObject and Classes in Java
Object and Classes in Java
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
When to use a structure vs classes in c++
When to use a structure vs classes in c++When to use a structure vs classes in c++
When to use a structure vs classes in c++
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
14. Linked List
14. Linked List14. Linked List
14. Linked List
 
Java: Objects and Object References
Java: Objects and Object ReferencesJava: Objects and Object References
Java: Objects and Object References
 
Basic structure of C++ program
Basic structure of C++ programBasic structure of C++ program
Basic structure of C++ program
 
C++ Programming
C++ ProgrammingC++ Programming
C++ Programming
 
Programming In C++
Programming In C++ Programming In C++
Programming In C++
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
 
Classes And Objects
Classes And ObjectsClasses And Objects
Classes And Objects
 
c++ programming Unit 2 basic structure of a c++ program
c++ programming Unit 2 basic structure of a c++ programc++ programming Unit 2 basic structure of a c++ program
c++ programming Unit 2 basic structure of a c++ program
 
1. introduction to semantics
1. introduction to semantics1. introduction to semantics
1. introduction to semantics
 
OOP java
OOP javaOOP java
OOP java
 
Introduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingIntroduction to Object Oriented Programming
Introduction to Object Oriented Programming
 
Basic concepts of object oriented programming
Basic concepts of object oriented programmingBasic concepts of object oriented programming
Basic concepts of object oriented programming
 
SEMANTICS
SEMANTICS SEMANTICS
SEMANTICS
 

Similaire à Classes and objects

Object and class presentation
Object and class presentationObject and class presentation
Object and class presentationnafisa rahman
 
APL-2-classes and objects.ppt
APL-2-classes and objects.pptAPL-2-classes and objects.ppt
APL-2-classes and objects.pptsrividyal2
 
Object Oriented Programming Constructors & Destructors
Object Oriented Programming  Constructors &  DestructorsObject Oriented Programming  Constructors &  Destructors
Object Oriented Programming Constructors & Destructorsanitashinde33
 
C++ppt. Classs and object, class and object
C++ppt. Classs and object, class and objectC++ppt. Classs and object, class and object
C++ppt. Classs and object, class and objectsecondakay
 
Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.Enam Khan
 
c++ lecture 1
c++ lecture 1c++ lecture 1
c++ lecture 1sai kumar
 
c++ lecture 1
c++ lecture 1c++ lecture 1
c++ lecture 1sai kumar
 
c++ introduction
c++ introductionc++ introduction
c++ introductionsai kumar
 
C++ training
C++ training C++ training
C++ training PL Sharma
 
Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Getachew Ganfur
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming CourseDennis Chang
 
Concept of Object-Oriented in C++
Concept of Object-Oriented in C++Concept of Object-Oriented in C++
Concept of Object-Oriented in C++Abdullah Jan
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methodsfarhan amjad
 

Similaire à Classes and objects (20)

Class and objects
Class and objectsClass and objects
Class and objects
 
Object and class presentation
Object and class presentationObject and class presentation
Object and class presentation
 
APL-2-classes and objects.ppt
APL-2-classes and objects.pptAPL-2-classes and objects.ppt
APL-2-classes and objects.ppt
 
Object Oriented Programming Constructors & Destructors
Object Oriented Programming  Constructors &  DestructorsObject Oriented Programming  Constructors &  Destructors
Object Oriented Programming Constructors & Destructors
 
C++ppt. Classs and object, class and object
C++ppt. Classs and object, class and objectC++ppt. Classs and object, class and object
C++ppt. Classs and object, class and object
 
Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.
 
Class and object
Class and objectClass and object
Class and object
 
Lecture 2 (1)
Lecture 2 (1)Lecture 2 (1)
Lecture 2 (1)
 
c++ lecture 1
c++ lecture 1c++ lecture 1
c++ lecture 1
 
c++ lecture 1
c++ lecture 1c++ lecture 1
c++ lecture 1
 
c++ introduction
c++ introductionc++ introduction
c++ introduction
 
Class and object
Class and objectClass and object
Class and object
 
C++ training
C++ training C++ training
C++ training
 
Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming Course
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Concept of Object-Oriented in C++
Concept of Object-Oriented in C++Concept of Object-Oriented in C++
Concept of Object-Oriented in C++
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
 
CS1Lesson13-IntroClasses.pptx
CS1Lesson13-IntroClasses.pptxCS1Lesson13-IntroClasses.pptx
CS1Lesson13-IntroClasses.pptx
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 

Dernier

#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 

Dernier (20)

#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 

Classes and objects

  • 2. Structures • One of the unique facilities provided by C language is structure • It is used to group logically related data items • It is user defined data-type and once structure type is defined, variables of that type can be created 2
  • 3. Limitations of Structures • They don’t allow data hiding • Structure members can be accessed using structure variable by any function any where in the scope • C does not allow the structure data type to be treated like built-in data type 3
  • 4. Introduction to Class • C++ supports all the features of structures as defined in C. • But C++ has expanded its capabilities further to suit its OOP philosophy and provide data hiding facility • In C++, a structure can have both variables and functions as members. • Also, some of the members can be declared as private so that they cannot be accessed directly by external functions. 4
  • 5. Contd… • The keyword struct can be omitted in the declaration of structure variable • Eg: student ram; • C++ incorporates all these extensions in another user-defined type known as class. • There is very little syntactical difference between structures and classes in C++ • They can be used interchangeably with minor modifications 5
  • 6. Contd… • Most of the C++ programmers tend to use structure for holding only data, and classes to hold both the data and functions • The only difference between a structure and a class in C++ is that, by default, the members of a class are private, while by default the members of a structure are public 6
  • 7. Class • Class is a collection of logically related data items and the associated functions which operate and manipulate those data • The entire collection can be called a new datatype • So, classes are user defined data types and behave like a built in types of a programming language 7
  • 8. Contd… • Classes allow the data and functions to be hidden, if necessary, from external use. • Being a user defined data type, any number of variables for that class can be declared • The variables are called objects 8
  • 9. Specifying a class • Specification of a class consists of two parts: – Class declaration – Function definition Syntax: class class_name { private: variable declarations function declarations public: variable declarations function declarations }; 9
  • 10. Contd… • The class specification starts with a keyword “class”, followed by a class-name. • The class-name is an identifier • The body of class is enclosed within braces and terminated by a semicolon • The functions and variables are collectively called class members. 10
  • 11. Contd… • The variables are called data members while the functions are called member functions • The two keywords private and public are termed as access-specifiers or visibility labels. • They are followed by colons • The class members that have been declared as private can be accessed only from within the class i.e. the member functions 11
  • 12. Contd… • Public members can be accessed from anywhere outside the class (using object and dot operator) • All the class members are private by default, so, keyword “private” is optional • If both the labels are missing then, by default, all the members will be private and the class will be completely inaccessible by the outsiders ( hence it wont be useful at all). 12
  • 13. Contd… • The key feature of OOP is data hiding. • Generally, data within a class is made private and the functions are public. • So, the data will be safe from accidental manipulations, while the functions can be accessed from outside the class • However, it is not necessary that the data must be private and functions public 13
  • 14. Example of class class test { int x, y; public: void get_data() { cin>>x>>y; } void put_data() { cout<<x<<y; } }; 14
  • 15. Creating objects • Once a class has been declared, variables of that type can be created by using the class name as data-type Test t1; //memory for t1 is allocated • This statement creates a variable t1 of type test. • The class variables are known as objects • t1 is called object of class test. • More than one object of a class can be created 15
  • 16. Contd… • Objects are also called instances of class • Objects can be created as follows: class employee { int id; char name[20]; public: void getname(); void putname(); }e1, e2, e3; 16
  • 17. Accessing class members • The class members are accessed using dot operator • However it works only for the public members • The dot operator is called member access operator • The general format is: class-object.class-member; • Eg: e1.getdata(); • The private data and functions of a class can be accessed only through the member functions of that class 17
  • 18. Eg: class A { int x, y; void fu1(); public: int z; void fu2(); }; --------------------------- --------------------------- A obj1; obj1.x=0; //generates error since x is private and can be accessed only though member functions obj1.z=0; //valid obj1.fu1(); //generates error since fu1() is private obj1.fu2(); //valid 18
  • 19. Defining Member Functions • Member function can be defined in two ways • Outside the class • Inside the class • The code for the function body would be identical in both the cases i.e perform same task irrespective of the place of definition 19
  • 20. Outside the Class • In this approach, the member functions are only declared inside the class, whereas its definition is written outside the class • General form: Return-type class-name::function-name(argument-list) { -------------- -------------- // function body -------------- } 20
  • 21. Contd… • The function is, generally defined immediately after the class-specifier • The function name is proceeded by: – Return-type of the function – Class-name to which the function belongs – Symbol with double colons(::) ie. Scope resolution operator – This operator tells the compiler that the function belongs to the class class-name 21
  • 22. Contd… • Eg: class A { int x, y; public: void getdata (); // function declaration inside the class } void A :: getdata() { cin>>x>>y; // function body } 22
  • 23. Inside the class • Function body can be included in the class itself by replacing function declaration by function definition • If it is done, the function is treated as an inline function • Hence, all the restrictions that apply to inline function, will also apply here 23
  • 24. • Eg: class A { int a, b; public: void getdata() { cin>>a>>b; } }; • Here, getdata() is defined inside the class. So, it will act like an inline function • A function defined outside the class can also be made ‘inline’ by using the qualifier ‘inline’ in the header line of a function definition 24
  • 25. Contd… • The member functions have some special characteristics: • A program may have several different classes which may use same function name • The scope resolution operator will resolve which function belongs to which class 25
  • 26. Contd… • Member functions can directly access private data of that class while non member function cannot do so unless it is a friend function • A member function can call another member function directly, without using the dot operator 26
  • 27. Nested Member Functions • An object of the class using dot operator, generally, calls a member function of a class • However, a member function can be called by using its name inside another member function of the same class. • This is known as “Nesting Of Member Functions” 27
  • 28. • Eg: #include<iostream.h> class addition { int a, b, sum; public: void read(); void show(); int add(); }; void addition::read() { cout<<“enter a, and b”; cin>>a>>b; } void addition::add() { return(a+b); } void addition ::show() { sum=add(); // nesting of function cout<<endl<<“sum of a and b is :” <<sum; } main() { addition a1; a1.read(); a1.show(); return(0); } Output: Enter a and b: 2 3 Sum of a and b is: 5 28
  • 29. Private Member Functions • Member functions are in general made public • But in some cases, we may need to make a function a private to hide them from outside world • Private member functions can only be called by another function that is a member of its class • Objects of the class cannot invoke it using dot operator 29
  • 30. • Eg: class A { int a, b; void read(); //private member function public: void update(); void write(); }; void A :: update() { read(); // called from update() function. no object used } • If a1 is an object of A, then the following statement is not valid a1.read(); • This is because, read is a private member function which cannot be called using object and dot operator 30
  • 31. Memory allocation for objects • Memory space for object is allocated when they are declared and not when the class is specified • The member functions are created and placed in the memory only once, when they are defined as a part of a class specification • All the objects belonging to the particular class will use same member functions when objects are created • However, the data members will hold different values for different object, so, space for data member is allocated separately for each object 31
  • 32. Array of objects • Array can be created of any datatype • Since a class is also a user defined data- type, array of objects can be created • Eg: a class Employee is specified. If we have to keep records of 20 employees in an organization having two departments, then instead of creating 20 separate variables, we can create array of objects as follows: Employee dept1[10]; Employee dept2[10]; 32
  • 33. Objects As Function Arguments • Like any other variable, objects can also be passed to the function, as an argument • There are two ways of doing this – Pass by value – Pass by reference • In the pass by value, the copy of the object is passed to the function • So, the changes made to the object inside the function do not affect the actual object 33
  • 34. Contd….. • On the other hand, address of the object is passed in the case of pass by reference • So the changes made to the object inside the function are reflected in the actual object • This method is considered more efficient 34
  • 35. • Eg: class height { int feet, inches; public: void get_height() { cout<<“Enter height in feet and inches”; cin>>feet>>inches; } void put_height() { cout<<“Feet:”<<feet; cout<<and inches:”<<inches; } void sum(height, height); }; void height:: sum(height h1, height h2) { inches= h1.inches+h2.inches; feet=inches/12; inches=inches%12; feet=feet+h1.feet+h2.feet; } int main() { height h1, h2, h3; h1.get_height(); h2.get_height(); h3.sum(h1, h2); cout<<“Height 1: ” ; h1.put_height(); cout<<“Height 2: ” ; h2.put_height(); cout<<“Height 3: ” ; h3.put_height(); return(0); } 35
  • 36. Static Data Members • Each object of a class maintain their own copy of data member • However, in some cases, it may be necessary that all objects of a class have access to the same copy of a single variable. • This can be made possible by using static variable • A static variable is declared using the “ static” keyword 36
  • 37. Contd… • Only one copy of static member is created for the entire class and is shared by all the objects of that class, no matter how many objects are created • It is visible only within the class, but its lifetime is the entire program • It is initialized to zero when the first object of its class is created • It is also known as class variable 37
  • 38. • Eg: class A { static int count; int variable; public: A() { count++; } void get_var() { cin>>variable; } void put_var() { cout<<variable; } void put_count() { cout<<count; } }; int A:: count; // the type and scope of each static member variable is defined outside class definition main() { A a, b, c; a.put_count(); b.put_count(); c.put_count(); return(0); } Output: 1 2 3 38
  • 39. Static Member Function • In a class, functions can also be declared as static • Properties of static functions are: – They can access only other STATIC members(functions or variables) declared in the same class – They can be called using class name ( instead of its object) – Eg: class_name::function_name 39
  • 40. class A { int no; static int count; //static member public: void set_no() { count++; no=count; } void put_no() { cout<<“ No is:” <<no; } static void put_count() //static member function accessing static member { cout<<endl<<“Count:” <<count; } }; Int A::count; main() { A a1, a2; a1.set_no(); a2.set_no(); A::put_count(); a1.set_no(); a2.set_no(); A::put_count(); a1.put_no(); a2.put_no(); return(0); } 40