28. Class and Object
• Class is a user defined datatype which holds its
owns data member and member function in
another word we can say class is collection of
data member and member function which can
accessed and use by creating object of that class
• An object is an instance of a class when ever class
is defined , on memory is allocated but when
object is initialized memory is allocated of that
class
29. Object
• An object is an instance of a class . When ever
class is defined , no memory is allocated but
when object is initialized memory is allocated
of that class .
• Their are three types of access specifier in
class
1. Private
2. Public
3. Protected
30. Declaration of class
• Syntax
• Class(class is a keyword) class Name
{
Private:
data member
member function
Public:
data member
member function
Protected:
data member
member function
}
Every class ends with semi- colunn.
31. Class(Example)
# incude<iostream.h>
Class demo {
private:
int a, b;
Public:
void input()
{
cout <<“enter the values”;
cin>> a>> b;
}
void show()
{
cout<<a<<“ ”<<b
}
};
Void main()
{
demo ob,ob1;
ob.input();
ob. Show();
ob1.input();
ob1.input();
32. Inheritance
• Inheritance is a mechanism in which one class
inherits the property of other calls is know as
inheritance .
• We can say when one class access the
property of another class is called inheritance
• Example : father and son
33. Types of inheritance
1. Single inheritance / Simple inheritance.
one base class and one derived class
2. Multi- level in heritance.
one base class and Multiple derived class
3. Multiple inheritance.
One or two base class but only one derived class
4. Hierarchical inheritance.
one base class and derived class are more then one
5. Hybrid inheritance.
• HYBRID INHEITANCE* IT TAKE MORE THEN TWO
INHERITANCE GIVEN above
• Like in example single + multi- level inheritance
34. Base
Derive
Derive 2
Base
Derive 1
1. Simple inheritance 2. Multi – level inheritance
3. Multiple – inheritance 4. Hierarchical inheritance
Base 1 Base 2
Derive
Base
Derive 1 Derive 2
36. Encapsulation
• It is one of the most important features of
oops that used to wrapping the data and
function into a single unit . the data of class is
not accessible to out side the class only those
function access data which are wrapped in the
class.(information hiding )
• Example : Class is an example of encapsulation
which binding the data and function together .
It can be used by object of class
37. Example of Encapsulation C++
Class A
{
//int a,b; // by default private //
public:
void show()
{
Cout<<“ Enter the value of a & b”<<endl;
Cin>>a>>b;
Cout <<a<<“ ”<<b
}
void sum()
{
Cout<<“ enter the value of a and b ”<<endl;
Cin>> a>>b;
Cout<< sum << a+b<<endl;
}
Private: // we have to make it private by using key word encapsulation we hide the data //
Int a,b; // by default public //
];
void main()
{
A obj, obj2;
obj.show();
obj2.sum();
}
38. Abstraction in C++
• Abstraction is the one of the most important
features of oop’s which is sharing only the
essential information to out side world and
hiding the internal details .
Abstraction
using Class
using header file
39. Example using class
• Class A
{
private:
int a=11; //XXXXXXX//
public:
void show()
{
cout<<a;
}
};
Main()
{
A obj;
obj.show();
}
40. Using header file
# include<math.h>
Main()
{
Int a=4,b;
b= sqrt(a);
Cout<<b;
}
41. Constructor
• Why we use constructor in C++
Constructor is a special member function of a class which
used to create and initialize the objects
Feature:
1. A constructor can only have one access modifier which
is public.
2. A constructor is never inherited an overridden.
3. Each and every c++ class has constructor either it is
provided by compiler by default or explicitly cratered
42. Destructor
• Destructor is a special member function that
executed automatically when an object is
destroyed that has been created by the
constructer.
• Destructor are used to de- allocate the
memory that has been allocated for the object
by the constructor
• A destructor declaration should always begin
with the tilde (~) symbol as show in .
45. Ex . Of constructor
Class A
{
Private:// optional .
Public:// we have to do to access out side.
Int a;
/* A()
{
a=100;
} */
void show()
{
cout<<a;
}
};
Void main()
{
A obj=A(); // A obj;
obj.show()
}
46. Types of constructor of c++
1. Default constructor
2. Parameterized constructor
3. Copy constructor
47. Default constructor :- default constructor is the
constructor which does not take any argument and it
has no parameters
• Syntax
Class Classname
{
Public:
Classname()
{
member data;
member function;
}
};
48. Parameterized constructor in c++
• A constructor which has parameters is called
parameterized constructor
• Constructor is used to provide different value
to distinct object.
49. Example . WAP to add two no using parameterized
constructor
#include<iostream>
using namespace std;
Class Add
{
Public;
Add(int a, int b)
{
int c = a+b;
cout<<“sum=”<<c;
}
};
Int main()
{
Add ob(10,20)
return 0;
}
50. C++ copy constructor
• A copy constructor is a an overloaded constructor
used to declare and initialize an object from
another object .
There are two types of copy constructor
1. Default copy constructor => The compiler
defines the default copy constructor. If the user
defines no copy constructor , compiler supplies
its constructor .
2. User defined constructor => The programmer
defines the user-defined constructor
51. Syntax of user define copy constructor
#include<iostream>
using namespace std;
Class CopyTest
{
public:
int x;
CopyTest(int a) // Parameterized constructor
{
x=a;
}
CopyTest(CopyTest & i) // Copy constructor
{
x= i.x;
}
};
Int main()
{
CopyTest a1(20);
CopyTest a2(a1);
cout<<a2.x;
return 0;
}
52. Inline function
• Inline is a request to the compiler(not a
command) to make a function as an inline
function. To reduce the overhead of function
calling . If compiler treat a function as a inline
function it substitute the code of function in a
single line
• Inline function should contain a single
instruction
• Ex: request only
54. Program on inline function
#include<iostream.h>
Inline int fun( int a, int b)
{
return(a>b)? a:b;
}
Void main()
{
cout<<“Maximum” fun(10,20);
//
Int s;
s=fun(30,12);
Cout<“maximum”<<s;
//
}
55.
56.
57. Function Overloading vs Function
Overriding in C++
• Function Overloading (achieved at compile time)
• Function Overloading provides multiple
definitions of the function by changing
signature i.e. changing number of parameters,
change datatype of parameters, return type
doesn’t play any role.
• It can be done in base as well as derived class
58. Function Overriding (achieved at run time)
• It is the redefinition of base class function in its derived
class with same signature i.e. return type and parameters.
• It can only be done in derived class.
• Example:
Class a
{
public: virtual void display()
{ cout << "hello"; }
};
Class b:public a
{ public: void display()
{ cout << "bye";} };
59. Function Overloading Function Overriding
Function Overloading provides multiple
definitions of the function by changing signature.
Function Overriding is the redefinition of base
class function in its derived class with same
signature.
An example of compile time polymorphism. An example of run time polymorphism.
Function signatures should be different. Function signatures should be the same.
Overloaded functions are in same scope. Overridden functions are in different scopes.
Overloading is used when the same function has
to behave differently depending upon
parameters passed to them.
Overriding is needed when derived class function
has to do some different job than the base class
function.
A function has the ability to load multiple times. A function can be overridden only a single time.
In function overloading, we don’t need
inheritance.
In function overriding, we need an inheritance
concept.
60. Default Argument s
# include <iostream>
Using namespace std;
Int sum(int a, int b, int c=b)
{Return a+b+c;}
Int main()
{
Int a=5 ,b=6;
Cout<< sum(a,b);
Return 0;
}
79. Static Data member
whenever we declare a data member as a
static either inside or outside of a class called
static data member
There is only one copy of static data member
even it there are many class objects
It is always initialized with zero because it’s
default value is ZERO
It is shared memory for all object of the class
It retains it’s values
80. Static member function
If we create a member function of a class as a
static called static called static member
function
It is access only static data members
It is also accessible if we don’t have any object
of class
81. Class A
{ Int a;
static int b;
public:
A(int x, int y)
{
a=x; b=y;
}
void show()
{
cout<<a<<“ ”<< b;
}
static void disp()
{
cout<<b; // a is not accessible
}
};
Int A:: b=0; // we have set Zero
Int main()
{
A obj(30,40), obj2(70,80);
obj.show()
obj2.show()
A:: disp(); // with out object
Obj.show()
}
a=30
B=40/80
a=70
82. Friend function
• Friend function is a function which is not the member of the class instant of that it
can access private and protected member of class.
• Syntax
friend return_type function name (class ob)
{
body
}
• Friend function is declared in the class with friend keyword.
• Friend function can become friend to more then one class.
83.
84. Friend class
If a class become a friend class to a other class
then it can access the all function ex . Private
and protected member of that class
Friend class is declared always with friend
keyword
Syntax
85. Class A
{
Public :
member function()
{
-----
-----
}
Friend class B;
};
Class B
{
public :
member function (A ob)
{
----
-----
};
86. Class A
{ int a,int b;
Public :
Void input ()
{ cout<< “Enter two number”;
cin>> a>>b}
friend class B;
};
Class B
{
int c;
public:
void add(A ob)
{ c= ob.a+ ob.b;
cout<< sum << c}
};
int main()
{
A ao ; B kk;
ao.input();
kk.add(ao);
87. Object as function argument
We have 2 way
1. A copy of the entire object is passed to the
function -> pass by value
2. Only the address of the object is transferred
to the function -> pass by reference
88. Class total
{
int n;
Public :
void getdata(void);
void putdata(void);
void add(total,total);
};
Void total :: getdata(void)
{
cout <<“enter number:”;
cin>> n; endl;
}
Void total:: add(total x ,y)
{
n= x.n + y.n;
}
Void total:: putdata(void)
{
Cout<<n;endl;
}
Int main()
{
total r, s, result;
r.getdata();
s.getdata();
Cout<< “value of A :”;
r.Putdata()
Cout<< “value of B: ”;
s.Putdata();
Result.add(r,s);
Cout<< “addition :”;
result. Putdata();
}
90. Dynamic initialization of object
• Dynamic initialization of object refers to
initializing the object at run time . Ex... The initial
value of an object is to be provided during run
time.
• Dynamic initialization of object can be achieved
using constructors and passing parameters value
to the constructor(parameterized constructor).
• This type of initialization is required to initialize
the class variables during run time.
91. Example of dynamic initialization of object
#include<iostream.h>
Class point{ int a,b;
public :
point (int x1,int y1) // parameterized constructor //
{
a=x1;
b=y1;
}
Int getA()
{
return a;
}
Int getB()
{
return b;
}
};
Void main()
{
Int x1,y1;
Cout<<“Enter first value:”;
Cin>> x1;
Cout<<“Enter second value”;
Cin>> y1;
Point p1(x1,y2) // dynamic initiation of objecct //
Cout<<“p1.a=”<<p1.getA(),<<“p1.b=” <<p1.getB();
}
92. Pointer to object
• Item X
• Item *ptr
• Object pointer are useful in creating object at
runtime
Object
defined
Class
Name
Pointer of
type item
93. • We can also use an object pointer to access the
public member of an object.
• SYNTAX:
Class_name object;
Class _name *pointer_name;
INITIALIZATION:
pointer_name=&object;
EX.
Class obj;
Class *obj;
Pobj=&obj;
94. Accessing member using pointer
object
• Syntax:
Pointer_name->function_name()
Pointer_name->member_name;
(*pointer_name).function_name();
(*pointer_name).member_name;
EX.
Pdell->disp()
Pdell->usb;
(*pdell).pisp()
(*pdell).usb;
95. This keyword in c++
• Whenever the name of instance variable and local
variable both are same and if we initialize instance
variable with help of local variable then our compiler
totally confused that which one is local variable and
which one is instance variable
• to avoid this problem we use this keyword
int a,b;
A(int a,int b)
{
This->a=a; this->b=b;
}
96. • In c++ programming this is a keyword that
refers to the current instance of the class
there can be 3 main usage of this keyword in
c++
• it can be used to pass current object as a
parameter to another method
• It can be used to refer current class instance
variable
• it can be used to declare indexers
97. Reference variable in C++
• A variable which are provide alternate name
for the previously defined or existing variable
is called reference variable.
• Ex-> int a=10;
• int &b=a; a
• b
10
123456