SlideShare a Scribd company logo
CLASSES AND OBJECTS
WHAT ARE OBJECTS?
DEFINITION:
An object is an entity, real or abstract that has some state
and behavior. For example: dog, apple, bicycle etc.
OBJECT STATE:
The state or attributes of an object are the characteristics
of an object and they describe the object.
OBJECT BEHAVIOR:
Object behavior is operations provided by or build into the
object.
OBJECT STATE BEHAVIOR
DOG COLOR,
BREED,
HUNGRY etc.
BARKING,
FETCHING,W
AGGINGTAIL
etc.
APPLE SHAPE,
COLOR etc.
SWEET IN
TASTE, JUICY
etc.
BICYCLE WHEELS,
GEARS, etc.
ACCELRATIN
G,
CHANGING
GEARS etc.
Objects are the fundamental building blocks of an object oriented system.
WHAT ARE CLASSES?
SYNTAX:
class user_defined_name
{
private:
data members
member functions
protected:
data members
member functions
public:
data members
member functions
};
C++ allows us to create software objects similar to real
world objects.
DEFINITION:
A class is a way to bind the data describing an entity and its
associated functions together. It is a set of objects that
share common attributes and behavior.
Where
class is a keyword
user_defined_name is the tag name or class specifier
private, protected, public are access specifiers
CLASS : EXAMPLE
class student
{
private:
int roll;
char name[20];
public:
void indata()
{ cin>> roll; gets(name); }
void outdata()
{ cout<< roll<<name;}
}
CLASS SPECIFIER /TAG NAME
KEYWORD :
CLASS
ACCESS SPECIFIER
DATA MEMBERS
MEMBER
FUNCTIONS
ACCESS SPECIFIER
An access specifier determines whether class members are accessible in
various parts of our program.
ACCESS SPECIFIER
PRIVATE
They are not accessible
outside the class. Only
member functions of the
class can use them.They can
not be inherited.
PROTECTED
They are not accessible
outside the class. Only
member functions of the
class can use them. But
they can be inherited.
PUBLIC
They are accessible
outside the class.They
can also be inherited.
NOTE: In the absence of a specifier all the members defined in a class are considered
to be private. Protected access specifier is discussed in detail in the inheritance PPT.
CLASS MEMBERS
Class members can be categorized as follows:
CLASS MEMBERS
DATA MEMBERS
They are variables which
describe the property of a
particular class. Data
members are generally kept
in the private section to keep
data safe from accidental
changes.
MEMBER FUNCTIONS
They are functions which operate on
data members.They are also called
messages. A message determines
what operations can be performed by
an object. Objects communicate with
each other through messages.They
can be defined in two ways:
Inside the classOutside the class
DEFINING MEMBER FUNCTIONS
INSIDE/OUTSIDETHE CLASS
INSIDETHE CLASS: Functions are defined inside the
class only if the function code is small. Such function
definitions are considered inline by default.
class student
{
private:
int roll;
char name[20];
public:
void indata()
{ cin>> roll; gets(name); }
void outdata()
{ cout<< roll<<name;}
}
OUTSIDETHE CLASS: In this case prototype of function
is declared inside the class.
SYNTAX:
return type class_name :: function_name(argument list)
class student
{ private:
int roll;
char name[20];
public:
void indata();
void outdata();
};
void student :: indata()
{ cin>> roll; gets(name); }
void student :: outdata()
{ cout<< roll<<name;}
CREATING OBJECTS
An object is an instance of a class. Objects represent variables of a user defined
data type class.
SYNTAX:
class _name object_name;
EXAMPLE:
The object of class STUDENT declared above is as follows:
student stu;
Here stu is the name of the object of class STUDENT.
NOTE: The class specification provides only a template and does not allocate any memory
space.The necessary memory space is allocated only when the object of the class is created.
CREATING OBJECTS Contd…..
 When we create many objects of the same class,
each object has its own copy of data members.
 All the objects of the same class use the same set of
member functions.
 The member functions are created and placed in
memory only once – when they are defined in the
class specification.
 This is feasible because functions for each object
