Procedural: Object Oriented

You might also like

You are on page 1of 130

C++

High Level Language

Object Oriented
Procedural
C
Partial Fully

C++ JAVA

Procedural Object Oriented


Focus on procedure Focus on data
Coding is Lengthy Coding Minimized
Cost is high Cost is low
Maintainance complex Maintainance Simple
No reusability Yes reusability present
by inheritance
Top to bottom Bottom to top
procedure of coding. procedure of coding.
1972:- C Dennis Ritchie

1980s:- C++ [c with class]


Developed by Bjarne Strostrup

C+simula 67c++

C++:- Procedural + object Oriented


[both types of languages.]

A Sample programe in C:-

#include<stdio.h>
#include<conio.h>
Void main()
{
Printf(hello);
Getch();
}

C++:-

#include<iostream.h>
#include<conio.h>
Void main()
{
Cout<<hello;
Getch();
}

OOPS:-
#include<iostream.h>
#include<conio.h>
Class A;
{

Void disp()
{
Cout<<hello;
}
};
Void main()
{
A obj;
Obj.disp();
Getch();
Return(0);
}

Object Oriented Programming:-

In object oriented programming A program is


conceptually organized around its data and
associated methods.

Features of object oriented programming


Data Abstraction & data hiding
Encapsulation
Inheritance
Polymorphism:
1)compile time polymorphism
a)function overloading
b)operator overloading
2)run time polymorphism
a)virtual classes
b)virtual functions

We can understand all features by using real


life example or programming example
But first we will understand all features by
real life example

Abstraction: - To show the essential feature


without showing the background details or
complexity. This is called Abstraction

Encapsulation: - To wrapping up data and


function in a single entity (class) is called
encapsulation

Inheritance: - To inherit property of parent


into child is called inheritance.

Polymorphism: - Poly means many and


morphism means forms an entity can be
present in more than one forms in real life it is
called polymorphism.

We can define or understand above things by:-


1) real life example
2) Implementation in language.
CLASS AND OBJECT :-

Class:- A class is a user define data type that


combine both member variable of primary and
derived data type and member function in it. A
class is a fundamental block of an object
oriented programming.
Class is the most powerful data type in C++, a
class defines the data and behavior(functions)
of the data type. Programmers can then create
objects that are instances or variable of this
class. classes support inheritance, a
fundamental part of object-oriented
programming.

Syntax To declare a class:-

class classname
{
Access specifiers:
variable declration;
//data members.
access specifier:
function declraration;
//member functions
}

class:- it is a keyword that tell to the compiler


new data type is declared.
classname:- it is a identifier.
access modifier:- they control the accessibility
of class member from outside boundary of
class.

Object:-
Objects are instances of a given data type .the
data type provides a blueprint for the object
that is created ,or instantiated when the
application is executed.

A sample program by using class :-


#include<iostream.h>
#include<conio.h>
class A
{
public:
void disp()
{
cout<<Hello Friends;
}
};
void main()
{
A obj;
obj.disp();
getch();
}

Sum of two no. by using Class.


#include<iostream.h>
#include<conio.h>
class A
{
Int a,b,c;
public:
void read()
{
cout<<enter two no.;
cin>>a>>b;
void sum()
Void display()
{
cout<<sum=<<c;
}
Void A:: sum();
{
C=a+b;
};
void main()
{
A obj;
Obj.read();
Obj.sum();
Obj.display();
Cout<<the sum
of<<a<<and<<b<<is:<<c;
getch();
return (0);
}
Access Modifier:
They are used for control the accessibility of
class member from outside boundary of class.

There are 3 access modifier present in C++.


1.) Public
2.) Private
3.) Protected

Public:- The all members that are declared


under public section can be access inside the
boundary of class and can be access outside
boundary of class.

Private:- The all members that are declared


under private section can be access inside the
boundary of class but can not be access
outside boundary of class.

Protected: - Same as private in a class but its


mean change in inheritance.

Access specifier:
Used in class Used in outside
of class
Public Yes Yes
Private Yes No
Protected Yes No
Note: By default Access modifier is private.

For Eg.:

#include<iostream.h>
#include<conio.h>
class A
{
int x,y,z;
public:
void disp()
{
z=x+y;
cout<<sum is <<z;
}
};
void main()
{
A obj;
obj.x=90;
obj.y=10;
obj.disp();
getch();
}

Eg:

#include<iostream.h>
#include<conio.h>
class A
{
int x,y,z;
public:
void disp()
{
z=x+y;
cout<<sum is <<z;
}
};
void main()
{
Int a,b;
A obj;
Cout<<Enter two no.;
Cin>>obj.a;
Cin>>obj.b;
Obj.disp();
getch();
}

#include<iostream.h>
#include<conio.h>
class A
{
int x,y,z;
public:
void input()
{
cout<<Enter two numbers;
cin>>x>>y;
}
void disp()
{
z=x+y;
cout<<sum is <<z;
}
};
void main()
{
A obj;
obj.input();
obj.disp();
getch();
}

#include<iostream.h>
#include<conio.h>
class A
{
int x,y,z;
void input()
{
cout<<Enter two numbers;
cin>>x>>y;
}
public:
void disp()
{
z=x+y;
cout<<sum is <<z;
}
};
void main()
{
A obj;
obj.input();
obj.disp();
getch();
}

O/P: Error in input Function Access.

#include<iostream.h>
#include<conio.h>
class A
{
int x,y,z;
void input()
{
cout<<Enter two numbers;
cin>>x>>y;
}
public:
void disp()
{
input();
z=x+y;
cout<<sum is <<z;
}
};
void main()
{
A obj;
obj.disp();
getch();
}

O/P: Program will be execute.

Class member function definition: - we can


define class member function in two places.
inside the boundary of class
outside boundary of class

Note: - we will declared class function inside


the boundary of class but we can define them
in inside the boundary of class or outside
boundary of class,

Inside the boundary of class:-

