Object Oriented Programming Concepts (OOPs)

Object Oriented Programming (OOP) is a programming model where programs are organized around objects and data rather than action and logic. OOP allows decomposition of a problem into a number of entities called objects and then builds data and functions around these objects.
Advantage of Object Oriented Programming
There are various advantage of using OOP over the procedural or parallel programming. Following are some of the basic advantages of using oop techniques.
1. Re-Usability of your code: If you will use OOP technique for creating your application then it will gives you a greater re-usability. For example, if you have created calculator class at one place then you can use the same calculator class in your application.
2. Easy to Maintain : Application develop using oop technique are easier to maintain than normal programming. Again let us take an example of your interest calculator class. Suppose your business need to change the calculation logic. They want to add some charges if your capital is less than 200 USD. Just think about your application is big and developed using normal programming techniques. So first you have to analyse that at how many places we have calculated interest, and then you will change. But just think of oop technique. You just need to change in your method of interest calculation at one place.
3. Good Level of Abstraction: Abstraction means making something hidden. By using oop technique you are abstracting your business logic from implementation. It will provide you greater ease. Again let us take and example of interest calculator. If you have created class for interest calculation and your team is going to use that class. Now you are only concern about how interest calculation will be performed, because you have created that. Your team member is always have understanding that if they will set rate and capital property and apply interest calculation method then it will return interest.
4. Molecularity: If we are creating separate class for your every problem then you are making it modular. So if someone need to change in the business logic part then he will always go to your business logic code part.

Class:- A class is the core of any modern Object Oriented Programming language such as C++. A class will not occupy any memory space and hence it is only a logical representation of data. Class is a collection of data members and member functions. Data members hold the data and member functions perform the operations on the data members. To create a class, you simply use the keyword "class" followed by the class name:
Class syntax:-
Class < class-name >
{
accessspecifier:
      data members;
accessspecifier:
      memberfunctions;
};
Access specifiers (or ) Visibility modes:-
Access specifiers are used to access the data members and member functions.
There are 3 types of access specifiers
1. private:- private means within the class.
2. protected:- Protected means within the class and outside the sub classes.
3. Public:- Public means any where it is accessible.
Object:-

Oject is a runtime entity. And it is a physical representation for the class. Object is having properties. Properties are nothing but data members and member functions.
How to create a object:-
Syntax:-
class-name object;
How to access the properties:-
object.datamembers;
object.memberfunctions;

//class with data members example
#include <iostream.h >
#include < conio.h >
class sample
{
int eno;
private:
char name[10];
protected:
float sal;
public:
long int cno;
};
void main()
{
clrscr();
sample s;
//s.eno=100;
//s.name="Rama";
//s.sal=12000;
s.cno=123123123;
cout<<"Employee Contact No = "<<s.cno;
getch();
}

//class with member functions example
#include <iostream.h>
#include <conio.h>
class sample
{
public:
int print()
{
return 10;
}
void read()
{
cout <<"This is Read()";
}
};
void main()
{
clrscr();
sample s;
cout <<"The number is"<<s.print();
s.read();
getch();
}