are identical, however data items will hold different
values for each object.
Student
s1
Student
s1
Student
s3;
void indata()
void
outdata()
Member
functions
Data members
ACCESSING MEMBER FUNCTIONS
Member functions can be accessed using a dot operator between the object name and the
member function.
SYNTAX:
object_name . Member_function;
EXAMPLE:
stu . indata();
The above statement means that a function indata() is being called for the object stu.
Only public members (data and functions) can be accessed by the objects of the class.
This function call can be described as sending a message indata() to the object stu.
Any reference to class data in the indata() method will now be applied to the data
members of the stu object.
NESTING OF MEMBER FUNCTIONS
When a member function of a class is called by another member function of the same class,
it is called nesting of member functions.
QWAP to create a classTravelPlan
Data Members: PlanCode long
Place char[20]
Number_of_travelers int
Number_of_buses int
Member Functions:
GetPlan() :function to allow the user to input data and call function NewPlan().
NewPlan() : Function to assign the values of Number_of_Buses as per the following:
Number ofTravellers < 20, Number_of_Buses = 1.
Number ofTravellers > 20 and < 40, Number_of_Buses = 2.
Number ofTravellers > 40, Number_of_Buses = 3.
ShowPlan() : A function to display the contents of all the data members.
NESTING OF MEMBER FUNCTIONS Contd….
#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <stdio.h>
classTravelPlan
{
long PlanCode;
char Place[20];
int Number_of_travellers;
int Number_of_buses;
public :
TravelPlan();
void GetPlan();
void NewPlan();
void ShowPlan();
};
voidTravelPlan::ShowPlan()
{
cout<<"nnnn"<<PlanCode;
cout<<"nn"<<Place;
cout<<"nn"<<Number_of_travellers;
cout<<"nn"<<Number_of_buses;
}
void main()
{ clrscr();
TravelPlanTP;
TP.GetPlan();
TP.ShowPlan();
getch(); }
void GetPlan()
{ cin >> PlanCode;
gets(Place);
cin >> Number_of_travellers;
NewPlan(); // function being
//called by the function GetPlan()
}
voidTravelPlan::NewPlan()
{
if (Number_of_travellers < 20)
Number_of_buses = 1;
else if (Number_of_travellers < 40)
Number_of_buses = 2;
else
Number_of_buses = 3;
};
ARRAY OF OBJECTS
An array of objects is stored inside the memory in the same way as a multi-
dimensional array.We can use the usual array accessing methods to access individual
elements and then the dot operator to access the member functions.
#include <iostream.h>
#include <stdio.h>
classTravelPlan
{
long PlanCode;
char Place[20];
int Number_of_travellers;
int Number_of_buses;
public :
TravelPlan();
void GetPlan();
void NewPlan();
void ShowPlan();
};
void GetPlan()
{ cin >> PlanCode;
gets(Place);
cin >> Number_of_travellers;
NewPlan();
}
voidTravelPlan::NewPlan()
{
if (Number_of_travellers < 20)
Number_of_buses = 1;
else if (Number_of_travellers < 40)
Number_of_buses = 2;
else
Number_of_buses = 3;
};
voidTravelPlan::ShowPlan()
{
cout<<"nnnn"<<PlanCode;
cout<<"nn"<<Place;
cout<<"nn"<<Number_of_travellers;
cout<<"nn"<<Number_of_buses;
}
void main()
{ clrscr();
TravelPlanTP [ 10 ]; // Object array
for( int i=0; i<10; i++ )
TP [ i ].GetPlan();
for( int i=0; i<10; i++ )
TP [ i }.ShowPlan();
getch(); }
OBJECTSAS FUNCTIONARGUMENTS
An object may be passed to the function as an argument. It can be done in two ways:
CALL BYVALUE CALL BY REFERENCE
In this method, a copy of the arguments is
made and it is this copy that is used by the
function.
In this method, instead of a value being
passed to the function, a reference of the
object in the calling function is passed.
The changes made to the members of the
object inside the called function are reflected
back to the calling function.
The called function works directly on the
actual object used in the call and changes
made to the object in the called function will
reflect in the actual object.
When the function to which the copy of the
object is passed terminates the copy of the
argument is destroyed.
No copy is made.Works on actual objects.
To pass the object by reference the object
name is preceded by “&” in the function
definition.
OBJECTSAS FUNCTIONARGUMENTS Contd…..
#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <stdio.h>
class time
{
int hour;
int min;
public :
void GetTime ();
void AddTime(time t1, time t2);
void ShowTime();
};
void GetTime()
{ cin<< hour;
cin<< min;
}
void ShowTime()
{
cout<< “ Hours=“<< hour;
cout<<“Minutes=“<< min;
}
void AddTime(time t1 , time t2)
{ int h1;
hour = t1.hour + t2.hour;
min = t1.min + t2.min;
if (hour>=60)
h1= hour – 60;
min= min + h1;
}
}
void main()
{
time x1, x2, x3;
x1.GetTime();
x2.GetTime();
x3.AddTime(x1, x2);
x1.ShowTime();
x2.ShowTime();
x3.ShowTime();
}
RETURNING OBJECTS
When an object is returned by a function, a temporary object is
automatically created which holds the return value.
It is this object that is actually returned by the function. After
the value is returned, this object is destroyed.
RETURNINGOBJECTS Contd…..
#include <iostream.h>
#include <conio.h>
class time
{
int hour;
int min;
public :
void GetTime ();
time AddTime(time t1);
void ShowTime();
};
void GetTime()
{ cin<< hour;
cin<< min;
}
void ShowTime()
{
cout<< “ Hours=“<< hour;
cout<<“Minutes=“<< min;
}
time AddTime(time t1 )
{ time t3;
t3.hour = t1.hour +hour;
t3.min = t1.min + min;
if (t3.hour>=60)
h1= t3.hour – 60;
t3.min= t3.min + h1;
}
}
void main()
{
time x1, x2, x3;
x1.GetTime();
x2.GetTime();
x3= x2.AddTime(x1);
x1.ShowTime();
x2.ShowTime();
x3.ShowTime();
}
ASSIGNING OBJECTS
C++ allows assigning one object to another
object, provided they belong to the same
class.
By default, when one object is assigned to
another, a bitwise copy of all data members
is made.
class A
{ int a1;
public:
void getdata();
void putdata();
}
class B
{ int b1;
public:
void indata();
void outdata();
}
void main()
{
A a11, a12;
B b11, b12;
a11.getdata();
a12= a11; // correct
b11 = a11; // not allowed
}
Explanation:
As a11 and a12 are objects of the same class, the operation
a12=a11; is allowed.
As b11 and a11 belong to different classes, the operation
b11=a11; is not allowed.