#include<iostream.h>
#include<conio.h>
class A
{
char n[20];
char mn[20],
char fn[20];
int m,r;
public:
void input()
{
cout<<Enter your name;
gets(n);
cout<<Enter your fathers name;
gets(fn);
cout<<Enter your mothers name;
gets(mn);

cout<<Enter your marks;


cin>>m;
cout<<Enter your Roll Numbers;
cin>>r;
}
void disp()
{
cout<<\nDetails are\n;
cout<<Name is <<n;
cout<<\nFathers name is <<fn;
cout<<Mothers name is <<mn;
cout<<Roll Number is <<r;
cout<<Marks is <<m;
}
};
void main()
{
A obj;
obj.input();
obj.disp();
getch();
}

Outside boundary of class:-


Syntax for define function outside boundary of
class
Returntype className::functionName()
{
//stat;
}
:: =scope resolution operater.

#include<iostream.h>
#include<conio.h>
class A
{
char n[20];
char mn[20],
char fn[20];
int marks , roll;
public:
void input();
void disp();
};

void A::input()
{
cout<<Enter your name;
gets(n);
cout<<Enter your fathers name;
gets(fn);
cout<<Enter your mothers name;
gets(mn);
cout<<Enter your marks;
cin>>marks;
cout<<Enter your Roll Numbers;
cin>>roll;
}
void A::disp()
{
cout<<\nDetails are\n;
cout<<Name is <<n;
cout<<\nFathers name is <<fn;
cout<<Mothers name is <<mn;
cout<<Roll Number is <<r;
cout<<Marks is <<m;
}

void main()
{
A obj;
obj.input();
obj.disp();
getch();
}

Creating More than one object :-


We can also make more than one object of
class type.
Every object take separate memory location
WAP that accept and display 2 student
information
#include<iostream.h>
#include<conio.h>
class A
{
char n[20];
char mn[20],
char fn[20];
int m,r;
public:
void input();
void disp();
};

void A::input()
{
cout<<Enter your name;
gets(n);
cout<<Enter your fathers name;
gets(fn);
cout<<Enter your mothers name;
gets(mn);
cout<<Enter your marks;
cin>>m;
cout<<Enter your Roll Numbers;
cin>>r;
}
void A::disp()
{
cout<<\nDetails are\n;
cout<<Name is <<n;
cout<<\nFathers name is <<fn;
cout<<Mothers name is <<mn;
cout<<Roll Number is <<r;
cout<<Marks is <<m;
}

void main()
{
A obj,k;
obj.input();
k.input();
obj.disp();
k.disp();
getch();
}

Array Objects:- we can also create array object


of class type
Every array object take separate memory
location.
WAP for array of class.
#include<iostream.h>
#include<conio.h>
class A
{
char n[20];
char mn[20],
char fn[20];
int m,r;
public:
void input();
vois disp();
};

void A::input()
{
cout<<Enter your name;
gets(n);
cout<<Enter your fathers name;
gets(fn);
cout<<Enter your mothers name;
gets(mn);
cout<<Enter your marks;
cin>>m;
cout<<Enter your Roll Numbers;
cin>>r;
}
void A::disp()
{
cout<<\nDetails are\n;
cout<<Name is <<n;
cout<<\nFathers name is <<fn;
cout<<Mothers name is <<mn;
cout<<Roll Number is <<r;
cout<<Marks is <<m;
}

void main()
{
A obj[10];
Int I;
For(i=0;i<10;i++)
{
Obj[i].input();
}
for(i=0;i<10;i++)
{
obj[i].disp();
}
getch();
}

Parameterized Method:- A class can have


parameterized method. A function can take
parameter. A method who take parameter is
called parameterized method. A method can
take any type of parameter like int, char, float,
long, double, array, class object as a argument.

Example :-

#include<iostream.h>
#include<conio.h>
class A
{
int x,y,z;
public:
void input(int a, int b )
{
x=a;
y=b;
}
void disp()
{
z=x+y;
cout<<sum is <<z;
}
};
void main()
{
A obj ;
obj.input(12,89);
obj.disp();
getch();
}

Example 2:- ( Value taken by user in main


function)

#include<stdio.h>
#include<conio.h>
class A
{
int x,y,z;
public:
void input(int a, int b )
{
x=a;
y=b;
}
void disp()
{
z=x+y;
cout<<sum is <<z;
}
};
void main()
{
int m,n;
cout<<Enter two number;
cin>>m>>n;
A obj ;
obj.input(m,n);
obj.disp();
getch();
}

Value Returning Function:


When Function Execution is complete a
function can return a value to the caller .A
function can return any type of value.
Like int , char, float ,long ,double etc.

Return Keyword is used for return any value.

Class A
{
Int x,y;
Public:
Void input(int a,int b)
{
X=a;
Y=b;
}
Int disp()
{
Return ( x + y ) ;
}
};

Void main()
{
Int r, m , n;
Cout<<enter the no. m and n;
Cin>>m>>n;
A obj;
Obj. input(); //ERROR
Obj. input (int m , int n);
R=obj.disp();
Cout<<sum=<<r;
}

WHATS THE OUTPUT:

1.
Class A
{
Int x,y,z;
Public:
Void input(int a,int b)
{
X=a;
Y=b;
}
Void disp()
{
Z=a+b;
Cout<<sum=<<z;
}
};
O/P: ERROR

2.
Class A
{
Int x,y,z;
Public:
Void input(int a,int b)
{
X=a;
Y=b;
}
Void disp()
{
Z=x+y;
Cout<<sum=<<z;
}
};

3. this can be used a program for this


operator.
Class A
{
Int x,y,z;
Public:
Void input(int x,int y)
{
x=x;
y=y;
}
Void disp()
{
Z=x+y;
Cout<<sum=<<z;
}
};

Void main()
{
A obj;
Obj.input(30.40);
Obj.disp();
}

O/P:
Sum=-12[Garbage value]