//class with data members and member functions example
#include <iostream.h>
#include <conio.h>
class sample
{
int eno;
char name[10];
float sal;
long int cno;
public:
void read()
{
cout<<"Enter Emp No,Emp Name,salary,Contact No:";
cin>>eno>>name>>sal>>cno;
}
void print()
{
cout<<"--------------------"<<endl;
cout<<"EMPLOYEE INFORMATION"<<endl;
cout<<"EMP NO = "<<eno<<endl;
cout<<"EMP NAME = "<<name<<endl;
cout<<"EMP SALARY = "<<sal<<endl;
cout<<"EMP CONTACT NO = "<<cno<<endl;
cout<<"--------------------"<<endl;
}
};
void main()
{
clrscr();
sample s;
s.read();
s.print();
getch();
}
//class with data members and member functions example
#include <iostream.h>
#include<conio.h>
class arithmetic
{
int n1,n2;
public:
void read()
{
cout<<"Enter two numbers";
cin>>n1>>n2;
}
void add()
{
cout<<"The sum of "<<n1<<" and "<<n2<<" is "<<n1+n2<<endl;
}
void sub()
{
cout<<n1<<" - "<<n2<<" = "<<n1-n2<<endl;
}
};
void main()
{
clrscr();
arithmetic a;
a.read();
a.add();
a.sub();
getch();
}
//class with member functions
#include<iostream.h>
#include<conio.h>
class sample
{
public:
void read()
{
cout<<"This is Read()"<<endl;
}
int print()
{
return 10;
}
};
void main()
{
sample s;
clrscr();
s.read();
cout<<"The number is "<<s.print();
getch();
}
//class with data members,member functions and implement that member functions outside the class
#include<iostream.h>
#include<conio.h>
class checkevenodd
{
int n,r;
public:
void read();
void calprint();
};
void checkevenodd::read()
{
cout<<"Enter a Number";
cin>>n;
}
void checkevenodd::calprint()
{
r=n/2;
if(r*2==n)
cout<<n<<" is Even";
else
cout<<n<<" is Odd";
}
void main()
{
checkevenodd c;
clrscr();
c.read();
c.calprint();
getch();
}
//class with multiple objects
#include<iostream.h>
#include<conio.h>
class showposneg
{
int n;
public:
void read()
{
cout<<"Enter a Number";
cin>>n;
}
void calprint()
{
if(n>0)
cout<<n<<" is positive"<<endl;
else
cout<<n<<" is negative"<<endl;
}
};
void main()
{
showposneg s,s1;
clrscr();
s.read();
s.calprint();
s1.read();
s1.calprint();
getch();
}
//class with data members and member functions implement the member functions outside with using pointer object
#include<iostream.h>
#include<conio.h>
class absolute
{
int n;
public:
void read();
void calprint();
};
void absolute::read()
{
cout<<"Enter a number";
cin>>n;
}
void absolute::calprint()
{
if(n>0)
cout<<"The absolute value of "<<n<<" is "<<n;
else
cout<<"The absolute value of "<<n<<" is "<<-n;
}
void main()
{
clrscr();
absolute *a;
(*a).read(); // same a->read();
a->calprint(); //same (*a).calprint();
getch();
}
this Pointer

Every object in C++ has access to its own address through an important pointer called this pointer. The this pointer is an implicit parameter to all member functions. Therefore, inside a member function, this may be used to refer to the invoking object.
Friend functions do not have a this pointer, because friends are not members of a class. Only member functions have a this pointer.
//member function with arguments
#include<iostream.h>
#include<conio.h>
class memwithargs
{
int n1,n2;
public:
void read(int n1,int n2);
void print();
};
void memwithargs::read(int n1,int n2)
{
this->n1=n1;
this->n2=n2;
}
void memwithargs::print()
{
Cout<<"The sum is "<<n1+n2<<endl;
}
void main()
{
memwithargs m;
clrscr();
m.read(10,20);
m.print();
int n,n1;
cout<<"Enter two numbers";
cin>>n>>n1;
m.read(n,n1);
m.print();
getch();
}
//how to access private member function outside the class
#include<iostream.h>
#include<conio.h>
class memwithargs
{
int n1,n2;
void read();
public:
void print();
};
void memwithargs::read()
{
cout<<"Enter two numbers";
cin>>n1>>n2;
}
void memwithargs::print()
{
read();
cout<<"The sum is "<<n1+n2<<endl;
}
void main()
{
memwithargs m;
clrscr();
m.print();
getch();
}
Default Arguments:-

If you want to pass arguments we can pass many no. of arguments. If you want to provide default values in the implementation function we have to provide from right to left.
Syntax:- <return-type> <functionname>(variable1=value,variable2=value)
{
Statements;
}
//member function with using default arguments
#include<iostream.h>
#include<conio.h>
class memdefargs
{
public:
void simpleinterest(float pi=5000,float t=2,float r=1.50)
{
cout<<"Simple Interest = "<< (pi*t*r)/100<<endl;
cout<<"With Interest Amount = "<< (pi+(pi*t*r)/100) <<endl;
}
};
void main()
{
clrscr();
memdefargs m;
m.simpleinterest();
m.simpleinterest(6000);
m.simpleinterest(7000,1.5);
m.simpleinterest(8000,3,2);
getch();
}
ASSIGNMENTS
1. Define a class student. Read admno, sname, subject1,subject2, subject3 values and calculate total and display on the screen.
2. Define a class with batsman bcode, bname, innings, notout, runs,batavg. batavg =runs/(innings-notout)Function to compute batavg. Function to accept value from bcode, name, innings, notout. Function to display on the screen.
3.Define a class Flight with read Flight number, Destination of type, Distance of type, Fuel of type. A member function CALFUEL() to calculate the value of Fuel as per the following criteria
Distance
Fuel
<=1000
500
more than 1000 and <=2000
1100
more than 2000
2200
calculate the quantity of Fuel and to allow user to view the content.

