SlideShare a Scribd company logo
Object Oriented
Programming using C++
Classes and object
Classes and Objects
Topics to be covered…….
Classes
Objects
Static Data members and functions
Constructors and its types
Destructors
Function overloading
Friend Functions
Inline Functions
Const member functions
Creation Of Classes and Objects
Classes in C++ are also called abstract data types.
Generally , a class has two parts:
1. Class declaration
2. Class function definitions
The class declaration describes the type and scope
of its members. The class function definitions
describe how the class functions are implemented.
The general form of a class declaration is:
class class_name
{
private:
variable declaration;
function declaration;
public:
variable declaration;
function declaration;
};
Here, keywords are class, private and public. private and
public are called access specifiers. The class variables and
functions are collectively called class members. Variables are
called data members and functions are called member
functions. The class members declared as public, can be
accessed from outside the class with the help of objects.
Whereas, the members declared as private can only be
accessed from within the class. If no specifier is mentioned,
then by default its private.
The binding of data and functions together into a single class-
type variable is referred to as Encapsulation. Encapsulation
implements data hiding and hence security.
Sample Class Example
class item
{
int number;
float cost;
public:
void getdata(int a, float b);
void putdata(void);
};
Member function definition
void item : : getdata(int a, float b)
{
number=a;
cost=b;
}
void item : : putdata(void)
{
cout<< number << cost;
Object Creation Example
item x, y, z;
Here x, y and z are objects of class item. The necessary memory
space is allocated to an object at this stage.
Accessing Class Member Example
The following is the format for calling the member function:
Object-name . function-name (actual-arguments);
Eg: x . getdata(100,75.5);
x . putdata( );
Nesting of member function
An Example…
class student
{
int tot_marks, roll_no;
char name[20];
public:
void get();
void show();
void disp_division();
};
void student:: show()
{ cout<<“Name”<<name;
cout<<“ Roll”<<roll_no;
cout<< disp_division();
}
void student:: get(){}
void student:: disp_division(){}
int main()
{
student S;
S.get();
S.show();
Return 0;
}
Array of Objects
Class student{};
void main()
{
student S[10];
for(int i=0;i<10;i++)
{
cout <<“ Details of
students”<<i+1<<“n;
Student[i].getdata();
}
for(int i=0;i<10;i++)
{cout <<“ Details of
students”<<i+1<<“n;
Student[i].showdata();
}
getch();
}
S[0],S[1],S[2]…..S[49]
Static Data members and Static Member functions
The static data member of a class is similar to a C static variable
with certain characteristics:
•it is declared with the keyword static followed by the type and
name of the variable
•it is initialized to zero when the first object of its class is created.
•Only one copy of this data member is shared by all the objects of
this class therefore they are also called class variables.
•Scope is local to the class in which it is declared and the lifetime
is entire program.
•The type and initialization of this data member is done outside
the class with the help of scope resolution operator and class
name.
Syntax for defining and initializing the static data member outside
the class:
type class-name : : static data member name= initialization value;
Like static data member we also have static
member functions with following properties:
•Static functions can have access to only static
members of the same class.
•They can be called using classname and scope
resolution operator(instead of using object name).
Syntax for calling the static member function: class-
name : : function-name( );
Class and object
Constructor
Constructor is a member function of a class that’s
used to create objects. Constructors have a special
property that their name is same as the class name
and they don’t return any value, not even void.
They are called automatically on the creation of
objects and allocate enough memory to the
objects. C++ supports various kinds of constructors.
These are:
1.Default constructor
2.Implicit constructor
3.Parameterized constructor
4.Copy constructor
5.Dynamic constructor
It’s not important to define a constructor in a class.
In that case an implicit constructor is called that
creates the objects. Every class has this constructor
by default. Default and Implicit constructors can be
overridden by other types so if other constructors
are defined then it becomes mandatory to define
default or implicit constructors explicitly.
Constructors must be defined in the public section
of the class. Having more than one type of
constructors in a class is called constructor
overloading.
Constructors must be defined in the public
section of the class. Having more than one type of
constructors in a class is called constructor
overloading.
Defining the types of constructors
1. Default constructor--- It takes no arguments
and initializes the data members of the class with
initial value zero at the time of creation.
Syntax: class-name( )
{
Datamember1=0;
Datamember2=0;
}
2.Implicit constructor--- It has no arguments and no body. It
just creates the object without initializing them. Present by-
default in the class.
Syntax : class-name( )
{
}
3.Parameterized constructor--- It has arguments as well as
body. It initializes the objects with desired values at the time
of creation.
Syntax: class-name( int a, float b)
{
Datamember1=a;
Datamember2=b;
}
4. Copy constructor --- It creates an object and
initializes it with the value of another object of
the same class.
Syntax: class-name( class-name & ob1)
{
Datamember1=ob1.
Datamember1;
}
Here, “&” is called reference operator and “ob1”
is the reference of the object passed to this
constructor as argument. Now the data members
of “ob1” will be copied to the data members of
calling object.
5.Dynamic constructor --- It creates the objects at rum time
by using the memory management operators new .
Syntax: class-name( char * s)
{
int len=strlen(s);
Datamember= new char[len+1];
strcpy (Datamember, s);
}
Destructor
Destructor is a member function like constructor that can
destroy the objects once they go out of scope or when the
program ends. A destructor is called automatically in any of
the above cases. However, we can define our own
destructor in a class.
A destructor has the same name as the class but it neither
takes nor return any arguments, not even void. Hence ,
destructor overloading is not possible. Destructor
declaration contains a ~ (tilde) sign before the name of the
class.
Syntax: ~class-name( )
{
cout<< “object destroyed”;
}
Program No.7
Create a class time with data members: ‘hrs’ and
‘mins’ and following member functions:
•default constructor
•parameterized constructor
•copy constructor
•sum() to add two time objects
•display() to display the time .
• Passing object as an argument to the
member function.
• Passing and retrieving of object into
the third variable.
• Will see them through one one
examples.
Class and object
Class and object
Friend function
Friend function is a non- member function of a class. It has the
ability of accessing the private members of the class. Friend
function can be used as a bridge between two classes by sharing
their private members.
Syntax for declaring friend function inside the class
class ABC
{
…………….
................
public:
……………
……………
friend void xyz( void);
};
Here, friend is a keyword. Its followed by the return type of the
function, its name and list of arguments.
Some important features of friend function:
1. A friend function is a non member function of a class. So its accessed
without an object.
2. A friend function is just declared in the class to which it has to be a friend
by using a keyword friend.
3. It is defined outside the class like a normal function ; without any class
name and scope resolution operator and without the keyword friend.
4. It has objects as arguments.
5. It can access the member names with the help of an object name and dot
membership operator.
6. It can be declared either in public or private part of a class without
affecting its meaning.
Example of using friend function as a
bridge between two classes
class Y; //forward declaration
class X
{
int a;
….
friend int fun1 ( X ob1, Y ob2);
….
};
class Y
{
Int b;
….
friend int fun1(X ob1, Y ob2);
};
int fun1( X ob1, Y ob2)
{
cout<< ob1.x <<ob2. y;
}
void main( )
{
X ob1; Y ob2;
………….
……………
fun1( ob1, ob2);
…
}
•

More Related Content

PPT
Class and object in C++
PPTX
Inheritance
PPTX
Polymorphism
PPTX
Inheritance, friend function, virtual function, polymorphism
PPTX
Friend functions
PDF
PPTX
Friend function & friend class
PPT
Lecture5
Class and object in C++
Inheritance
Polymorphism
Inheritance, friend function, virtual function, polymorphism
Friend functions
Friend function & friend class
Lecture5

What's hot (20)

PPTX
OOP C++
PPTX
Object as function argument , friend and static function by shahzad younas
PPTX
C# classes objects
DOCX
JAVA Notes - All major concepts covered with examples
PPT
Inheritance, polymorphisam, abstract classes and composition)
PPS
Inheritance chepter 7
PPTX
Inheritance
PPTX
Classes and objects
PDF
Chapter23 friend-function-friend-class
PDF
Java unit2
PPT
Java: Inheritance
PPT
Friends function and_classes
PPTX
class and objects
PPTX
Classes and Objects in C#
PPTX
Multiple inheritance possible in Java
PDF
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
PDF
Implementation of oop concept in c++
PPTX
Learn Concept of Class and Object in C# Part 3
PPTX
Object Oriented Programming Using C++
PPTX
+2 CS class and objects
OOP C++
Object as function argument , friend and static function by shahzad younas
C# classes objects
JAVA Notes - All major concepts covered with examples
Inheritance, polymorphisam, abstract classes and composition)
Inheritance chepter 7
Inheritance
Classes and objects
Chapter23 friend-function-friend-class
Java unit2
Java: Inheritance
Friends function and_classes
class and objects
Classes and Objects in C#
Multiple inheritance possible in Java
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Implementation of oop concept in c++
Learn Concept of Class and Object in C# Part 3
Object Oriented Programming Using C++
+2 CS class and objects
Ad