4.
Class A
{
Int x,y,z;
Public:
Void input(int x,int y)
{
This->x=x;
This->y=y;
}
Void disp()
{
Z=x+y;
Cout<<sum=<<z;
}
};

Void main()
{
A obj;
Obj.input(30,40);
Obj.disp();
}

O/P:
Sum=70
Constructor and Destructor

class A
{
int x=9; // error
int y=10; // error
public :
void disp()
{
cout<<x<<y;
}
};

Note: - we cannot initialize class data


members at the time of declaration. If we want
to initial class member we have to make a
function and we can give value inside the
function like this inside the class in a member
function using a constructor.

class A
{
int x;
int y;
public :
void disp()
{
x=9;
y=90;
cout<<x<<y;
}
};
void main()
{
A obj
obj.disp();
getch();
}

Constructor :- A constructor is a special


member function of the class it is
automatically called whenever object of that
class is created. Constructor has same name as
class name in which constructor has declared.
Constructor is used for initial class member. If
any class has no constructor the compiler
automatically create default constructor in
class.
Some points about constructor:-
1.) It has same name as class name in which it
is declared.
2.) constructor cannot be called.
It is automatically called whenever object of
that class is created.
3.) It is used for initial class member.
4.) Constructor has no return type not even
void.
5.) Constructor cannot return any value.
6.) Constructor cannot be inherited.
7.) A class can have more than one constructor
but signature or parameters are different
Constructor always declared under public
section.

Class A
{
Int x,y;
Public:
A()
{
X=10;
Y=20;
}
};
A obj; //constructor will be callled

Constructor are two types


1.)default constructor
2.)Parameterized Constructor

Default constructor:- A constructor with no


parameter is called default constructor. If your
class has no constructor then compiler will
create default constructor in the class.

class A
{
int x;
int y;
public :
A()
{
x=9;
y=90;
cout<<x<<y;
}
};
void main()
{
A obj
getch();
}

class A
{
int x;
int y;
private :
A()
{
x=9;
y=90;
cout<<x<<y;
}
};
void main()
{
A obj; //error
getch();
}

O/P
compilation error we cannot create object of
above class because constructor declared
under private section

Parameterized Constructor:-

A Constructor with parameter is called


parameterized constructor. We must pass value
for parameterized constructor at the time of
object creation.

class A
{
int x;
int y,z;
public :
A(int a, int b)
{
x=a;
y=b;
}
void disp()
{
z=x+y;
cout<<Sum is <<z;
}
};
void main()
{
A obj; //error
A obj(90,80); //correct
getch();
}

Example 2:

class A
{
int x;
int y,z;
public :
A(int a, int b)
{
x=a;
y=b;
}
void disp()
{
z=x+y;
cout<<Sum is <<z;
}
};
void main()
{
int m,n;
cout<<enter two number;
cin>>m>>n;
A obj(m,n);
Obj.disp();
getch();
}

Whats the output:-

class A
{
int x;
int y,z;
public :
A(int a, int b)
{
x=a;
y=b;
}
void disp()
{
z=x+y;
cout<<Sum is <<z;
}
};
void main()
{
A obj; //error
A obj2(90,80); // correct
A k(4,7); // correct
A h(8,56) ; //correct
Obj2.disp();
k.disp();
h.disp();
getch();
}

A obj:- this line produce error because class


has parameterized constructor it is mandatory
for us pass value at the time of object creation.

Default Argument Constructor

class A
{
public:
A(int a=5)
{
}
};
void main()
{
A obj;
A kp(90);
getch();
}

Example 2(whats the output)


class A
{
int x;
int y,z;
public :
A()
{

}
A(int a, int b)
{
x=a;
y=b;
}
void disp()
{
z=x+y;
cout<<Sum is <<z;
}
};
void main()
{
A obj;
A obj2(90,80);
A k(4,7);
A h(8,56) ;
getch();
}

Program will be executed.

Constructor call:- A constructor can be call in


two ways
implict call
A ob(20,80);

explicit call
A ob = A(20,80);

Constrcutor overloading:
A class can have more than one constructor
but signature are different. Signature means
type of parameter or number of parameter are
different.
#include<iostream.h>
#include<conio.h>
class A
{
int x,y,z;
Public:
A()
{
x=90;
y=100;
}
A(int a)
{
x=a;
y=2;
}
A(int a, int b)
{
x=a;
y=b;
}
void disp()
{
z=x+y;
cout<<Sum is<<z;
}
};
Void main()
{
A K1;
A K2(30,60);
A K3(1);
K1.disp();
K2.disp();
K3.disp();
getch();
}

Function overloading:

#include<stdio.h>
#include<conio.h>
class A
{
int x,y,z;
public:
void input( )
{
x=90;
y=100;
z=x+y;
cout<<Sum is<<z;

}
void input(int a)
{
x=a;
y=2;
z=x+y;
cout<<Sum is<<z;

}
void input(float a)
{
x=a;
y=90;
z=x+y;
cout<<Sum is<<z;

}
};
Void main()
{
A K1;
K1.input();
K1.input(20.7);
K1.input(19);
getch();
}
Copy Constructor:- It is used for initial one
object member from another object member
with same value. Copy constructor present
in(classname &objectName)

class Sample
{
int x,y;
public:
Sample(int a, int b)
{
x=a;
y=b;
}
Sample(Sample &ob)
{
x=ob.x;
y=ob.y;
}
void show()
{
cout<<x<<\t<<y;
}
};
void main()
{
Sample s1(10,20);
Sample s2(s1);
s1.show();
s2.show();
getch();
}

Destructor: - it is used for deallocate memory


that is provided by constructor. Destructor has
same name as class name but precede by tilde
(~) sign.

Points about destructor:


1) A class can have only one destructor
2) destructor can not take any parameter
3) It is called whenever object goes out of
scope.
4) Destructor always declared under public
section.

