Object Oriented Programming Polymorphism
Object Oriented Programming Polymorphism
//
public:
ShortTerm(int,char*,int,char*,char*,int);
void display();
};
class LongTerm : public DepositAccount
{
int no_of_years;
int loan_taken;
public:
LongTerm(int,char*,int,char*,char*,int,int);
void display();
};
/*----------------------Class Implementations---------------------*/
// member function definitions for 'Account'
void Account::Account(int a, char* b)
{
number = a;
strcpy(name, b);
}
// member function definitions for 'SavingsAccount'
void SavingsAccount::SavingsAccount(int a, char* b, int c) :
Account(a,b)
{
balance = c;
}
void SavingsAccount::display()
{
cout << "Savings Account Details --- \n";
cout << "Number : " << number << "\n";
cout << "Name : " << name << "\n";
cout << "Balance : " << balance << "\n";
}
// member function definitions for 'Deposit'
void Deposit::Deposit(int a, char* b)
{
amount = a;
strcpy(maturity_date, b);
}
// member function definitions for 'DepositAccount'
void DepositAccount::DepositAccount(int a, char* b, int c, char*
d, char* e)
: Account(a,b), Deposit(c,d)
{
strcpy(opening_date, e);
}
// member function definitions for 'ShortTerm'
#include <iostream.h>
class A
{
int a1;
public :
int a2;
void set_a1(int);
void display_a1a2();
};
/*----------------------Class Implementations---------------------*/
// member function definitions for class A
void A::set_a1(int x)
{
a1 = x;
}
void A::display_a1a2()
{
cout << "a1 = " << a1 << "\n";
cout << "a2 = " << a2 << "\n";
}
void main(void)
{
A obj[5];
// array of objects declaration
for (int i=0; i<5; i++) // array of objects initialisation
{
obj[i].set_a1(i+1);
obj[i].a2 = (i+1)*10;
}
A *p[5];
// array of pointers to objects - declaration
for (i=0; i<5; i++) // array of pointers to objects initialisation
p[i] = &obj[i];
for (i=0; i<5; i++) // usage of array of pointers to objects
{
cout << "Object - " << i+1 << ":\n";
p[i]->display_a1a2();
cout << "\n";
}
}
Output :
Object - 1:
a1 = 1
a2 = 10
Object - 2:
a1 = 2
a2 = 20
Object - 3:
a1 = 3
a2 = 30
Object - 4:
a1 = 4
a2 = 40
Object - 5:
a1 = 5
a2 = 50
Example 3 : Program : constructor, destructor in multiple inheritance
#include<iostream.h>
#include<conio.h>
class base1
{
public:
base1()
{
cout<<"Inside base1 constructor"<<endl;
}
~base1()
{
cout<<"Inside base1 destructor"<<endl;
}
};//end of base class
class base2
{
public:
base2()
{
cout<<"Inside base2 constructor"<<endl;
}
~base2()
{
cout<<"Inside base2 destructor"<<endl;
}
};//end of derived class
~derived2()
{
cout<<"Inside derived2 destructor"<<endl;
}
}; //end of derived class
void main()
{
clrscr();
derived2 obj;
}
Example 6 : Program : constructor, destructor in multiple inheritance
#include<iostream.h>
#include<conio.h>
class base1
{
protected:
int a;
public:
base1(int x)
{
a=x;
cout<<"Inside base1 constructor"<<endl;
}
~base1()
{
cout<<"Inside base1 destructor"<<endl;
}
};//end of base class
class base2
{
protected:
int b;
public:
base2(int y)
{
b=y;
cout<<"Inside base2 constructor"<<endl;
}
~base2()
{
~base1()
{
cout<<"Inside base1 destructor"<<endl;
}
};//end of base class
class base2
{
protected:
int b;
public:
base2(int y)
{
b=y;
cout<<"Inside base2 constructor"<<endl;
}
~base2()
{
cout<<"Inside base2 destructor"<<endl;
}
};//end of derived class
class derived : public base1,public base2
{
public:
derived(int x,int y):base1(x),base2(y)
{
cout<<"Inside derived constructor"<<endl;
}
~derived()
{
cout<<"Inside derived destructor"<<endl;
}
void show()
{
cout<<"a : "<<a<<" "<<"b : "<<b<<endl;
}
}; //end of derived class
void main()
{
clrscr();
derived obj(3,4);
obj.show();
}
Example 9 :
#include<iostream.h>
#include<conio.h>
class person
{
public:
char name[20];
int code;
void get_name( )
{
cout<<"\nEnter name : ";
cin>>name;
}
void get_code( )
{
cout<<"\nEnter code : ";
cin>>code;
}
void put_namecode( )
{
cout<<"\nName of person is:"<<name;
cout<<"\nCode is:"<<code;
}
};
class account: virtual public person
{
protected :
int pay;
public:
void get_pay( )
{
cout<<"\nEnter amount to be paid : ";
cin>>pay;
}
void put_pay( )
{
cout<<"\nAmount to be paid : ";
cout<<pay;
}
};
class admin: virtual public person
{
protected :
int exp;
public:
void get_exp( )
{
cout<<"\nEnter experience : ";
cin>>exp;
}
void put_exp( )
{
cout<<"\nExperience of person is : ";
cout<<exp;
}
};
class master : public account, public admin
{
public:
void getdata( )
{
get_name();
get_code();
get_pay();
get_exp();
}
void showdata()
{
put_namecode( );
put_pay( );
put_exp( );
}
};
void main( )
{
master m[20];
int ch,i, code,n,ans=1;
do
{
clrscr();
cout<<"\nMenu";
cout<<"\n1.Create";
cout<<"\n2.Display";
cout<<"\n3.Update";
cin>>ch;
switch(ch)
{
case 1:cout<<"\nEnter no.of persons : ";
cin>>n;
for(int i=0;i<n;i++)
{
m[i].getdata();
}
break;
case 2: cout<<"\nDisplay all entries";
for(i=0;i<n;i++)
m[i].showdata();
break;
case 3: cout<<"\nEnter employee code : ";
cin>>code;
for(i=0;i<n;i++)
if(m[i].code==code)
{
m[i].get_name();
m[i].get_pay();
m[i].get_exp();
break;
}
if(i==n)
cout<<"\nEmp code not found";
break;
}
cout<<"\nDo u want to continue(1/0) ?";
cin>>ans;
}
while(ans==1);
getch();
}
Example 10 :
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int rno;
public:
void get_number()
{
cout<<"\nEnter roll no : ";
cin>>rno;
}
void put_number()
{
cout<<"\nRoll no:"<<rno;
}
};
class test:public student
{
protected :
int mk1,mk2;
public:
void get_marks()
{
cout<<"\nEnter marks1,marks2 : ";
cin>>mk1>>mk2;
}
void put_marks()
{
cout<<"\nMarks are:\n ";
cout<<"Marks1 = "<<mk1<<"\t Marks2 = "<<mk2;
}
};
class sports
{
protected:
int score;
public:
void get_score( )
{
cout<<"\nEnter score:";
cin>>score;
}
void put_score( )
{
cout<<"\nScore of sports is:"<<score;
}
};
class result:public test, public sports
{
private:
int total;
public:
void display( )
{
total =mk1+mk2+score;
put_number( );
put_marks( );
put_score( );
cout<<"\nTotal is :"<<total;
}
};
void main( )
{
result R;
R.get_number( );
R.get_marks( );
R.get_score( );
R.display( );
getch( );
}
int no_of_months;
public:
void set(int,char*,int,char*,int);// Overriding functions
void display();
};
class LongTerm : public DepositeAccount
{
int no_of_years;
int loan_taken;
public:
void set(int,char*,int,char*,int,int);// Overriding functions
void display();
};
/*----------------------Class Implementations---------------------*/
// member function definitions for 'Account'
void Account::set(int a, char* b)
{
number = a;
strcpy(name, b);
}
void Account::display()
{
cout << "Number : " << number << "\n";
cout << "Name : " << name << "\n";
}
// member function definitions for 'SavingsAccount'
void SavingsAccount::set(int a, char* b, int c)
{
Account::set(a,b);
balance = c;
}
void SavingsAccount::display()
{
cout << "Savings Account Details --- \n";
Account::display();
cout << "Balance : " << balance << "\n";
}
// member function definitions for 'DepositeAccount'
void DepositeAccount::set(int a, char* b, int c, char* d)
{
Account::set(a,b);
amount = c;
strcpy(opening_date, d);
}
void DepositeAccount::display()
{
Account::display();
cout << "Amount
: " << amount << "\n";
cout << "Date of Opening : " << opening_date << "\n";
}
// member function definitions for 'ShortTerm'
void ShortTerm::set(int a, char* b, int c, char* d, int e)
{
DepositeAccount::set(a,b,c,d);
no_of_months = e;
}
void ShortTerm::display()
{
cout << "Short Term Deposite Account Details --- \n";
DepositeAccount::display();
cout << "Duration in Months : " << no_of_months << "\n";
}
// member function definitions for 'LongTerm'
void LongTerm::set(int a, char* b, int c, char* d, int e, int f)
{
DepositeAccount::set(a,b,c,d);
no_of_years = e;
loan_taken = f;
}
void LongTerm::display()
{
cout << "Long Term Deposite Account Details --- \n";
DepositeAccount::display();
cout << "Duration in Years : " << no_of_years << "\n";
cout << "Loan Taken
: " << loan_taken << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
SavingsAccount S;
S.set(1323, "Stavan", 10000);
S.display();
cout << "\n";
ShortTerm ST;
ST.set(17099, "Kush", 25000, "12/02/07", 3);
ST.display();
cout << "\n";
LongTerm LT;
LT.set(17169, "Saachi", 30000, "15/03/07", 2, 1000);
LT.display();
cout << "\n";
}
int no_of_months;
public:
void set(int,char*,int,char*,char*,int);// Overriding functions
void display();
};
class LongTerm : public DepositeAccount
{
int no_of_years;
int loan_taken;
public:
void set(int,char*,int,char*,char*,int,int);// Overriding functions
void display();
};
/*----------------------Class Implementations---------------------*/
// member function definitions for 'Account'
void Account::set(int a, char* b)
{
number = a;
strcpy(name, b);
}
void Account::display()
{
cout << "Number : " << number << "\n";
cout << "Name : " << name << "\n";
}
// member function definitions for 'SavingsAccount'
void SavingsAccount::set(int a, char* b, int c)
{
Account::set(a,b);
balance = c;
}
void SavingsAccount::display()
{
cout << "Savings Account Details --- \n";
Account::display();
cout << "Balance : " << balance << "\n";
}
// member function definitions for 'Deposite'
void Deposite::set(int a, char* b)
{
amount = a;
strcpy(maturity_date, b);
}
void Deposite::display()
{
cout << "Amount : " << amount << "\n";
S.display();
cout << "\n";
ShortTerm ST;
ST.set(17099, "Kush", 25000, "12/05/02", "12/02/02", 3);
ST.display();
cout << "\n";
LongTerm LT;
LT.set(17169, "Vishwas", 30000, "15/03/04", "15/03/02", 2, 1000);
LT.display();
cout << "\n";
}
Example 13 :
#include<iostream.h>
#include<stdio.h>
class shape
{
protected:
double bs,ht;
public:
void getdata();
virtual void displayarea()
{
cout<<"\nArea : ";
}
};
void shape::getdata()
{
cout<<"\nEnter base : ";
cin>>bs;
cout<<"\nEnter height : ";
cin>>ht;
}
class triangle:public shape
{
double area;
public:
void displayarea()
{
area=0.5*bs*ht;
cout<<"\nArea of triangle is:"<<area;
}
};
class rect:public shape
{
double area;
public:
void displayarea()
{
area=bs*ht;
cout<<"\nArea of rect is:"<<area;
}
};
void main()
{
shape *p,s;
triangle t;
rect r;
t.getdata();
p=&t;
p->displayarea();
r.getdata();
p=&r;
p->displayarea();
}
Example 14 :
#include<iostream.h>
#include<conio.h>
class staff
{
private:
int code;
char name[50];
char add[100];
public:
void getdata()
{
cout<<"\nEnter code :";
cin>>code;
cout<<"\nEnter name : ";
cin>>name;
cout<<"\nEnter Address : ";
cin>>add;
}
void showdata()
{
cout<<"\nCode = "<<code;
cout<<"\nName = "<<name;
cout<<"\nAddress ="<<add;
}
};
class teacher:public staff
{
private:
char sub[20];
char pub[20];
public:
void getdata()
{
staff::getdata();
cout<<"\nEnter subject and publication:";
cin>>sub>>pub;
}
void showdata()
{
staff::showdata();
cout<<"\nSubject is:"<<sub;
cout<<"\nPublication is:"<<pub;
}
};
class officer:public staff
{
private:
char grade[5];
public:
void getdata()
{
staff::getdata();
cout<<"\nEnter grade:";
cin>>grade;
}
void showdata()
{
staff::showdata();
cout<<"\nGrade is:"<<grade;
}
};
class typist:public staff
{
private:
int speed;
public:
void getdata()
{
staff::getdata();
cout<<"\nEnter speed : ";
cin>>speed;
}
void showdata()
{
staff::showdata();
cout<<"\nSpeed is : "<<speed;
}
};
class casual:public typist
{
private :
int dailywages;
public:
void getdata()
{
typist::getdata();
cout<<"\nEnter daily wages :";
cin>>dailywages;
}
void showdata()
{
typist::showdata();
cout<<"\nDailywages are :"<<dailywages;
}
};
class regular:public typist
{
private:
int pay;
public:
regular() {}
void showdata()
{
typist::showdata();
cout<<"\nMonthly pay is:"<<pay;
}
};
void main()
{
casual c;
officer o;
teacher t;
regular r;
c.getdata();
o.getdata();
t.getdata();
r.getdata();
c.showdata();
o.showdata();
t.showdata();
r.showdata();
}
Example 15 :
# include <iostream.h>
# include <conio.h>
class B1
{
int x;
public :
B1(int i)
{
x = i;
cout<<"\nB1 initialised";
}
~B1()
{
cout <<"\nB1 destroyed";
}
};
class B2
{
float y;
public :
B2(float j)
{
y = j;
cout<<"\nB2 initialized";
}
~B2()
{
cout<<"\nB2 destroyed";
}
};
class D : public B1, public B2
{
int m, n;
public :
D(int a, float b, int c, int d) : B1(a), B2(b)
{
m=c;
n=d;
cout<<"\nD is initialised";
}
void show_mn (void)
{
cout<<"\nm="<<m<<"\n"
<<"n="<<n<<"\n";
}
~D()
{
cout<<"\nD is destroyed";
}
};
main( )
{
clrscr( );
{
D d(5, 10.75, 20, 30);
cout<<"\n";
d.show_mn();
}
getch();
return 0;
}
Output :
B1 initialised
B2 initialized
D is initialised
m=20
n=30
D is destroyed
B2 destroyed
B1 destroyed
Example 16 : Program to illustrates Mixed Inheritance Hierarchy with Abstract
Classes
#include <iostream.h>
#include <string.h>
/*----------------------Class Interfaces-------------------------*/
class Account
// Abstract Base class
{
protected :
int number;
char name[10];
public :
Account(int, char*);
virtual void display() = 0;
// pure virtual function
};
class SavingsAccount : public Account
{
int balance;
public:
SavingsAccount(int,char*,int);
void display();
};
class Deposit
// Abstract Base class
{
protected :
int amount;
char maturity_date[9];
public:
Deposit(int, char*);
virtual void display() = 0; // pure virtual function
};
class DepositAccount : public Account, public Deposit
{
// Abstract Base class
protected :
char opening_date[9];
public:
DepositAccount(int,char*,int,char*,char*);
virtual void display() = 0; // pure virtual function
};
class ShortTerm : public DepositAccount
{
int no_of_months;
public:
ShortTerm(int,char*,int,char*,char*,int);
void display();
};
class LongTerm : public DepositAccount
{
int no_of_years;
int loan_taken;
public:
LongTerm(int,char*,int,char*,char*,int,int);
void display();
};
/*----------------------Class Implementations---------------------*/
// member function definitions for 'Account'
void Account::Account(int a, char* b)
{
number = a;
strcpy(name, b);
}
// member function definitions for 'SavingsAccount'
void SavingsAccount::SavingsAccount(int a, char* b, int c) :
Account(a,b)
{
balance = c;
}
void SavingsAccount::display()
{
cout << "Savings Account Details --- \n";
cout << "Number : " << number << "\n";
cout << "Name : " << name << "\n";
cout << "Balance : " << balance << "\n";
}
// member function definitions for 'Deposit'
void Deposit::Deposit(int a, char* b)
{
amount = a;
strcpy(maturity_date, b);
}
{
char name[10];
int empno;
public :
void set(int, char*);
void display();
};
class manager : public employee // Level - 1
{
int no_of_projects;
public:
void set(int, char*, int);
// overriding functions
void display();
};
class area_manager : public manager // Level - 2
{
char location[10];
public:
void set(int, char*, int, char*);// overriding functions
void display();
};
/*----------------------Class Implementations---------------------*/
// member function definitions for 'employee'
void employee::set(int c, char* s)
{
empno = c;
strcpy(name, s);
}
void employee::display()
{
cout << "Employee No. : " << empno << "\n";
cout << "Name
: " << name << "\n";
}
// member function definitions for 'manager'
void manager::set(int c, char* s, int p)
{
employee::set(c, s);
no_of_projects = p;
}
void manager::display()
{
employee::display();
};
class area_manager : private manager // Level - 2
{
char location[10];
public:
void set(int, char*, int, char*);// overriding functions
void display();
};
/*----------------------Class Implementations---------------------*/
// member function definitions for 'employee'
void employee::set(int c, char* s)
{
empno = c;
strcpy(name, s);
}
void employee::display()
{
cout << "Employee No. : " << empno << "\n";
cout << "Name
: " << name << "\n";
}
// member function definitions for 'manager'
void manager::set(int c, char* s, int p)
{
employee::set(c, s);
no_of_projects = p;
}
void manager::display()
{
employee::display();
cout << "No of Projects currently handling : ";
cout << no_of_projects << "\n";
}
// member function definitions for 'area_manager'
void area_manager::set(int c, char* s, int p, char* l)
{
manager::set(c, s, p);
strcpy(location, l);
}
void area_manager::display()
{
manager::display();
cout << "Location : ";
cout << location << "\n";
}
{
cout << "Employee No. : " << empno << "\n";
cout << "Name
: " << name << "\n";
}
// member function definitions for 'manager'
void manager::set(int c, char* s, int p)
{
employee::set(c, s);
no_of_projects = p;
}
void manager::display()
{
employee::display();
cout << "No of Projects currently handling : ";
cout << no_of_projects << "\n";
}
// member function definitions for 'area_manager'
void area_manager::set(int c, char* s, int p, char* l)
{
manager::set(c, s, p);
strcpy(location, l);
}
void area_manager::display()
{
manager::display();
cout << "Location : ";
cout << location << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
area_manager A;
A.set(1001, "Saachi", 7, "New York");
A.display();
}
Example 20 : Program to illustrates Multi-level Inheritance with protected members
#include <iostream.h>
#include <string.h>
/*----------------------Class Interfaces-------------------------*/
class employee
// base class
{
protected :
char name[10];
int empno;
public :
void set(int, char*);
void display();
};
class manager : protected employee // Level - 1
{
protected :
int no_of_projects;
public:
void set(int, char*, int);
// overriding functions
void display();
};
class area_manager : protected manager // Level - 2
{
char location[10];
public:
void set(int, char*, int, char*);// overriding functions
void display();
};
/*----------------------Class Implementations---------------------*/
// member function definitions for 'employee'
void employee::set(int c, char* s)
{
empno = c;
strcpy(name, s);
}
void employee::display()
{
cout << "Employee No. : " << empno << "\n";
cout << "Name
: " << name << "\n";
}
// member function definitions for 'manager'
void manager::set(int c, char* s, int p)
{
//protected members of the parent are directly available
empno = c;
strcpy(name, s);
no_of_projects = p;
}
void manager::display()
{
class base1
{
// base class
int x;
public :
void set_x(int);
void display_x();
};
class base2
{
// base class
float y;
public:
void set_y(float);
void display_y();
};
class derived : public base1, private base2
{
char str[10];
public:
void set(float, char*);
void display();
};
/*----------------------Class Implementations---------------------*/
// member function definitions for 'base1'
void base1::set_x(int c)
{
x = c;
}
void base1::display_x()
{
cout << "x = " << x << "\n";
}
// member function definitions for 'base2'
void base2::set_y(float c)
{
y = c;
}
void base2::display_y()
{
cout << "y = " << y << "\n";
}
// member function definitions for 'derived'
void derived::set(float b, char* c)
{
/* public member of 'base2' becomes private in 'derived'
hence, accessed within the function body */
set_y(b);
strcpy(str, c);
}
void derived::display()
{
/* public member of 'base2' becomes private in 'derived'
hence, accessed within the function body */
display_y();
cout << "str = ";
cout << str << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
derived D;
/* public member of the 'base1' is public in 'derived'
hence, can be directly used */
D.set_x(10);
D.set(25.3, "God");
/* public member of the 'base1' is public in 'derived'
hence, can be directly used */
D.display_x();
D.display();
}
Example 22 : Program to illustrates Container Classes with constructors
#include <iostream.h>
#include <string.h>
/*----------------------Class Interfaces-------------------------*/
class A
{
int x;
public :
A(int);
// Constructor
void display();
};
class B
{
char w[10];
int y;
public :
B(char*,int);
void display();
};
class C
{
// Constructor
// container class
A obj1;
B obj2;
int z;
public :
C(int,int,int,char*);
void display();
// object of class A
// object of class B
};
/*----------------------Class Implementations---------------------*/
// member function definitions for 'A'
A::A(int a)
{
x = a;
}
void A::display()
{
cout << "x = "<< x << "\n";
}
// member function definitions for 'B'
B::B(char* a, int b)
{
strcpy(w, a);
y = b;
}
void B::display()
{
cout << "w = " << w << "\n";
cout << "y = " << y << "\n";
}
// member function definitions for 'C'
C::C(int a, int b, int c, char* d): obj1(a), obj2(d,c)
{
z = b;
}
void C::display()
{
obj1.display();
obj2.display();
cout << "z = " << z << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
C c(10,20,30,"God");
c.display();
}
Example 23 : Program to illustrates Container Classes
#include <iostream.h>
#include <string.h>
/*----------------------Class Interfaces-------------------------*/
class character
{
char element;
public :
void set_char(char);
void display_char();
};
class word
// container class
{
character w[5];
// contain array of objects of
int length;
// class 'character'
public :
void set_word(char*);
int get_length();
void display_word();
};
class line
// container class
{
word l[6];
// contain array of objects of
// class 'word'
public :
void set_line(char**);
void display_line();
};
/*----------------------Class Implementations---------------------*/
// member function definitions for 'character'
void character::set_char(char a)
{
element = a;
}
void character::display_char()
{
cout << element;
}
/*----------------------Class Interfaces-------------------------*/
class employee
{
char name[10];
int empno;
public :
void set(int, char*);
void display_heading();
void display();
};
class manager : public employee
{
int no_of_projects;
public:
void set(int, char*, int);
// functions overriding the
void display_heading();
// base class definitions
void display_projects();
};
/*----------------------Class Implementations---------------------*/
// member function definitions for 'employee'
void employee::set(int c, char* s)
{
empno = c;
strcpy(name, s);
}
void employee::display_heading()
{
cout << "Employee Information : \n";
}
void employee::display()
{
cout << "Employee No. : " << empno << "\n";
cout << "Name
: " << name << "\n";
}
// member function definitions for 'manager'
void manager::set(int c, char* s, int p)
{
no_of_projects = p;
employee::set(c, s);// base class function explicitly invoked
}
void manager::display_heading()
{
cout << "Manager Details : \n";
}
void manager::display_projects()
{
cout << "No of Projects currently handling : ";
cout << no_of_projects << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
manager peter;
peter.set(1000, "Peter", 4);
peter.display_heading();
peter.display();
peter.display_projects();
}
{
cout << "a1 = " << a1 << "\n";
cout << "a2 = " << a2 << "\n";
}
// member function definitions for class B
void B::set(int x)
{
a3 = x;
}
void B::display()
{
A::display();
cout << "a3 = " << a3 << "\n";
cout << "a4 = " << a4 << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
A *base, obj1;
base = &obj1;
base->set(1);
base->a2 = 2;
cout << "Base class data members : \n";
base->display();
B obj2;
// Derived class object
base = &obj2;
base->set(10);
base->a2 = 20;
cout << "Base class data members (through subsumption) : \n";
base->display();
B *derived;
derived = &obj2;
derived->set(30);
derived->a4 = 40;
cout << "Derived class data members : \n";
derived->display();
}
int a1;
public :
int a2;
A(int, int);
void display_a1a2();
};
/*----------------------Class Implementations---------------------*/
// member function definitions for class A
void A::A(int x, int y)
{
a1 = x;
a2 = y;
}
void A::display_a1a2()
{
cout << "a1 = " << a1 << "\n";
cout << "a2 = " << a2 << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
int A::*ip;
// declaration of pointer to data member
// of class 'A'
void (A::*fp)();
// declaration of pointer to member function
// of class 'A'
A obj(12,14);
ip = &A::a2;
// pointers to class members assigned
fp = &A::display_a1a2;
obj.*ip = 15; // modified object's data member through pointer
(obj.*fp)();
A(int, int);
void display_a1a2();
};
/*----------------------Class Implementations---------------------*/
// member function definitions for class A
A::A(int x, int y)
{
a1 = x;
a2 = y;
}
void A::display_a1a2()
{
cout << "a1 = " << a1 << "\n";
cout << "a2 = " << a2 << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
int A::*ip;
// declaration of pointer to data member
// of class 'A'
void (A::*fp)();
// declaration of pointer to member function
// of class 'A'
A obj(12,14), *p;
p = &obj;
// pointer to object assigned
ip = &A::a2;
// pointers to class members assigned
fp = &A::display_a1a2;
p->*ip = 15;
// modified object's data member through pointer
(p->*fp)();
// call to member function through pointer
}
/*----------------------Class Implementations---------------------*/
// member function definitions for class A
void A::set_a1(int x)
{
a1 = x;
}
void A::display_a1a2()
{
cout << "a1 = " << a1 << "\n";
cout << "a2 = " << a2 << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
A obj, *p;
// pointer to object declared
p = &obj;
// pointer to object assigned
p->set_a1(10);
p->a2 = 20;
(*p).display_a1a2();// same as 'p->display_a1a2()'
}
};
class four_wheeler : public vehicle
{
char is_ac;
public :
four_wheeler(char*, int, char);
void display();
};
/*----------------------Class Implementations---------------------*/
// member function definitions for base class 'vehicle'
vehicle::vehicle(char *a, int b)
{
strcpy(make, a);
milage = b;
}
// member function definitions for base class 'two_wheeler'
two_wheeler::two_wheeler(char *a, int b, char c):vehicle(a,b)
{
is_auto_clutch = c;
}
void two_wheeler::display()
{
cout << "Two-wheeler Details - \n";
cout << "Vehicle Make : " << make << "\n";
cout << "Milage : " << milage << "\n";
cout << "Is Auto Clutch available : " << is_auto_clutch << "\n";
}
// member function definitions for base class 'four_wheeler'
four_wheeler::four_wheeler(char *a, int b, char c):vehicle(a,b)
{
is_ac = c;
}
void four_wheeler::display()
{
cout << "Four-wheeler Details - \n";
cout << "Vehicle Make : " << make << "\n";
cout << "Milage : " << milage << "\n";
cout << "Is A/C available : " << is_ac << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
vehicle *base;
// base class pointer
two_wheeler bike("Hero Honda",60,'N');
base = &bike;
base->display();
cout << "\n";
four_wheeler car("Maruti",18,'Y');
base = &car;
base->display();
}
};
class two_wheeler : public vehicle
{
char is_auto_clutch;
public :
two_wheeler(char*, int, char);
void display();
};
class four_wheeler : public vehicle
{
char is_ac;
public :
four_wheeler(char*, int, char);
void display();
};
/*----------------------Class Implementations---------------------*/
// member function definitions for base class 'vehicle'
vehicle::vehicle(char *a, int b)
{
strcpy(make, a);
milage = b;
}
// member function definitions for base class 'two_wheeler'
two_wheeler::two_wheeler(char *a, int b, char c):vehicle(a,b)
{
is_auto_clutch = c;
}
void two_wheeler::display()
{
cout << "Two-wheeler Details - \n";
cout << "Vehicle Make : " << make << "\n";
cout << "Milage : " << milage << "\n";
cout << "Is Auto Clutch available : " << is_auto_clutch << "\n";
}
// member function definitions for base class 'four_wheeler'
four_wheeler::four_wheeler(char *a, int b, char c):vehicle(a,b)
{
is_ac = c;
}
void four_wheeler::display()
{
cout << "Four-wheeler Details - \n";
cout << "Vehicle Make : " << make << "\n";
cout << "Milage : " << milage << "\n";
cout << "Is A/C available : " << is_ac << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
vehicle *base;
// base class pointer
two_wheeler bike("Hero Honda",60,'N');
base = &bike;
base->display();
cout << "\n";
four_wheeler car("Maruti",18,'Y');
base = &car;
base->display();
}
class A
{
int a1;
public :
int a2;
void set_a1(int);
void display_a1a2();
};
// B as an extension of A
class B : public A
// public derivation
{
int b;
// private data memeber
public :
void set_a2_and_b(int, int);// public member functions
void display_b();
};
/*----------------------Class Implementations---------------------*/
// member function definitions for class A
void A::set_a1(int x)
{
a1 = x;
}
void A::display_a1a2()
{
cout << "a1 = " << a1 << "\n";
cout << "a2 = " << a2 << "\n";
}
// member function definitions for class B
void B::set_a2_and_b(int x, int y)
{
a2 = x; // accessing public data member of base directly
b = y;
}
void B::display_b()
{
cout << "b = " << b << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
B obj;
int A::get_a1()
{
return a1;
}
// member function definitions for class B
void B::set(int x, int y, int z)
{
/* public members of base are accessed within the member function
of the derived class. */
set_a1a2(x,y);
// accessing own private member as usual
b = z;
}
void B::display()
{
/* public members of base are accessed within the member function
the derived class.*/
cout << "a1 = " << get_a1() << "\n";
cout << "a2 = " << a2 << "\n";
// accessing own private member as usual
cout << "b = " << b << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
B obj;
// OUTSIDE WORLD ONLY SEE THE INTERFACE OF THE DERIVED
CLASS
obj.set(10,20,30);
obj.display();
}
Output :
a1 = 10
a2 = 20
b = 30
of
};
// B as an extension of A
class B : protected A
// protected derivation
{
int b;
// private data memeber
public :
void set(int, int, int, int);// public member functions
void display();
};
/*----------------------Class Implementations---------------------*/
// member function definitions for class A
void A::set_a1(int x)
{
a1 = x;
}
int A::get_a1()
{
return a1;
}
// member function definitions for class B
void B::set(int w, int x, int y, int z)
{
// public members of base are accessed
set_a1(w);
a3 = y;
// protected member of the base is accessed
a2 = x;
// accessing own private member as usual
b = z;
}
void B::display()
{
// public member of the base is accessed
cout << "a1 = " << get_a1() << "\n";
// protected member of the base is accessed
cout << "a2 = " << a2 << "\n";
// public member of the base is accessed
cout << "a3 = " << a3 << "\n";
// accessing own private member as usual
cout << "b = " << b << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
B obj;
// OUTSIDE WORLD ONLY SEE THE INTERFACE OF THE DERIVED
CLASS
obj.set(10,20,30,40);
obj.display();
}
Example 34 : Program to illustrates this pointer
#include<iostream.h>
#include<conio.h>
class example
{
private :
int i;
public :
void setdata(int ii)
{
i = ii;
//one way to set data
cout<<endl<<" My object's address is "<<this << endl;
this->i=ii;
// another way to set data
}
void showdata( )
{
cout<<i;
// one way to display data
// virtual function
};
class two_wheeler : public vehicle
{
char is_auto_clutch;
public :
two_wheeler(char*, int, char);
void display();
};
class four_wheeler : public vehicle
{
char is_ac;
public :
four_wheeler(char*, int, char);
void display();
};
/*----------------------Class Implementations---------------------*/
// member function definitions for base class 'vehicle'
vehicle::vehicle(char *a, int b)
{
strcpy(make, a);
milage = b;
}
void vehicle::display()
{
cout << "Vehicle Make : " << make << "\n";
cout << "Milage : " << milage << "\n";
}
// member function definitions for base class 'two_wheeler'
two_wheeler::two_wheeler(char *a, int b, char c):vehicle(a,b)
{
is_auto_clutch = c;
}
void two_wheeler::display()
{
cout << "Two-wheeler Details - \n";
vehicle::display();
cout << "Is Auto Clutch available : " << is_auto_clutch << "\n";
}
// member function definitions for base class 'four_wheeler'
four_wheeler::four_wheeler(char *a, int b, char c):vehicle(a,b)
{
is_ac = c;
}
void four_wheeler::display()
{
cout << "Four-wheeler Details - \n";
vehicle::display();
cout << "Is A/C available : " << is_ac << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
vehicle *base;
// base class pointer
two_wheeler bike("Hero Honda",60,'N');
base = &bike;
base->display();
cout << "\n";
four_wheeler car("Maruti",18,'Y');
base = &car;
base->display();
}
protected:
int a;
public:
base(int x)
{
a=x;
cout<<"Inside base constructor"<<endl;
}
~base()
{
cout<<"Inside base destructor"<<endl;
}
};//end of base class
class derived : public base
{
int b;
public:
derived(int x,int y):base(y)
{
b=x;
cout<<"Inside derived constructor"<<endl;
}
~derived()
{
cout<<"Inside derived destructor"<<endl;
}
void show()
{
cout<<"\na : "<<a<<" "<<"b : "<<b<<endl;
}
};//end of derived class
void main()
{
clrscr();
derived obj(5,4);
obj.show();
}
Example 40 :
# include<iostream.h>
# include<conio.h>
class B
{
public:
B( )
{
cout<<"Constructor of base class";
}
~B( )
{
cout<<"Destructor of base class";
}
};
class D1: private B
{
public:
D1( )
{
cout<<"\n This is constructor of derived class 1";
}
~D1( )
{
cout<<"\n This is destructor of derived class 1";
}
vid showmesg( )
{
cout<<"\n Hi This derived class function";
}
};
class D2:public D1
{
public:
D2( )
{
cout<<"\n This is constructor of derived class 1";
}
~D2( )
{
cout<<"\n This is constructor of derived class 1";
}
};
void main()
{
clrscr( );
B b;
cout<<"\n";
D1 d1;
cout<<"\n";
D2 d2;
d2.showmesg();
cout<<"\n";
getch( );
}