More Related Content

What's hot (20)

Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
This pointer
This pointerThis pointer
This pointer
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++
 
Vectors in Java
Vectors in JavaVectors in Java
Vectors in Java
 
standard template library(STL) in C++
standard template library(STL) in C++standard template library(STL) in C++
standard template library(STL) in C++
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Structure in C language
Structure in C languageStructure in C language
Structure in C language
 
Tokens in C++
Tokens in C++Tokens in C++
Tokens in C++
 
Python Modules
Python ModulesPython Modules
Python Modules
 
Strings in C
Strings in CStrings in C
Strings in C
 
Python list
Python listPython list
Python list
 
Function in C
Function in CFunction in C
Function in C
 
Friend function & friend class
Friend function & friend classFriend function & friend class
Friend function & friend class
 

Viewers also liked

Advanced data structures using c++ 3
Advanced data structures using c++ 3Advanced data structures using c++ 3
Advanced data structures using c++ 3Shaili Choudhary
 
Structured query language functions
Structured query language functionsStructured query language functions
Structured query language functionsVineeta Garg
 
Structured query language constraints
Structured query language constraintsStructured query language constraints
Structured query language constraintsVineeta Garg
 
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
13. Indexing MTrees - Data Structures using C++ by Varsha Patil13. Indexing MTrees - Data Structures using C++ by Varsha Patil
13. Indexing MTrees - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. Patil
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. PatilDiscrete Mathematics S. Lipschutz, M. Lipson And V. H. Patil
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. Patilwidespreadpromotion
 
Working with Cookies in NodeJS
Working with Cookies in NodeJSWorking with Cookies in NodeJS
Working with Cookies in NodeJSJay Dihenkar
 
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
16. Algo analysis & Design - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
Data Structure
Data StructureData Structure
Data Structuresheraz1
 
Data file handling in c++
Data file handling in c++Data file handling in c++
Data file handling in c++Vineeta Garg
 