class A
{
int x;
int y,z;
public :
A(int a, int b)
{
x=a;
y=b;
}
~A() //destructor
{
}
void disp()
{
z=x+y;
cout<<Sum is <<z;
}
};
void main()
{
int m,n;
cout<<enter two number;
cin>>m>>n;
A obj(m,n);
getch();
}
To define constructor outside boundary of
class

class A
{
int x,y;
public:
A();
void show();
};

A::A()
{
x=9;
y=8;
}
void A::show()
{
int z;
z=x+y;
cout<<Sum is <<z;
}

Function overloading:
A class can have more than one function of
same name but signature are different.
Signature means type of parameter or number
of parameter are different.
#include<iostream.h>
#include<conio.h>
class A
{
int x,y,z;
public:
void input( )
{
x=90;
y=100;
z=x+y;
cout<<Sum is<<z;

}
void input(int a)
{
x=a;
y=2;
z=x+y;
cout<<Sum is<<z;

}
void input(int a, int b)
{
x=a;
y=b;
z=x+y;
cout<<Sum is<<z;

}
void input(float a)
{
}
};
Void main()
{
A K1;
K1.input();
K1.input(20,40);
K1.input(1);
getch();
}
#include<iostream.h>
#include<conio.h>
class A
{
int x,y,z;
public:
void input()
{
x=90;
y=100;
z=x+y;
cout<<Sum is<<z;

}
void input()
{
x=9;
y=2;
z=x+y;
cout<<Sum is<<z;
}
void input()
{
x=a;
y=b;
z=x+y;
cout<<Sum is<<z;

}
};
Void main()
{
A K1;
K1.input();
K1.input();
K1.input();
getch();
}
Error

#include<iostream.h>
#include<conio.h>
class A
{
int x,y,z;
public:
void input()
{
x=90;
y=100;
z=x+y;
cout<<Sum is<<z;

}
int input()
{
x=9;
y=2;
z=x+y;
return z;

}
};
Void main()
{
int r;
A K1;
K1.input();
r=K1.input();
getch();
}

Static Member:-

Whats The O/p


class A
{
int x;
public:
A()
{
x=0;
}
void show()
{
cout<<x;
x++;
}
};
void main()
{
A a1,a2,a3,a4;
a1.show();
a2.show();
a3.show();
a4.show();
a1.show();
a1.show();
a2.show();
}

o/p
0
0
0
0
1
2
1

2)
class A
{
static int x;
public:
A()
{
x=0;
}
void show()
{
cout<<x;
x++;
}
};
void main()
{
A a1,a2,a3,a4;
a1.show();
a2.show();
a3.show();
a4.show();
a1.show();
a1.show();
a2.show();
}
o/p
0
1
2
3
4
5
6

Static Member :- A class can contain static


member means static variable and static
function.
static member variable :- There is only one
copy of static member present in the memory.
All objects share that copy.
Note:-
1) Static variable must declared inside the
class.
2) Static variable must define outside the
boundary of class.
3) Static variable automatically initial with
zero.

Static Function :- We can also create static


function by using static keyword.
Note
1) Static function can access only static
member of the class
2) Static function call by using
className::functionname()
class A
{
public :
static void input()
{
cout<<Hello;
}
};
void main()
{
A::input();
}

INHERITANCE:

We can extend functionality of an existing


class by creating a new class that derives from
the existing class. The derived class inherits
the properties of the base class , and you can
add or override methods and properties are
required.

There are two main classes in inheritance.


1. Super class or base class or parent
class.
2. Derived class or sub class or child
class.

Base class:
An existing class which property used by
derived or sub class .it is also known as parent
or super class.

Derived Class:
A newly defined class that use the property of
an existing class .
Inheritance present in many forms.
1. Single inheritance.
2. Multiple inheritance.
3. Hierarchal inheritance.
4. Multilevel inheritance.
5. Hybrid inheritance.

Single inheritance:
When a class inherits from only one base class
it is called single inheritance. 1 base class and
1 derived class
Multiple inheritance:
When a class inherits from more than one base
class it is called multiple inheritance.more
than 1 base class and 1 derived class.

Hierarchal inheritance:
When more than one class inherits from same
base it is called hierarchal inheritance.

Multilevel inheritance:
When a class inherits from a class who also
inherits from another class it is called
multilevel inheritance.

Hybrid inheritance:
It is a collection of two or more than
inheritance.

Single Inheritance:

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class person
{
char n[20];
char fn[20];
char city[30];
public:
void readdata()
{
cout<<Enter your name;
gets(n);
cout<<Enter your fathers name;
gets(fn);
cout<<Enter your city;
gets(city);
}
void dispdata()
{
cout<<\nDetails are\n;
cout<<Name is <<n;
cout<<\nFathers name is <<fn;
cout<<city is <<city;
}
};
Class employee: public person
{
Int id ;
Float sal;
Public:
Void input()
{
Cout<<enter id;
Cin>>id;
Cout<<enter salary;
Cin>>sal;
}
Void disp()
{
Cout<<id=<<id;
Cout<<\n salary=<<sal;
}
};
void main()
{
Employee obj;
Obj.readdata();
Obj.input();
Obj.dispdata();
Obj.disp();
Getch();
}

getch();
}

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class person
{
char n[20];
char fn[20];
char city[30];
public:
void readdata()
{
cout<<Enter your name;
gets(n);
cout<<Enter your fathers name;
gets(fn);
cout<<Enter your city;
gets(city);
}
void dispdata()
{
cout<<\nDetails are\n;
cout<<Name is <<n;
cout<<\nFathers name is <<fn;
cout<<city is <<city;
}
};
Class employee: public person
{
Int id ;
Float sal;
Public:
Void input()
{
Readdata()
Cout<<enter id;
Cin>>id;
Cout<<enter salary;
Cin>>sal;
}
Void disp()
{
Dispdata();
Cout<<id=<<id;
Cout<<\n salary=<<sa;l
}
};
void main()
{
Employee obj;
Obj.input;
Obj.disp();
Getch();
}

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class person
{
char n[20];
char fn[20];
char city[30];
public:
void readdata()
{
cout<<Enter your name;
gets(n);
cout<<Enter your fathers name;
gets(fn);
cout<<Enter your city;
gets(city);
}
void dispdata()
{
cout<<\nDetails are\n;
cout<<Name is <<n;
cout<<\nFathers name is <<fn;
cout<<city is <<city;
}
};
Class employee: public person
{
Int id ;
Float sal;
Public:
Void input()
{
Cout<<enter id;
Cin>>id;
Cout<<enter salary;
Cin>>sal;
}
Void disp()
{
Cout<<id=<<id;
Cout<<\n salary=<<sa;l
}
};
void main()
{
Employee obj;
Obj.readdata();
Obj.input;
Obj.dispdata();
Obj.disp();
Getch();
}