4. Define a class BOOK. Read BOOK NO, BOOKTITLE, PRICE, TOTAL_COST(). It invokes TOTAL_COST() and prints the total, cost to be paid by the user.
5. Define a class REPORT. Read admission number,name,marks, average. Function to display all data report on the screen.
5.Write the definition for a class called Rectangle that has length and width. T o calculate and return the perimeter of the rectangle.
6. Write main function to create two rectangle objects. Set the length and width of the first rectangle to 5 and 2.5. Set the length and width of the second rectangle to 5 and 18.9. Display each rectangle and its area and perimeter.
(i). Check whether the two Rectangles have the same area and print a message indicating the result. Set the length and width of the first rectangle to 15 and 6.
(ii). Display each Rectangle and its area and perimeter again. Again, check whether the two Rectangles have the same area and print a message indicating the result.
7. Write the definition for a class called complex that has floating point data members for storing real and imaginary parts. The class has the following member functions:
void set(float, float) to set the specified value in object
void disp() to display complex number object
complex sum(complex) to sum two complex numbers & return complex number
1. Write the definitions for each of the above member functions.
2. Write main function to create three complex number objects. Set the value in two objects and call sum() to calculate sum and assign it in third object. Display all complex numbers.
8. Write the definition for a class called Distance that has data member feet as integer and inches as float. The class has the following member functions:
void set(int, float) to give value to object
void disp() to display distance in feet and inches
Distance add(Distance) to sum two distances & return distance
1. Write the definitions for each of the above member functions.
2. Write main function to create three Distance objects. Set the value in two objects and call add() to calculate sum and assign it in third object. Display all distances.
9. Write the definition for a class called time that has hours and minutes as integer. The class has the following member functions:
void settime(int, int) to set the specified value in object
void showtime() to display time object
time sum(time) to sum two time object & return time
1. Write the definitions for each of the above member functions.
2. Write main function to create three time objects. Set the value in two objects and call sum() to calculate sum and assign it in third object. Display all time objects.

Encapsulation
Binding the data members and member functions into a single unit is known as Encapsulation.
Ex:- Every class with data members and member functions is a encapsulated class.
Constructor