10. Search Tree - Data Structures using C++ by Varsha Patil
10. Search Tree - Data Structures using C++ by Varsha Patil10. Search Tree - Data Structures using C++ by Varsha Patil
10. Search Tree - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
Stacks in algorithems & data structure
Stacks in algorithems & data structureStacks in algorithems & data structure
Stacks in algorithems & data structurefaran nawaz
 
stacks in algorithems and data structure
stacks in algorithems and data structurestacks in algorithems and data structure
stacks in algorithems and data structurefaran nawaz
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Vineeta Garg
 
Structure of C++ - R.D.Sivakumar
Structure of C++ - R.D.SivakumarStructure of C++ - R.D.Sivakumar
Structure of C++ - R.D.SivakumarSivakumar R D .
 
11. Hashing - Data Structures using C++ by Varsha Patil
11. Hashing - Data Structures using C++ by Varsha Patil11. Hashing - Data Structures using C++ by Varsha Patil
11. Hashing - Data Structures using C++ by Varsha Patilwidespreadpromotion
 

Viewers also liked (20)

Advanced data structures using c++ 3
Advanced data structures using c++ 3Advanced data structures using c++ 3
Advanced data structures using c++ 3
 
Structured query language functions
Structured query language functionsStructured query language functions
Structured query language functions
 
Structured query language constraints
Structured query language constraintsStructured query language constraints
Structured query language constraints
 
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
13. Indexing MTrees - Data Structures using C++ by Varsha Patil13. Indexing MTrees - Data Structures using C++ by Varsha Patil
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
 
Pp
PpPp
Pp
 
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. Patil
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. PatilDiscrete Mathematics S. Lipschutz, M. Lipson And V. H. Patil
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. Patil
 
Working with Cookies in NodeJS
Working with Cookies in NodeJSWorking with Cookies in NodeJS
Working with Cookies in NodeJS
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
Data types in c++
Data types in c++Data types in c++
Data types in c++
 
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
 
C++ data types
C++ data typesC++ data types
C++ data types
 
Data Structure
Data StructureData Structure
Data Structure
 
Data file handling in c++
Data file handling in c++Data file handling in c++
Data file handling in c++
 
10. Search Tree - Data Structures using C++ by Varsha Patil
10. Search Tree - Data Structures using C++ by Varsha Patil10. Search Tree - Data Structures using C++ by Varsha Patil
10. Search Tree - Data Structures using C++ by Varsha Patil
 
Stacks in algorithems & data structure
Stacks in algorithems & data structureStacks in algorithems & data structure
Stacks in algorithems & data structure
 
stacks in algorithems and data structure
stacks in algorithems and data structurestacks in algorithems and data structure
stacks in algorithems and data structure
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Queues in C++
Queues in C++Queues in C++
Queues in C++
 
Structure of C++ - R.D.Sivakumar
Structure of C++ - R.D.SivakumarStructure of C++ - R.D.Sivakumar
Structure of C++ - R.D.Sivakumar
 
11. Hashing - Data Structures using C++ by Varsha Patil
11. Hashing - Data Structures using C++ by Varsha Patil11. Hashing - Data Structures using C++ by Varsha Patil
11. Hashing - Data Structures using C++ by Varsha Patil
 

Similar to Classes and objects1

Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methodsfarhan amjad
 
chapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfchapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfstudy material
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3Atif Khan
 
3 functions and class
3   functions and class3   functions and class
3 functions and classtrixiacruz
 
Unit vi(dsc++)
Unit vi(dsc++)Unit vi(dsc++)
Unit vi(dsc++)Durga Devi
 
CLASSES, STRUCTURE,UNION in C++
CLASSES, STRUCTURE,UNION in C++CLASSES, STRUCTURE,UNION in C++
CLASSES, STRUCTURE,UNION in C++Prof Ansari
 
Classes and objects
Classes and objectsClasses and objects
Classes and objectsShahid Javid
 
Object oriented programming 2
Object oriented programming 2Object oriented programming 2
Object oriented programming 2Aadil Ansari
 
Object and class presentation
Object and class presentationObject and class presentation
Object and class presentationnafisa rahman
 
Bca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objectsBca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objectsRai University
 
Mca 2nd sem u-2 classes & objects
Mca 2nd  sem u-2 classes & objectsMca 2nd  sem u-2 classes & objects
Mca 2nd sem u-2 classes & objectsRai University
 
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptclassandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptmanomkpsg
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ Dev Chauhan
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++Swarup Boro
 

