Classes & Objects
What is Class?
It is a user defined data type. Which holds its
own data members (int a, float b, double c …
ect) and member functions (you study early),
which can be accessed by creating an instance
(also called object) of that class.
Data members and functions are called
properties of the class.
Points to be noted about class
• So, from above declaration we can say class is a way to bind
data members and function together .In object oriented
Programming (OOP) to binding data(Variables) and Method
together into a single class is referred to as encapsulation.
• The body of class is enclosed within braces ({ } ) and
always ends with semicolon (;).
• The keywords private and public are known as Access
specifiers
• By default members of class are private, if no access
specifier declare.
There are three types of access specifiers in c++.
• public
• private
• protected (this concept is better understand
under inheritance topic)
Accessing Private Data Members
• Only member function can have access the
private data member.
• Member function are Those Function Which is
declare inside a class body.
Class Method Defining
• Member function or class method can be
defined in two Places.
1. Outside the class
2. Inside the class
Function Outside the class
If we define the function outside the
class ,first we declare with in a class by
terminate semicolon(;) and then define
outside the class body by using the scope
resolution :: operator along with class name
and with function name.
Constructors
A constructor is a member function of a class
which initializes objects of a class. In C+
+,Constructor is automatically called when
object(instance of class) create. It is special
member function of the class.
Constructors
How constructors are different from a normal
member function?
• Constructor has same name as the class itself
• Constructors don’t have return type, not even
void
• A constructor is automatically called when an
object is created.
• If we do not specify a constructor, C++ compiler
generates a default constructor for us (expects
no parameters and has an empty body).
Types of Constructors
1.Default Constructors: Default constructor is
the constructor which doesn’t take any
argument. It has no parameter.
// Cpp program to illustrate the concept of Constructors
#include <iostream.h>
class construct
{
public:
int a, b;
// Default Constructor
construct()
{
a = 10;
b = 20;
}
};
void main()
{
// Default constructor called automatically
// when the object is created
construct c;
cout << "a: "<< c.a << endl << "b: "<< c.b;
}
Parameterized Constructor
A constructor which has parameters is called
parameterized constructor. It is used to
provide different values to distinct objects.
#include <iostream.h>
class Employee
{
public:
void main()
int id;//data member (also instance
{
variable) Employee e1 =Employee(101, "sar",
string name;//data member(also instance 8900f); //creating an object of Employee
variable) Employee e2=Employee(102, "Nakul", 59000f);
float salary;
e1.display();
Employee(int i, string n, float s) e2.display();
{ }
Output:
id = i;
101 sar 890000
name = n; 102 Nakul 59000
salary = s;
}
void display()
{
cout<<id<<" "<<name<<"
"<<salary<<endl;
}
};
Constructor Overloading in C++
• In C++, We can have more than one
constructor in a class with same name, as long
as each has a different list of arguments.This
concept is known as Constructor Overloading
and is quite similar to function overloading.
• Overloaded constructors essentially have the
same name (name of the class) and different
number of arguments.
• A constructor is called depending upon the
number and type of arguments passed.
• While creating the object, arguments must be
passed to let compiler know, which
constructor needs to be called.
// C++ program to illustrate Constructor overloading
void main()
#include <iostream.h>
class construct
{
{ // Constructor Overloading
// with two different constructors
public:
float area;
// of class name
construct o;
// Constructor with no parameters construct o2( 10, 20);
construct()
{
area = 0; o.disp();
} o2.disp();
// Constructor with two parameters
}
construct(int a, int b) Output:
{ 0
area = a * b;
}
200
void disp()
{
cout<< area<< endl;
}
};
What is destructor?
• Destructor is a member function which destructs or
deletes an object.or free memory
• How destructors are different from a normal
member function?
• Destructors have same name as the class preceded
by a tilde (~)
• Destructors don’t take any argument and don’t
return anything
Note
• If we do not write our own destructor in class,
compiler creates a default destructor for us.
The default destructor works fine unless we
have dynamically allocated memory or pointer
in class. When a class contains a pointer to
memory allocated in class, we should write a
destructor to release memory before the class
instance is destroyed. This must be done to
avoid memory leak
#include <iostream>
class Employee
{
public:
Employee()
{
cout<<"Constructor Invoked"<<endl;
}
~Employee()
{
cout<<"Destructor Invoked"<<endl;
}
};
int main(void)
{
Employee e1; //creating an object of Employee
Employee e2; //creating an object of Employee
return 0;
}
Constructor Invoked
Constructor Invoked
Destructor Invoked
Destructor Invoked
Operator overloading
Suppose I,j,k are variable of int type. The
statement k=i+j is valid.
Suppose a,b,c are object of integer class ,and if we
want to collect the sum of the member data of the
object a and b in the object c. we cannot write
c=a+b?
Operator overloading is one of the feature in c++ .
It assign additional functionality to the exiting
operators.
Syntax of operator overloading
type operator operator_to_be_overloaded(formal argument)
{
Local variable
Statements
Return statement
}
#include<iostream>
using namespace std;
class integer
{
private:
int x;
public:
integer (int y=0)
{
x=y;
}
integer operator -()
{
integer t;
t.x=-x;
return t;
}
void display()
{
cout<<"x="<<x<<"\n";
}
};
int main()
{
integer a,b(5),c(-7);
cout<<"b=";
b.display();
a=-b;
cout<<"-a=";
a.display();
cout<<"c=";
c.display();
a=-c;
cout<<"a=";
a.display();
return 0;
}
#include<iostream>
using namespace std;
class integer
{
private:
int x;
public:
integer (int y=0)
{
x=y;
}
integer operator ++()
{
++x;
}
integer operator ++(int)
{
x++;
}
void display()
{
cout<<x<<"\n";
}
};
int main()
{
integer a(5);
cout<<"a=";
a.display();
++a;
cout<<"after ++a;,a=";
a.display();
a++;
cout<<"after a++;,a=";
a.display();
return 0;
}
Overload binary operator
* / < <=
type operator operator_to_be_overloaded(formal argument)
{
Local variable
Statements
Return statement
}
Note that special member function used for overloading a binary
operator require one more argument of the class type. This is
because operator can operate on two object .
#include<iostream>
int main()
using namespace std;
{
class measure
measure m1(2,3),m2(4,10),m3;
{
cout<<"m1:";
private:
m1.display();
int feet;
cout<<"m2:";
float inches;
m2.display();
public:
m3=m1+m2;
measure (int f=0,float i=0)
cout<<"sum=";
{
m3.display();
feet=f;
return 0;
inches=i;
}
}
measure operator +(measure &m)
{
measure t;
t.feet=feet+m.feet;
t.inches=inches+m.inches;
if(t.inches>=12)
{
t.feet++;
t.inches-=12;
}
return (t);
}
void display()
{
cout<<feet<<"-"<<inches<<"\n";
}
};
Inheritance
In C++, inheritance is a fundamental concept in object-oriented
programming (OOP) that allows a class (called the derived class) to
inherit properties and behaviors (i.e., member variables and functions)
from another class (called the base class).
#include <iostream> Example of Single Inheritance:
using namespace std;
// Base class
class Animal {
public:
void sound() {
cout << "Animal makes a sound" << endl;
}
};
// Derived class
class Dog : public Animal {
public:
void sound() {
cout << "Dog barks" << endl;
}
};
int main() {
Dog d;
d.sound(); // Calls the sound() method from Dog class
return 0;
}