Multiple Inheritance:
A class can inherits more than one class.
Syntax:
Derived class name: visibility mode base class
name, visibility mode base class name.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class person
{
char n[20];
char fn[20];
char city[30];
public:
void input()
{
cout<<Enter your name;
gets(n);
cout<<Enter your fathers name;
gets(fn);
cout<<Enter your city;
gets(city);
}
void disp()
{
cout<<\nDetails are\n;
cout<<Name is <<n;
cout<<\nFathers name is <<fn;
cout<<city is <<city;
}
};
Class student
{
Int r,m;
Int reg;
Public:
Void getdata()
{
Cout<<enter roll no.;
Cin>>r;
Cout<<enter marks;
Cin>>m;
Cout<<enter reg. no.;
Cin>>reg;
}
Void dispdata()
{
Cout<<\n your reg. no. is<<reg;
Cout<<\n your id is <<r;
Cout<<your marks is<<m;
}
};
Class graduate: public person, public student
{
Char st[20];
Public:
Void take()
{
Input();
Getdata();
Cout<<enter your stream;
Gets(st);
}
Void show()
{
Disp();
Dispdata();
Cou<<enter your stream=<<st;
}
};

void main()
{
graduate obj;
Obj.take();
Obj.show();
Getch();
}

Multilevel inheritance:

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class person
{
char n[20];
char fn[20];
char city[30];
public:
void input()
{
cout<<Enter your name;
gets(n);
cout<<Enter your fathers name;
gets(fn);
cout<<Enter your city;
gets(city);
}
void disp()
{
cout<<\nDetails are\n;
cout<<Name is <<n;
cout<<\nFathers name is <<fn;
cout<<city is <<city;
}
};
Class student : public person
{
Int r,m;
Int reg;
Public:
Void getdata()
{
Cout<<enter roll no.;
Cin>>r;
Cout<<enter marks;
Cin>>m;
Cout<<enter reg. no.;
Cin>>reg;
}
Void dispdata()
{
Cout<<\n your reg. no. is<<reg;
Cout<<\n your id is <<r;
Cout<<your marks is<<m;
}
};
Class graduate: public student
{
Char st[20];
Void take()
{
Getdata();
Cout<<enter your stream;
Gets(st);
}
Void show()
{
Dispdata();
Cou<<enter your stream=<<st;
}
};

void main()
{
graduate obj;
Obj.take();
Obj.show();
Getch();
}

Access Modifier in inheritance:

Public Derivation:

Access Modifier used in derive Access by


object
of Derived
class

Public yes yes


Private no no
Protected yes no

Private derivation:

Access Modifier used in derive Access by


object
of Derived
class

Public yes no
Private no no
Protected yes no

Protected derivation:
Access Modifier used in derive Access by
object
of Derived
class

Public yes no
Private no no
Protected yes no

Class A
{
Int x,y;
Public:
Int a,b;
Protected:
Int m,n;
};
Class B; public A
{
Public:
Void disp()
X=5; //ERROR
A=10;
M=15;
}
};

Void main()
{
B obj;
Obj.b=13;
Obj.y=15; //ERROR
Obj.n=20; // ERROR
}

Constructor in inheritance:
1.) The base class constructor is constructed
before the derived class constructor
means when we create instance (object)
of derived class the base class
constructor will be called.
2.) If base class has default constructor than
it is not mandatory to derived class to
have constructor but when base class has
parameterized constructor than it is
mandatory to derived class to have
constructor because derived class
constructor pass value for base class
constructor.

Class A
{
Public:
A()
{
Cout<<const of A;
}
Void disp()
{
Cout<<hello;
}
};
Class B:public A
{
Public:
B()
{
Cout<<const of B;
}
};
Void main()
{
B Obj;
}

O/P: Const of A
Const of B

Class A
{
Public:
Void disp()
{
Cout<<hello;
}
};
Class B:Public A
{
Public :
B()
{
Cout<<const of B;
}
};
Void main()
{
B obj;
}

O/P: const of B

Class A
{
Public:
A()
{
Cout<<const of A;
}
Void disp()
{
Cout<<hello;
}
};
Class B:Public A
{
Public :
Void show()
{
Cout<<hello friends;
}
};
Void main()
{
B obj;
}

O/P: const of A

Class A
{
Public:
A(int x)
{
Cout<<the value of x=<<x;
}
};
Class B:Public A
{
Public :
Void show()
{
Cout<<hello friends;
}
};
Void main()
{
B obj; //B obj(25);
}

O/P: ERROR

Class A
{
Public:
A(int x)
{
}
};
Class B:Public A
{
Public :
B():A(25)
{
}
};
Void main()
{
B obj;
}

Class A
{
Public:
A(int x)
{
}
};
Class B:Public A
{
Public :
B(int a,int b):A(a)
{
}
};
Void main()
{
B obj(12,25);
}
Ambiguity in multiple inheritance:

Class A
{
Public:
Int x;
};
Class B:public A
{
Public:
Int y;
};
Class C:public A
{
Public:
Int z;
};
Class D:public B,public C
{
Public:
Void disp()
{
X=90;.//error
Cout<<value=<<x;.//error
}
};
Void main()
{
D obj;
Obj.disp();
}