Similar to Class and object (20)

PPTX
OOPs & C++ UNIT 3
PPT
Mca 2nd sem u-2 classes & objects
PPT
Bca 2nd sem u-2 classes & objects
PPT
Unit vi(dsc++)
PDF
Unit_2_oop By Alfiya Sayyed Maam from AIARKP
PDF
Introduction to C++ Class & Objects. Book Notes
PPT
classandobjectunit2-150824133722-lva1-app6891.ppt
PPT
classes & objects.ppt
PPTX
OBJECT ORIENTED PROGRAMING IN C++
PDF
22 scheme OOPs with C++ BCS306B_module1.pdf
PPT
static member and static member fumctions.ppt
PPTX
Oop objects_classes
PPTX
Lecture 4.2 c++(comlete reference book)
PPT
cpp class unitdfdsfasadfsdASsASass 4.ppt
PDF
classes and objects.pdfggggggggffffffffgggf
PPTX
05 Object Oriented Concept Presentation.pptx
PDF
Class object
PPTX
class c++
PPT
DS Unit 6.ppt
PDF
Classes-and-Objects-in-C++.pdf
OOPs & C++ UNIT 3
Mca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objects
Unit vi(dsc++)
Unit_2_oop By Alfiya Sayyed Maam from AIARKP
Introduction to C++ Class & Objects. Book Notes
classandobjectunit2-150824133722-lva1-app6891.ppt
classes & objects.ppt
OBJECT ORIENTED PROGRAMING IN C++
22 scheme OOPs with C++ BCS306B_module1.pdf
static member and static member fumctions.ppt
Oop objects_classes
Lecture 4.2 c++(comlete reference book)
cpp class unitdfdsfasadfsdASsASass 4.ppt
classes and objects.pdfggggggggffffffffgggf
05 Object Oriented Concept Presentation.pptx
Class object
class c++
DS Unit 6.ppt
Classes-and-Objects-in-C++.pdf
Ad