Rules of Constructors 1. C++ constructors are the special member function of the class which are used to initialize the objects of that class
2. The constructor of class is automatically called immediately after the creation of the object.
3. Name of the constructor should be exactly same as that of name of the class.
4. Constructor should be same name as the class and declared in the public section of the class.
5. C++ constructor is called only once in a lifetime of object when object is created.
6. C++ constructors can be overloaded.
7. C++ constructors does not return any value so constructor have no return type.
8. C++ constructor does not return even void as return type.
9. Constructor can not be virtual.
10. Constructor can not be inherited, but from the derived class we can call the base class constructors.
11. It is not possible to refers to the address of constructors.
12. It is not possible to use the constructor as member of union if the object is created with constructor.
C++ Constructor Types
1. Default Constructor
2. Parametrized Constructor
3. Copy Constructor
Default Constructor 1. If the programmer does not specify the constructor in the program then compiler provides the default constructor.
2. In C++ we can overload the default compiler generated constructor
3. In both cases (user created default constructor or default constructor generated by compiler), the default constructor is always parameterless.
Syntax
class_name() {
-----
-----
}
Example of Default Constructor
//default constructor
#include<iostream.h>
#include<conio.h>
class defcon
{
public:
defcon()
{
}
};
void main()
{
clrscr();
defcon d;
getch();
}
//default constructor to print a message.
#include<iostream.h>
#include<conio.h>
class defcon
{
public:
defcon()
{
cout<<"This is Default Constructor";
}
};
void main()
{
clrscr();
defcon d;
getch();
}
//Write a program read a number and print a number with using default constructor
#include<iostream.h>
#include<conio.h>
class defcons
{
int a;
public:
defcons()
{
cout<<"Enter a Number";
cin>>a;
cout<<"The Number is "<<a;
}
};
void main()
{
clrscr();
defcons d;
getch();
}
Write a program to calculate the given year is leap year or not
#include<iostream.h>
#include<conio.h>
class defcon
{
int n;
public:
defcon()
{
cout<<"Enter Year";
cin>>n;
}
void print()
{
(n%4==0)?cout<<n<<" is Leap Year": cout<<n<<" is Not a Leap Year";
}
};
void main()
{
clrscr();
defcon d;
d.print();
getch();
}
Parameterized Constructor : class name and function name is same and it receives arguments/parameters, is called parameterized constructor.
//Write a program read employee information and print employee information.
#include<iostream.h>
#include<conio.h>
class paramet
{
public:
paramet(int empno,char ename[],float sal)
{
Cout<<"---------------------";
cout<<endl<<"EMPLOYEE INFORMATION";
cout<<endl<<"---------------------";
cout<<endl<<"EMP NO = "<<empno;
cout<<endl<<"EMP NAME = "<<ename;
cout<<endl<<"EMP SALARY = "<<sal;
cout<<endl<<"----------------------" <<endl;
}
};
void main()
{
clrscr();
paramet p(100,"Rama",12000); //implicit call
paramet p1=paramet(101,"Sneha",15500); //explicit call
int eno;
char name[10];
float sal;
cout<<"Enter employee number,name,salary";
cin>>eno>>name>>sal;
paramet p2=paramet(eno,name,sal); //explicit call
getch();
}
Copy Constructor:- A constructor that initializes an object using values of another object passed to it as parameter, is called copy constructor. It creates the copy of the passed object. There can be multiple constructors of the same class, provided they have different signatures.
//copy constructor example
#include<iostream.h>
#include<conio.h>
class copycon
{
int n;
public:
copycon()
{
cout<<"Enter a number";
cin>>n;
cout<<endl<<"Default Constructor Calling";
}
copycon(int x)
{
n=x;
cout<<endl<<"Parametrized Constructor Calling";
}
copycon(copycon &obj)
{
cout<<endl<<"Copy Constructor Calling";
n=obj.n;
}
void print()
{
cout<<endl<<"The number is "<<n;
}
};
void main()
{
clrscr();
copycon c;
c.print();
copycon c2(c);
c2.print();
copycon c1(10);
c1.print();
copycon c3(c1);
c3.print();
getch();
}<;/p>;
Wap check the given number is armstrong or not
#include<iostream.h>
#include<conio.h>
class copycon
{
int n,arm,t;
public:
copycon()
{
arm=0;
n=123;
t=n;
cout<<"The Number is "<<n<<endl;
}
copycon(int num)
{
arm=0;
t=num;
n=num;
cout<<"The Number is "<<n<<endl;
}
copycon(copycon &o);
};
copycon::copycon(copycon &o)
{
n=o.n;
while(n)
{
o.arm+=(n%10)*(n%10)*(n%10);
n/=10;
}
(o.arm==o.t)?cout<<o.t<<" is Armstrong":cout<<o.t<<" is Not Armstrong";
}
void main()
{
clrscr();
copycon c;
copycon c1(c);
int n;
cout<<"Enter a Number";
cin>>n;
copycon c2(n);
copycon c3(c2);
getch();
}
Destructor
A destructor is a special member function of a class that is executed whenever an object of it's class goes out of scope or whenever the delete expression is applied to a pointer to the object of that class.
A destructor will have exact same name as the class prefixed with a tilde (~). Destructor can be very useful for releasing resources before coming out of the program like closing files, releasing memories etc.
//destructor example
#include<iostream.h>
#include<conio.h>
class destruct
{
public:
destruct()
{
cout<<"This is Default Constructor";
}
~destruct()
{
cout<<endl<<"This is Destructor";
}
};
void main()
{
clrscr();
destruct d;
getch();
}
Inheritance
Deriving a new class from existing class is known as Inheritance.
(or)
Inheriting the base class features into derived class is known as Inheritance.
(or)
Extend the definition of a class without making any physical changes to the existing class is inheritance.
Any new class that you create from an existing class is called derived class; existing class is called base class.
Private
Private members of the base class are inaccessible to the derived class.
Protected members of the base class become private members of the derived class.
Public members of the base class become private members of the derived class.
Protected
Private members of the base class are inaccessible to the derived class.
Protected members of the base class become protected members of the derived class.
Public members of the base class become protected members of the derived class.
Public
Private members of the base class are inaccessible to the derived class.
Protected members of the base class become protected members of the derived class.
Public members of the base class become public members of the derived class.
Types of Inheritance
There are 6 types of inheritance.
Single Inheritance: It is the inheritance hierarchy wherein one derived class inherits from one base class.
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiDED8_bcwtzLdhsl7uWWjAY4QvhChSTdhV6B9YzFJKxt_qxe336J_RKA53uRvcz7diUrZYRcLp5oQtwylq4kSYvYhdD1tjX5TFsPu_x4qXj-x5QEFRqg4ASBIaHK1jQE8cEwn8htzzoh0/s320/singleinheritance.jpg
//Single Inheritance
#include<iostream.h>
#include<conio.h>
class A
{
public:
void showA()
{
cout<<”This is Base Class Method”<<endl;
}
};
Class B:public A
{
public:
void showB()
{
cout<<”This is Derived Class Method”<<endl;
}
};
void main()
{
clrscr();
B b;
b.showA();
b.showB();
getch();
}
//Inheritance with Default Constructor
#include<iostream.h>
#include<conio.h>
class A
{
protected:
A()
{
cout<<”This is Base Class Constructor”<<endl;
}
};
class B
{
public:
B()
{
A::A();
Cout<<”This is Derived Class constructor”;
}
};
void main()
{
clrscr();
B b;
getch();
}
//Inheritance with Parametrized Constructor
(or)
// How to call a base class parametrized constructor in derived class
#include<iostream.h>
#include<conio.h>
class A
{
protected:
int r;
protected:
A(int a)
{
r=a/2;
}
};
Class B:private A
{
public:
B(int t):A(t)
{
((r*2)==t)?cout<<t<<” is Even”:cout<<t<<”is Odd.”<<endl;
}
};
void main()
{
clrscr();
int a;
cout<<”Enter a number”;
cin>>a;
B b(a);
getch();
}
Multi Level InheritanceBase class is inheriting features into derived class. Derived Class is inheriting features into sub derived class is nothing but Multilevel Inheritance.
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEik_r40fJhNXLWJT-sDfXQcStNhojO5N0Iu61N_YzXIPVyW-z7q-e7YGxr8tPoFYBwzAcsglMfY9kK6PHMwkk3DslSCf-7Q3f0MqxCInQ9i1dQ-UU84rvpTrBqdsNtZ5zLOgTq2le7DZ2I/s320/multilevel.jpg
//Multilevel Inheritance employee details reading and printing and calculating salary with bonus example
#include<iostream.h>
#include<conio.h>
class A
{
protected:
int eno;
char ename[10];
float sal;
};
class B:protected A
{
protected:
void read()
{
cout<<”Enter Emp no”;
cin>>eno;
cout<<”Enter Emp Name”;
cin>>ename;
cout<<”Enter Emp Salary”;
cin>>sal;
}
};
class C:protected B
{
public:
void print()
{
B::read();
cout<<”EMPLOYEE INFORMATION”<<endl;
cout<<”EMP NO = “<<eno<<endl;
cout<<”EMP NAME = “<<ename;
cout<<”EMP SALARY = “<<sal
}
};
class D:protected C
{
public:
void show()
{
C::print();
If(sal>10000)
bo=1000;
else
bo=500
cout<<”With Bonus Salary = “<<sal+bo;
}
};
void main()
{
clrscr();
D d;
d.show();
getch();
}
Hierarchial Inheritance It is the inheritance hierarchy wherein multiple subclasses inherit from one base class.
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi9vdEtMFMiEOyowIZb2zQqO4Y6DPJB8Hhi7max97bISVQTmh4cNpWDhxXr40nVUAOKaGjLvKJ7MXvcanAFNlGe-eM5SQKMqRV-qXopATpYPoFa4cmDjMjhEprKPvnOBo24vD9QXj8D6j0/s320/hierarchial.jpg
//hierarchial inheritance with reading and printing a number, calculating the square and cube of a number
#include<iostream.h>
#include<conio.h>
class Base
{
protected:
int a;
public:
void read()
{
cout<<”Enter a number”;
cin>>a;
}
};
class Derived1:public Base
{
public:
void print()
{
cout<<endl<<”The number is “<<n;
}
};
class Derived2:protected Base
{
public:
void show()
{
read();
cout<<”The square of the number is “<<n*n;
}
};
class Derived3:private Base
{
public:
void calprint()
{
base::read();
cout<<”Cube of a number is “<<n*n*n;
}
};
void main()
{
Derived1 d1;
d1.read();
d1.print();
Derived2 d2;
d2.show();
Derived3 d3;
d3.calprint();
}
Multiple Inheritance It is the inheritance hierarchy wherein one derived class inherits from multiple base class(es)
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhWEogaHx_CkYJQYS4q9PKuC0jkEB3LIoxFuq3u5G0OXOSikneE7mNbnLrbnp6fmzkqjZWYw1HKVb1-SBqn_sn_xxzVUqjBQTLMF4OGUbjMu0y58c8AT-Em6P446sJEI4DtbFsX3H9de9U/s320/multiple.jpg
//findout the biggest number among the given numbers with using multiple inheritance
#include<iostream.h>
#include<conio.h>
class Base1
{
protected:
int n1;
};
class Base2
{
protected:
int n2;
};
class Derived:Base1,Base2
{
public:
void read()
{
cout<<”Enter two numbers”;
cin>>n1>>n2;
}
void calprint()
{
if(n1>n2)
cout<<n1<<” is Big”;
else
cout<<n2<<” is Big”;
}
};
void main()
{
clrscr();
Derived d;
d.read();
d.calprint();
}
Hybrid InheritanceSingle and Multilevel inheritance combination is nothing but hybrid inheritance.
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhJIwreWYLhxRVfFDocfvkN8MILYWZR-Unhj5A5y2XaeNTzoeAU47d_rnmn-PRFJBGrb7h6lBefQDlmJWao8JShLD0C5TlBoOUhhrozOPCupwXp-YrNJZpvSfHa1tqQh4BBUvfCsYvoHsc/s320/hybridinheritance.png
Multipath InheritanceHierarchial and Multiple inheritance combination is nothing but Multipath Inheritance.
Virtual Base Class
Multipath inheritance may lead to duplication of inherited members from a grandparent base class (A). This may be avoided by making the common base class (A) a virtual base class. When a class is made a virtual base class(A) to B,C. C++ takes necessary care to see that only one copy of that class is inherited to D.
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6Kfr40lzbc96mV9B3DL_0sCyBZCW5VT9FJ8zOMV2fFrwmMwTkOppR8UqTA0GkpFzhgefSjm1rHqZL5m2Tn1cNlvMc150pUiQ4LqWgFxPUdhZEIK_VFPBOQFZSsMgjl_GxX9TsMLHgqdc/s320/multipath.png
#include<iostream.h>
#include<conio.h>
class A
{
protected:
int n1;
};
class B:virtual public A
{
protected:
int n2;
};
class C:virtual public A
{
protected:
int n3;
};
class D:public B,public C
{
public:
void read()
{
cout<<"Enter three numbers";
cin>>n1>>n2>>n3;
}
void bigprint()
{
((n1>n2)&&(n1>n3))?cout<<n1<<" is Big":(n2>n3) ?cout<<n2<<" is Big":cout<<n3<<" is Big";
}
};
void main()
{
clrscr();
D d;
d.read();
d.bigprint();
getch();
}