B class has x and y as a member and C class


has X and Z as a member when we inherit B
and C class in D then D class has two copy of
x in memory one of B and another by C

So,
X=90 will produce error we can correct them
by using:

By using scope resolution operator

1.)
Class A
{
Public:
Int x;
};
Class B:public A
{
Public :
Int y;
};
Class C :public A
{
Public:
Int z;
};
Class D:public, C,B
{
Public:
Void disp()
{
B::x=90;
Cout<<value=<<B::x;
}
};
Void main()
{
D obj;
Obj.disp();
}

2.)
By using virtual Base class:

Class A
{
Public:
Int x;
};
Class B:public virtual A
{
Public:
Int y;
};
Class C:public virtual A
{
Public:
Int z;
};
Class D:public B,public C
{
Public:
Void disp()
{
X=90;
Cout<<value=<<x;
}
};
Void main()
{
D obj;
Obj.disp();
}

Pointer object of class:

We can also create pointer object of a class


when we create pointer object of class we can
access class member with the help of(->) sign
in place of (.) operator.

Class A
{
Int x,y;
Public:
Void input()
{
Cout<<enter two no.;
Cin>>x>>y;
}
Void disp()
{
Z=x+y;
}
};
Void main()
{
A *obj;
Obj=new A;
Obj->input();
Obj->disp();
Delete obj;
Getch();
}

NEW: it is used for dynamically allocated


memory.

DELETE: it is used for deallocate memory.


POLYMORPHISM:
There are two types of polymorphism:

1) Compile time or static polymorphism or


early binding.
EX: 1) function overloading
2) Operator overloading

3) Runtime polymorphism or dynamic


polymorphism or late Binding.

EX: Method overriding.

Binding: To attach an object with function


known as binding .
There are two types of Binding:

1.) Early binding: in early binding an object


attach with method at compile time.
2.) Late Binding: in late binding an object
attach with function at run time.
Syntax for new command:
Class_name=*object;
Object=new class name;
Now as theobject is pointer type we use ->
Class A
{
Public:
Void disp()
{
Cout<<Hello class A;
}
};
Class B:public A
{
Public:
Void display()
{
Disp();
Cout<<hello class B;
}
};
Void main()
{
A *obj;
Obj=new A;
Obj->disp();
Obj=new B;
Obj->disp();
Getch();
}

O/P: class A
Class A

Class A
{
Public:
Virtual Void disp()=0;
};
Class B:public A
{
Public:
Void disp()
{
Cout<<hello class B;
}
};
Class C:public A
{
Public:
Void disp()
{
Cout<<hello class C;
}
};

Void main()
{
A *obj;
Obj=new B;
Obj->disp();
Obj=new C;
Obj->disp();
Getch();
}

O/P: Hello class B


Hello Class C

Pure virtual function:-


A function with no body is called pure virtual
function.

Syntax to declare virtual function:


Virtual returntype function name()=0;
Base class pointer object can access derived
class function but must defined with virtual
keyword in base class.

Class A
{
Public:
Void disp()
{
Cout<<Hello class A;
}
};
Class B:public A
{
Public:
Void disp()
{
Cout<<hello class B;
}
};
Void main()
{
B obj;
Obj.disp();
Obj.disp();
Getch();
}

O/P
Hello Class A
Hello class B

Class A
{
Public:
Void disp()
{
Cout<<Hello class A;
}
};
Class B:public A
{
Public:
Void disp()
{
Disp();
Cout<<hello class B;
}
};
Void main()
{
B obj;
Obj.disp();
Getch();
}

O/P: Run time error


Stack overflow

Class A
{
Public:
Void disp()
{
Cout<<Hello class A;
}
};
Class B:public A
{
Public:
Void disp()
{
A::disp()
Cout<<hello class B;
}
};
Void main()
{
B obj;
Obj.disp();
Obj.disp();
Getch();
}

O/P: hello class A


Hello class B

Interfaces in C++ (Abstract Classes)


An interface describes the behavior or
capabilities of a C++ class without committing
to a particular implementation of that class.
The C++ interfaces are implemented using
abstract classes and these abstract classes
should not be confused with data abstraction
which is a concept of keeping implementation
details separate from associated data.
A class is made abstract by declaring at least
one of its functions as pure virtual function.
A pure virtual function is specified by placing
"= 0" in its declaration as follows:
class Box
{
public:
virtual double getVolume() = 0;
private:
double length;
double breadth;
double height;
};
The purpose of an abstract class (often
referred to as an ABC) is to provide an
appropriate base class from which other
classes can inherit. Abstract classes cannot be
used to instantiate objects and serves only as
an interface. Attempting to instantiate an
object of an abstract class causes a
compilation error.
Thus, if a subclass of an ABC needs to be
instantiated, it has to implement each of the
virtual functions, which means that it supports
the interface declared by the ABC. Failure to
override a pure virtual function in a derived
class, then attempting to instantiate objects of
that class, is a compilation error.
Classes that can be used to instantiate objects
are called concrete classes.
Abstract Class Example:
Consider the following example where parent
class provides an interface to the base class to
implement a function called getArea():
#include <iostream>

class Shape
{
public:
virtual int getArea() = 0;
void setWidth(int w)
{
width = w;
}
void setHeight(int h)
{
height = h;
}
protected:
int width;
int height;
};
// Derived classes
class Rectangle: public Shape
{
public:
int getArea()
{
return (width * height);
}
};
class Triangle: public Shape
{
public:
int getArea()
{
return (width * height)/2;
}
};
int main(void)
{
Rectangle Rect;
Triangle Tri;
Rect.setWidth(5);
Rect.setHeight(7);
// Print the area of the object.
cout << "Total Rectangle area: " <<
Rect.getArea() << endl;

Tri.setWidth(5);
Tri.setHeight(7);
// Print the area of the object.
cout << "Total Triangle area: " <<
Tri.getArea() << endl;

return 0;
}
When the above code is compiled and
executed, it produces the following result:
Total Rectangle area: 35
Total Triangle area: 17
You can see how an abstract class defined an
interface in terms of getArea() and two other
classes implemented same function but with
different algorithm to calculate the area
specific to the shape.