Recently uploaded (20)

PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
Structs to JSON How Go Powers REST APIs.pdf
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPT
Project quality management in manufacturing
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
OOP with Java - Java Introduction (Basics)
PPT
Mechanical Engineering MATERIALS Selection
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
composite construction of structures.pdf
PPTX
Geodesy 1.pptx...............................................
PPTX
Construction Project Organization Group 2.pptx
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
PPT on Performance Review to get promotions
Strings in CPP - Strings in C++ are sequences of characters used to store and...
Model Code of Practice - Construction Work - 21102022 .pdf
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Structs to JSON How Go Powers REST APIs.pdf
Embodied AI: Ushering in the Next Era of Intelligent Systems
Project quality management in manufacturing
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
OOP with Java - Java Introduction (Basics)
Mechanical Engineering MATERIALS Selection
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
composite construction of structures.pdf
Geodesy 1.pptx...............................................
Construction Project Organization Group 2.pptx
Arduino robotics embedded978-1-4302-3184-4.pdf
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPT on Performance Review to get promotions

Class and object

  • 1. Object Oriented Programming using C++ Classes and object
  • 3. Topics to be covered……. Classes Objects Static Data members and functions Constructors and its types Destructors Function overloading Friend Functions Inline Functions Const member functions
  • 4. Creation Of Classes and Objects Classes in C++ are also called abstract data types. Generally , a class has two parts: 1. Class declaration 2. Class function definitions The class declaration describes the type and scope of its members. The class function definitions describe how the class functions are implemented.
  • 5. The general form of a class declaration is: class class_name { private: variable declaration; function declaration; public: variable declaration; function declaration; };
  • 6. Here, keywords are class, private and public. private and public are called access specifiers. The class variables and functions are collectively called class members. Variables are called data members and functions are called member functions. The class members declared as public, can be accessed from outside the class with the help of objects. Whereas, the members declared as private can only be accessed from within the class. If no specifier is mentioned, then by default its private. The binding of data and functions together into a single class- type variable is referred to as Encapsulation. Encapsulation implements data hiding and hence security.
  • 7. Sample Class Example class item { int number; float cost; public: void getdata(int a, float b); void putdata(void); }; Member function definition void item : : getdata(int a, float b) { number=a; cost=b; } void item : : putdata(void) { cout<< number << cost;
  • 8. Object Creation Example item x, y, z; Here x, y and z are objects of class item. The necessary memory space is allocated to an object at this stage. Accessing Class Member Example The following is the format for calling the member function: Object-name . function-name (actual-arguments); Eg: x . getdata(100,75.5); x . putdata( );
  • 9. Nesting of member function An Example… class student { int tot_marks, roll_no; char name[20]; public: void get(); void show(); void disp_division(); }; void student:: show() { cout<<“Name”<<name; cout<<“ Roll”<<roll_no; cout<< disp_division(); } void student:: get(){} void student:: disp_division(){} int main() { student S; S.get(); S.show(); Return 0; }
  • 10. Array of Objects Class student{}; void main() { student S[10]; for(int i=0;i<10;i++) { cout <<“ Details of students”<<i+1<<“n; Student[i].getdata(); } for(int i=0;i<10;i++) {cout <<“ Details of students”<<i+1<<“n; Student[i].showdata(); } getch(); } S[0],S[1],S[2]…..S[49]
  • 11. Static Data members and Static Member functions The static data member of a class is similar to a C static variable with certain characteristics: •it is declared with the keyword static followed by the type and name of the variable •it is initialized to zero when the first object of its class is created. •Only one copy of this data member is shared by all the objects of this class therefore they are also called class variables. •Scope is local to the class in which it is declared and the lifetime is entire program. •The type and initialization of this data member is done outside the class with the help of scope resolution operator and class name. Syntax for defining and initializing the static data member outside the class: type class-name : : static data member name= initialization value;
  • 12. Like static data member we also have static member functions with following properties: •Static functions can have access to only static members of the same class. •They can be called using classname and scope resolution operator(instead of using object name). Syntax for calling the static member function: class- name : : function-name( );
  • 14. Constructor Constructor is a member function of a class that’s used to create objects. Constructors have a special property that their name is same as the class name and they don’t return any value, not even void. They are called automatically on the creation of objects and allocate enough memory to the objects. C++ supports various kinds of constructors. These are: 1.Default constructor 2.Implicit constructor 3.Parameterized constructor 4.Copy constructor 5.Dynamic constructor
  • 15. It’s not important to define a constructor in a class. In that case an implicit constructor is called that creates the objects. Every class has this constructor by default. Default and Implicit constructors can be overridden by other types so if other constructors are defined then it becomes mandatory to define default or implicit constructors explicitly. Constructors must be defined in the public section of the class. Having more than one type of constructors in a class is called constructor overloading.
  • 16. Constructors must be defined in the public section of the class. Having more than one type of constructors in a class is called constructor overloading. Defining the types of constructors 1. Default constructor--- It takes no arguments and initializes the data members of the class with initial value zero at the time of creation. Syntax: class-name( ) { Datamember1=0; Datamember2=0; }
  • 17. 2.Implicit constructor--- It has no arguments and no body. It just creates the object without initializing them. Present by- default in the class. Syntax : class-name( ) { } 3.Parameterized constructor--- It has arguments as well as body. It initializes the objects with desired values at the time of creation. Syntax: class-name( int a, float b) { Datamember1=a; Datamember2=b; }
  • 18. 4. Copy constructor --- It creates an object and initializes it with the value of another object of the same class. Syntax: class-name( class-name & ob1) { Datamember1=ob1. Datamember1; } Here, “&” is called reference operator and “ob1” is the reference of the object passed to this constructor as argument. Now the data members of “ob1” will be copied to the data members of calling object.
  • 19. 5.Dynamic constructor --- It creates the objects at rum time by using the memory management operators new . Syntax: class-name( char * s) { int len=strlen(s); Datamember= new char[len+1]; strcpy (Datamember, s); }
  • 20. Destructor Destructor is a member function like constructor that can destroy the objects once they go out of scope or when the program ends. A destructor is called automatically in any of the above cases. However, we can define our own destructor in a class. A destructor has the same name as the class but it neither takes nor return any arguments, not even void. Hence , destructor overloading is not possible. Destructor declaration contains a ~ (tilde) sign before the name of the class. Syntax: ~class-name( ) { cout<< “object destroyed”; }
  • 21. Program No.7 Create a class time with data members: ‘hrs’ and ‘mins’ and following member functions: •default constructor •parameterized constructor •copy constructor •sum() to add two time objects •display() to display the time .
  • 22. • Passing object as an argument to the member function. • Passing and retrieving of object into the third variable. • Will see them through one one examples.
  • 25. Friend function Friend function is a non- member function of a class. It has the ability of accessing the private members of the class. Friend function can be used as a bridge between two classes by sharing their private members. Syntax for declaring friend function inside the class class ABC { ……………. ................ public: …………… …………… friend void xyz( void); }; Here, friend is a keyword. Its followed by the return type of the function, its name and list of arguments.
  • 26. Some important features of friend function: 1. A friend function is a non member function of a class. So its accessed without an object. 2. A friend function is just declared in the class to which it has to be a friend by using a keyword friend. 3. It is defined outside the class like a normal function ; without any class name and scope resolution operator and without the keyword friend. 4. It has objects as arguments. 5. It can access the member names with the help of an object name and dot membership operator. 6. It can be declared either in public or private part of a class without affecting its meaning.
  • 27. Example of using friend function as a bridge between two classes class Y; //forward declaration class X { int a; …. friend int fun1 ( X ob1, Y ob2); …. }; class Y { Int b; …. friend int fun1(X ob1, Y ob2); }; int fun1( X ob1, Y ob2) { cout<< ob1.x <<ob2. y; } void main( ) { X ob1; Y ob2; …………. …………… fun1( ob1, ob2); … } •