Similar to Classes and objects1 (20)

Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
 
chapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfchapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdf
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3
 
Lecture 2 (1)
Lecture 2 (1)Lecture 2 (1)
Lecture 2 (1)
 
3 functions and class
3   functions and class3   functions and class
3 functions and class
 
ccc
cccccc
ccc
 
Unit vi(dsc++)
Unit vi(dsc++)Unit vi(dsc++)
Unit vi(dsc++)
 
CLASSES, STRUCTURE,UNION in C++
CLASSES, STRUCTURE,UNION in C++CLASSES, STRUCTURE,UNION in C++
CLASSES, STRUCTURE,UNION in C++
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Object oriented programming 2
Object oriented programming 2Object oriented programming 2
Object oriented programming 2
 
Object and class presentation
Object and class presentationObject and class presentation
Object and class presentation
 
Bca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objectsBca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objects
 
02.adt
02.adt02.adt
02.adt
 
Mca 2nd sem u-2 classes & objects
Mca 2nd  sem u-2 classes & objectsMca 2nd  sem u-2 classes & objects
Mca 2nd sem u-2 classes & objects
 
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptclassandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.ppt
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++
 
DS Unit 6.ppt
DS Unit 6.pptDS Unit 6.ppt
DS Unit 6.ppt
 
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
 
Class and object
Class and objectClass and object
Class and object
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++
 

Recently uploaded

Application of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesApplication of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesRased Khan
 
2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptxmansk2
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonSteve Thomason
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleCeline George
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resourcesaileywriter
 
NLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptxNLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptxssuserbdd3e8
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfVivekanand Anglo Vedic Academy
 
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxJose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxricssacare
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...Sayali Powar
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxRaedMohamed3
 
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptxSolid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptxDenish Jangid
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxakshayaramakrishnan21
 
Advances in production technology of Grapes.pdf
Advances in production technology of Grapes.pdfAdvances in production technology of Grapes.pdf
Advances in production technology of Grapes.pdfDr. M. Kumaresan Hort.
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPCeline George
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxEduSkills OECD
 
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...Nguyen Thanh Tu Collection
 
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfQucHHunhnh
 
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxMatatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxJenilouCasareno
 

Recently uploaded (20)

Application of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesApplication of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matrices
 
2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resources
 
NLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptxNLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptx
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxJose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
Operations Management - Book1.p  - Dr. Abdulfatah A. SalemOperations Management - Book1.p  - Dr. Abdulfatah A. Salem
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
 
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptxSolid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptx
 
Advances in production technology of Grapes.pdf
Advances in production technology of Grapes.pdfAdvances in production technology of Grapes.pdf
Advances in production technology of Grapes.pdf
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
 
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
 
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxMatatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 