Operators overloading in C++:


Every operator has predefined meaning, they
work on primitive data type like + operator
add two no. to change the functionality of an
operator so operator can work on user defined
data type is known as operator overloading.
You can redefine or overload most of the built-
in operators available in C++. Thus a
programmer can use operators with user-
defined types as well.

Overloadable/Non-overloadableOperators:
Following is the list of operators which can be
overloaded:

+ - * / % ^
& | ~ ! , =
< > <= >= ++ --
<< >> == != && ||
+= -= /= %= ^= &=
|= *= <<= >>= [] ()
delete
-> ->* new new [] delete
[]
Following is the list of operators, which can
not be overloaded:

:: .* . ?:

Overloaded operators are functions with


special names the keyword operator followed
by the symbol for the operator being defined.
Like any other function, an overloaded
operator has a return type and a parameter list.
Box operator+(const Box&);
declares the addition operator that can be used
to add two Box objects and returns final Box
object. Most overloaded operators may be
defined as ordinary non-member functions or
as class member functions. In case we define
above function as non-member function of a
class then we would have to pass two
arguments for each operand as follows:
Box operator+(const Box&, const Box&);
Following is the example to show the concept
of operator over loading using a member
function. Here an object is passed as an
argument whose properties will be accessed
using this object, the object which will call
this operator can be accessed using this
operator as explained below:
#include <iostream>

class Box
{
public:

double getVolume(void)
{
return length * breadth * height;
}
void setLength( double len )
{
length = len;
}

void setBreadth( double bre )


{
breadth = bre;
}

void setHeight( double hei )


{
height = hei;
}
Box operator+(const Box& b)
{
Box box;
box.length = this->length + b.length;
box.breadth = this->breadth + b.breadth;
box.height = this->height + b.height;
return box;
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
int main( )
{
Box Box1; // Declare Box1 of type
Box
Box Box2; // Declare Box2 of type
Box
Box Box3; // Declare Box3 of type
Box
double volume = 0.0; // Store the volume
of a box here
// box 1 specification
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
// box 2 specification
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
// volume of box 1
volume = Box1.getVolume();
cout << "Volume of Box1 : " << volume
<<endl;
// volume of box 2
volume = Box2.getVolume();
cout << "Volume of Box2 : " << volume
<<endl;

// Add two object as follows:


Box3 = Box1 + Box2;

// volume of box 3
volume = Box3.getVolume();
cout << "Volume of Box3 : " << volume
<<endl;

return 0;
}
When the above code is compiled and
executed, it produces the following result:
Volume of Box1 : 210
Volume of Box2 : 1560
Volume of Box3 : 5400

Template

Templates are the foundation of generic


programming, which involves writing code in
a way that is independent of any particular
type.
A template is a blueprint or formula for
creating a generic class or a function. The
library containers like iterators and algorithms
are examples of generic programming and
have been developed using template concept.
There is a single definition of each container,
such as vector, but we can define many
different kinds of vectors for example, vector
<int> or vector <string>.
You can use templates to define functions as
well as classes, let us see how do they work:
Function Template:
The general form of a template function
definition is shown here:
template <class type> ret-type func-
name(parameter list)
{
// body of function
}
Here, type is a placeholder name for a data
type used by the function. This name can be
used within the function definition.
The following is the example of a function
template that returns the maximum of two
values:

Void add (int a,int b)


{
Int c;
C=a+b;
Cout<<sum=<<c;
}
#include <iostream.h>
#include <string.h>
template <class T>
void add(T a, T b)
{
T c;
c=a+b;
cout<<Sum = <<c;
}
void main ()
{
add(10,20);
add(10.6,10.7);
}
If we compile and run above code, this would
produce the following result:
Class Template:
Just as we can define function templates, we
can also define class templates. The general
form of a generic class declaration is shown
here:
template <class type> class class-name {
.
.
.
}
Here, type is the placeholder type name,
which will be specified when a class is
instantiated. You can define more than one
generic data type by using a comma-separated
list.

Constant member:
we can declare constant variable, constant
function and constant object. A const object
can call only constant member.

A class can also contain const member.


we can not modify the value of const ember .
#include<iostream.h>
#include<conio.h>
class A
{
public:
void disp(const int a)
{
cout<<"value of a ="<<a;
}
};
void main()
{
cout<<"hello ";
A obj;
obj.disp(10);
getch();
}
Program wil be executed

2)
#include<iostream.h>
#include<conio.h>
class A
{
public:
void disp(const int a)
{
a=a+2;
cout<<"value of a ="<<a;
}
};
void main()
{
cout<<"hello ";
A obj;
obj.disp(10);
getch();
}

Friend Function:-
A friend function is a friend of a class. it can
access private member of a class with the help
of object.

#include<iostream.h>
#include<conio.h>
class A
{
int x,y;
public:
void disp()
{
cout<<"value of a =";
}
friend void abc(); // friend function
};

Void A:: abc()


{
A obj;
obj.x=90;
obj.y=9;
cout<<obj.x;
cout<<obj.y;
}
void main()
{
cout<<"hello ";
A obj;
obj.disp();
abc();
getch();
}
1) A friend function is a friend of the class
it can declared in any block public,
private or protected.
2) it is not a member of a class
3) it can access private member of a class
with the help of object.
4) it call only by function name
5) it body defined outside the class.

Type conversion:-
There are two types of type conversion
1) Implicit Type Conversion:- It is done by
compiler
2) Explicit Type Conversion:- It is done by
programmer. It is also called type
casting.

implicit type conversion


int a=19;
long b=a;

Explicit Type Conversion


long a=90;
int b=(int)a;
Proxy Class:-
A proxy class is a stand-in for another class.