Polymorphism
Polymorphism means ability to make more than form. (or) A message can be processed in different ways.
There are 2 types of polymorphism.
1. Compile Time polymorphism
2. Run Time polymorphism
Compile time polymorphism:- At the time of compiling a program it binds the data. Ex:- Operator Overloading,Function Overloading
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi0TOmbRyVerULvPPR8P8W0mxFXbhEb328iuQ3qJjp-6D36K1gYuLSeaxQdDB9LKD2xM7XxUalM4JdLWevE-9p253wDscfZYSxD-Er5XWBhX8-Nt5VI7ZNgd6W2FqBasT_l2bEmauAaJpY/s320/0.png
//Unary operator overloading
#include<iostream.h>
#include<conio.h>
class sam
{
int a,b,c;
public:
sam()
{
a=10;
b=20;
c=30;
}
sam(int a,int b,int z)
{
this->a=a;
this->b=b;
c=z;
}
operator ++()
{
++a;
++b;
c++;
}
void print()
{
cout<<"The a value is "<<a<<endl;
cout<<"The b value is "<<b<<endl;
cout<<"The c value is "<<c<<endl;
}
};
void main()
{
clrscr();
sam s;
s.print();
++s;
s.print();
sam s1(99,88,77);
s1.print();
s1++;
s1.print();
getch();
}
//Binary operator overloading
#include<iostream.h>
#include<conio.h>
class sam
{
int a,b,c;
public:
sam()
{
a=10;
b=20;
c=30;
}
sam(int a,int b,int z)
{
this->a=a;
this->b=b;
c=z;
}
sam operator/(sam s)
{
sam t;
t.a=t.a/s.a;
t.b=t.b/s.b;
t.c=t.c/s.c;
return t;
}
void print()
{
cout<<"A value is "<<a<<endl<<"B value is "<<b<<endl<<"C value is "<<c;
}
};
void main()
{
clrscr();
sam o;
o.print();
sam o1(1,2,3);
o1.print();
sam o2;
o2=o/o1;
o2.print();
getch();
}
Runtime Polymorphism:-
 At runtime it binds the data.