Classes and objects1

  • 2. WHAT ARE OBJECTS? DEFINITION: An object is an entity, real or abstract that has some state and behavior. For example: dog, apple, bicycle etc. OBJECT STATE: The state or attributes of an object are the characteristics of an object and they describe the object. OBJECT BEHAVIOR: Object behavior is operations provided by or build into the object. OBJECT STATE BEHAVIOR DOG COLOR, BREED, HUNGRY etc. BARKING, FETCHING,W AGGINGTAIL etc. APPLE SHAPE, COLOR etc. SWEET IN TASTE, JUICY etc. BICYCLE WHEELS, GEARS, etc. ACCELRATIN G, CHANGING GEARS etc. Objects are the fundamental building blocks of an object oriented system.
  • 3. WHAT ARE CLASSES? SYNTAX: class user_defined_name { private: data members member functions protected: data members member functions public: data members member functions }; C++ allows us to create software objects similar to real world objects. DEFINITION: A class is a way to bind the data describing an entity and its associated functions together. It is a set of objects that share common attributes and behavior. Where class is a keyword user_defined_name is the tag name or class specifier private, protected, public are access specifiers
  • 4. CLASS : EXAMPLE class student { private: int roll; char name[20]; public: void indata() { cin>> roll; gets(name); } void outdata() { cout<< roll<<name;} } CLASS SPECIFIER /TAG NAME KEYWORD : CLASS ACCESS SPECIFIER DATA MEMBERS MEMBER FUNCTIONS
  • 5. ACCESS SPECIFIER An access specifier determines whether class members are accessible in various parts of our program. ACCESS SPECIFIER PRIVATE They are not accessible outside the class. Only member functions of the class can use them.They can not be inherited. PROTECTED They are not accessible outside the class. Only member functions of the class can use them. But they can be inherited. PUBLIC They are accessible outside the class.They can also be inherited. NOTE: In the absence of a specifier all the members defined in a class are considered to be private. Protected access specifier is discussed in detail in the inheritance PPT.
  • 6. CLASS MEMBERS Class members can be categorized as follows: CLASS MEMBERS DATA MEMBERS They are variables which describe the property of a particular class. Data members are generally kept in the private section to keep data safe from accidental changes. MEMBER FUNCTIONS They are functions which operate on data members.They are also called messages. A message determines what operations can be performed by an object. Objects communicate with each other through messages.They can be defined in two ways: Inside the classOutside the class
  • 7. DEFINING MEMBER FUNCTIONS INSIDE/OUTSIDETHE CLASS INSIDETHE CLASS: Functions are defined inside the class only if the function code is small. Such function definitions are considered inline by default. class student { private: int roll; char name[20]; public: void indata() { cin>> roll; gets(name); } void outdata() { cout<< roll<<name;} } OUTSIDETHE CLASS: In this case prototype of function is declared inside the class. SYNTAX: return type class_name :: function_name(argument list) class student { private: int roll; char name[20]; public: void indata(); void outdata(); }; void student :: indata() { cin>> roll; gets(name); } void student :: outdata() { cout<< roll<<name;}
  • 8. CREATING OBJECTS An object is an instance of a class. Objects represent variables of a user defined data type class. SYNTAX: class _name object_name; EXAMPLE: The object of class STUDENT declared above is as follows: student stu; Here stu is the name of the object of class STUDENT. NOTE: The class specification provides only a template and does not allocate any memory space.The necessary memory space is allocated only when the object of the class is created.
  • 9. CREATING OBJECTS Contd…..  When we create many objects of the same class, each object has its own copy of data members.  All the objects of the same class use the same set of member functions.  The member functions are created and placed in memory only once – when they are defined in the class specification.  This is feasible because functions for each object are identical, however data items will hold different values for each object. Student s1 Student s1 Student s3; void indata() void outdata() Member functions Data members
  • 10. ACCESSING MEMBER FUNCTIONS Member functions can be accessed using a dot operator between the object name and the member function. SYNTAX: object_name . Member_function; EXAMPLE: stu . indata(); The above statement means that a function indata() is being called for the object stu. Only public members (data and functions) can be accessed by the objects of the class. This function call can be described as sending a message indata() to the object stu. Any reference to class data in the indata() method will now be applied to the data members of the stu object.
  • 11. NESTING OF MEMBER FUNCTIONS When a member function of a class is called by another member function of the same class, it is called nesting of member functions. QWAP to create a classTravelPlan Data Members: PlanCode long Place char[20] Number_of_travelers int Number_of_buses int Member Functions: GetPlan() :function to allow the user to input data and call function NewPlan(). NewPlan() : Function to assign the values of Number_of_Buses as per the following: Number ofTravellers < 20, Number_of_Buses = 1. Number ofTravellers > 20 and < 40, Number_of_Buses = 2. Number ofTravellers > 40, Number_of_Buses = 3. ShowPlan() : A function to display the contents of all the data members.
  • 12. NESTING OF MEMBER FUNCTIONS Contd…. #include <iostream.h> #include <conio.h> #include <string.h> #include <stdio.h> classTravelPlan { long PlanCode; char Place[20]; int Number_of_travellers; int Number_of_buses; public : TravelPlan(); void GetPlan(); void NewPlan(); void ShowPlan(); }; voidTravelPlan::ShowPlan() { cout<<"nnnn"<<PlanCode; cout<<"nn"<<Place; cout<<"nn"<<Number_of_travellers; cout<<"nn"<<Number_of_buses; } void main() { clrscr(); TravelPlanTP; TP.GetPlan(); TP.ShowPlan(); getch(); } void GetPlan() { cin >> PlanCode; gets(Place); cin >> Number_of_travellers; NewPlan(); // function being //called by the function GetPlan() } voidTravelPlan::NewPlan() { if (Number_of_travellers < 20) Number_of_buses = 1; else if (Number_of_travellers < 40) Number_of_buses = 2; else Number_of_buses = 3; };
  • 13. ARRAY OF OBJECTS An array of objects is stored inside the memory in the same way as a multi- dimensional array.We can use the usual array accessing methods to access individual elements and then the dot operator to access the member functions. #include <iostream.h> #include <stdio.h> classTravelPlan { long PlanCode; char Place[20]; int Number_of_travellers; int Number_of_buses; public : TravelPlan(); void GetPlan(); void NewPlan(); void ShowPlan(); }; void GetPlan() { cin >> PlanCode; gets(Place); cin >> Number_of_travellers; NewPlan(); } voidTravelPlan::NewPlan() { if (Number_of_travellers < 20) Number_of_buses = 1; else if (Number_of_travellers < 40) Number_of_buses = 2; else Number_of_buses = 3; }; voidTravelPlan::ShowPlan() { cout<<"nnnn"<<PlanCode; cout<<"nn"<<Place; cout<<"nn"<<Number_of_travellers; cout<<"nn"<<Number_of_buses; } void main() { clrscr(); TravelPlanTP [ 10 ]; // Object array for( int i=0; i<10; i++ ) TP [ i ].GetPlan(); for( int i=0; i<10; i++ ) TP [ i }.ShowPlan(); getch(); }
  • 14. OBJECTSAS FUNCTIONARGUMENTS An object may be passed to the function as an argument. It can be done in two ways: CALL BYVALUE CALL BY REFERENCE In this method, a copy of the arguments is made and it is this copy that is used by the function. In this method, instead of a value being passed to the function, a reference of the object in the calling function is passed. The changes made to the members of the object inside the called function are reflected back to the calling function. The called function works directly on the actual object used in the call and changes made to the object in the called function will reflect in the actual object. When the function to which the copy of the object is passed terminates the copy of the argument is destroyed. No copy is made.Works on actual objects. To pass the object by reference the object name is preceded by “&” in the function definition.
  • 15. OBJECTSAS FUNCTIONARGUMENTS Contd….. #include <iostream.h> #include <conio.h> #include <string.h> #include <stdio.h> class time { int hour; int min; public : void GetTime (); void AddTime(time t1, time t2); void ShowTime(); }; void GetTime() { cin<< hour; cin<< min; } void ShowTime() { cout<< “ Hours=“<< hour; cout<<“Minutes=“<< min; } void AddTime(time t1 , time t2) { int h1; hour = t1.hour + t2.hour; min = t1.min + t2.min; if (hour>=60) h1= hour – 60; min= min + h1; } } void main() { time x1, x2, x3; x1.GetTime(); x2.GetTime(); x3.AddTime(x1, x2); x1.ShowTime(); x2.ShowTime(); x3.ShowTime(); }
  • 16. RETURNING OBJECTS When an object is returned by a function, a temporary object is automatically created which holds the return value. It is this object that is actually returned by the function. After the value is returned, this object is destroyed.
  • 17. RETURNINGOBJECTS Contd….. #include <iostream.h> #include <conio.h> class time { int hour; int min; public : void GetTime (); time AddTime(time t1); void ShowTime(); }; void GetTime() { cin<< hour; cin<< min; } void ShowTime() { cout<< “ Hours=“<< hour; cout<<“Minutes=“<< min; } time AddTime(time t1 ) { time t3; t3.hour = t1.hour +hour; t3.min = t1.min + min; if (t3.hour>=60) h1= t3.hour – 60; t3.min= t3.min + h1; } } void main() { time x1, x2, x3; x1.GetTime(); x2.GetTime(); x3= x2.AddTime(x1); x1.ShowTime(); x2.ShowTime(); x3.ShowTime(); }
  • 18. ASSIGNING OBJECTS C++ allows assigning one object to another object, provided they belong to the same class. By default, when one object is assigned to another, a bitwise copy of all data members is made. class A { int a1; public: void getdata(); void putdata(); } class B { int b1; public: void indata(); void outdata(); } void main() { A a11, a12; B b11, b12; a11.getdata(); a12= a11; // correct b11 = a11; // not allowed } Explanation: As a11 and a12 are objects of the same class, the operation a12=a11; is allowed. As b11 and a11 belong to different classes, the operation b11=a11; is not allowed.