Let's suppose you have a class that has a


method that takes 60 seconds to complete.
That means everytime you call that method,
your program waits. But let's also assume you
rarely call that method. Let's further assume
this method is named Load() and the class is
MyClass
Expand|Select|Wrap|Line Numbers
class MyClass
{
public:
void Load(); //takes a long time
void AMethod();
etc... //the other methods.
};.

The proxy class would look like:


[code=cpp]
class MyClassProxy
{
MyClass* theObject;
public:
MyClassProxy(); : theObject(0) {}
MyClass* operator->();
MyClass& operator*();
};

So when you create a MyClassProxy object,


the MyClass* inside is set to zero.

Now you use MyClassProxy objects instead of


MyClass objects.

If someone needs the MyClass object, they use


the operator-> overload of MyClassProxy.
This function just returns the MyClass* if the
MyClass object exists otherwise is creates it
and calls Load().
MyClass* MyClassProxy::operator->()
{
if (!this->theObject)
{
theObject = new MyClass;
theObject->Load();
}
return theObject;
};

So not until you use the proxy object with the


-> operator do you see the 60 seccond delay.

MyClassProxy p; //no delay


p->AMethod(); //Here the MyClass o
bject is created
//Loaded and the MyCl
ass::AMethod called.
Exception Handing in C++
An exception is a problem that arises during
the execution of a program. A C++ exception
is a response to an exceptional circumstance
that arises while a program is running, such as
an attempt to divide by zero.
Exceptions provide a way to transfer control
from one part of a program to another. C++
exception handling is built upon three
keywords: try, catch, and throw.
throw: A program throws an exception
when a problem shows up. This is done
using a throw keyword.
catch: A program catches an exception
with an exception handler at the place in a
program where you want to handle the
problem. The catch keyword indicates the
catching of an exception.
try: A try block identifies a block of code
for which particular exceptions will be
activated. It's followed by one or more
catch blocks.
Assuming a block will raise an exception, a
method catches an exception using a
combination of the try and catch keywords. A
try/catch block is placed around the code that
might generate an exception. Code within a
try/catch block is referred to as protected code,
and the syntax for using try/catch looks like
the following:
try
{
// protected code
}catch( ExceptionName e1 )
{
// catch block
}catch( ExceptionName e2 )
{
// catch block
}catch( ExceptionName eN )
{
// catch block
}
You can list down multiple catch statements
to catch different type of exceptions in case
your try block raises more than one exception
in different situations.
Throwing Exceptions:
Exceptions can be thrown anywhere within a
code block using throw statements. The
operand of the throw statements determines a
type for the exception and can be any
expression and the type of the result of the
expression determines the type of exception
thrown.
Following is an example of throwing an
exception when dividing by zero condition
occurs:
double division(int a, int b)
{
if( b == 0 )
{
throw "Division by zero condition!";
}
return (a/b);
}
Catching Exceptions:
The catch block following the try block
catches any exception. You can specify what
type of exception you want to catch and this is
determined by the exception declaration that
appears in parentheses following the keyword
catch.
try
{
// protected code
}catch( ExceptionName e )
{
// code to handle ExceptionName exception
}
Above code will catch an exception of
ExceptionName type. If you want to specify
that a catch block should handle any type of
exception that is thrown in a try block, you
must put an ellipsis, ..., between the
parentheses enclosing the exception
declaration as follows:
try
{
// protected code
}catch(...)
{
// code to handle any exception
}
The following is an example, which throws a
division by zero exception and we catch it in
catch block.
#include <iostream>
using namespace std;

double division(int a, int b)


{
if( b == 0 )
{
throw "Division by zero condition!";
}
return (a/b);
}

int main ()
{
int x = 50;
int y = 0;
double z = 0;
try {
z = division(x, y);
cout << z << endl;
}catch (const char* msg) {
cerr << msg << endl;
}

return 0;
}
Because we are raising an exception of type
const char*, so while catching this exception,
we have to use const char* in catch block. If
we compile and run above code, this would
produce the following result:
Division by zero condition!

OPERATOR OVERLOADING:

Operator overloading is a mechanism, in C++,


which provides a special meaning to an
operator . it is one of the exciting features of
C++ language.
The input and output operator (<< and >>)
used in C++. For input or display , are good
examples of overloading. That is the left shift
(<<) and right shift (>>) operators which are
used to left and right shift the bits ,
respectively are overloaded to perform a
different function of input And output when
used with cin and cout.

NOTE: thus operator overloading provides the


ability to use the same operator to perform
different actions.
Operator overloading:-
Every operator have speial meaning but we can
change the functionality of an operator it is called
operator overloading. We can overlaod unary
operator as well as binary operator.

Unary Operator Overloading:-


class student
{
Int roll_numb;
Int age;
Public:
Student(int rn,int ag)
{
Roll_numb=rn;
Age=ag;
}
Void operator ++()
{
Age =age+1;
}
};
Void main()
{
Student ram(1200,19);
++ram;
Getch();
}

Program overloading unary operator ++ using


arguments.
class student
{
Int roll_numb;
Int age;
Public:
Student(int rn,int ag)
{
Roll_numb=rn;
Age=ag;
}
Void operator ++(int)
{
Age =age+1;
}
};
Void main()
{
Student ram(1200,19);
Ram++;
Getch();
}

Binary Operator overload :-


To overlaod binary operator is called binary
operator overloading

class student
{
Int roll_numb;
Int age;
Public:
Student(int rn,int ag)
{
Roll_numb=rn;
Age=ag;
}
Student operator +(student obj)
{
Student temp(0,0); //roll_numb and age initialized
to 0
Temp.age =age+obj.age;
Retturn temp;
}
};
Void main()
{
Student ram(1200,19),mohan(1201,20);
Student ramesh(0,0);
Ramesh=ram+mohan; // add the age of ram and
mohan and asssign the sum to the age of ramesh
Getch();
}

You might also like