Ex:- Virtual functions.
Overriding Base Class Functions
A derived class can override a member function of its base class by defining a derived class member function with the same name and parameter list. It is often useful for a derived class to define its own version of a member function inherited from its base class. This may be done to specialize the member function to the needs of the derived class. When this happens, the base class member function is said to be overridden by the derived class.
#include<iostream.h>
#include<conio.h>
class A
{
public:
void print()
{
cout<<"This is Base Class print()";
}
};
class B:public A
{
public:
void print()
{
cout<<endl<<"This is Derived Class print()";
}
};
void main()
{
clrscr();
B b;
b.print();
getch();
}
Gaining Access to an Overridden Function
 
It is occasionally useful to be able to call the overridden version. This is done by using the scope resolution operator to specify the class of the overridden member function being accessed.
//How to execute both base and derived class methods.
#include<iostream.h>
#include<conio.h>
class A
{
public:
void print()
{
cout<<"This is Base Class print()";
}
};
class B:public A
{
public:
void print()
{
A::print();
cout<<endl<<"This is Derived Class print()";
}
};
void main()
{
clrscr();
B b;
b.print();
getch();
}
Runtime Polymorphism:-
In C++, a pointer variable of a base class type can point to an object of its derived class. There are situations when this feature of C++ can be used to develop generic code for a variety of applications. In general, a pointer to a base class that actually points to a derived class object must first be appropriately cast before the additional features of the derived class can be used. Virtual functions are used in C++ to support polymorphic behavior.
Pointer of base class
#include<iostream.h>
#include<conio.h>
class A
{
public:
virtual void print()
{
cout<<"This is Base Class print()";
}
};
class B:public A
{
public:
void print()
{
cout<<endl<<"This is Derived Class print()";
}
};
void main()
{
clrscr();
A *a,o;
a=&o;
a->print(); //same as (*a).print();
B b;
a=&b;
(*a).print();
getch();
}
A member of a class that can be redefined in its derived classes is known as a virtual member. In order to declare a member of a class as virtual, we must precede its declaration with the keyword virtual. The member function print() has been declared as virtual in the base class because it is later redefined in each derived class. The advantage of having virtual function is that we are able to access print() function of derived class by pointer variable of base class.
Data Abstraction:-
 Abstraction is nothing but hiding. Hiding concept is lined up with inheritance and virtual functions.
Pure Virtual Function and Abstract Class
 
In above example, base class abstr member function display() do not need any implementation because it is overriding in derived class. If this is the case, the C++ language permits the programmer to declare the function a pure virtual function. The C++ way of declaring a pure virtual function is to put the expression = 0 in the class declaration. For example, if a member function double display() is being declared pure virtual, then its declaration in its class looks like
virtual double display() = 0;
A pure virtual function is sometimes called an abstract function, and a class with at least one pure virtual function is called an abstract class. The C++ compiler will not allow you to instantiate an abstract class. Abstract classes can only be subclassed: that is, you can only use them as base classes from which to derive other classes.
A class derived from an abstract class inherits all functions in the base class, and will itself be an abstract class unless it overrides all the abstract functions it inherits. The usefulness of abstract classes lies in the fact that they define an interface that will then have to be supported by objects of all classes derived from it.
#include<iostream.h>
#include<conio.h>
class abstr
{
protected:
int n;
public:
void read()
{
n=5;
}
virtual void display(){};
};
class purev:public abstr
{
public:
void display()
{
cout<<endl<<"The number is "<<n;
}
};
class purev1:public abstr
{
public:
void display()
{
cout<<endl<<"The square of a number is "<<n*n;
}
};
void main()
{
clrscr();
purev p;
p.read();
p.display();
purev1 p1;
p1.read();
p1.display();
getch();
}

Dynamic Binding:- Object binds the data dynamically at runtime.
Message Passing:- Object passes the data from one function to another.