Sie sind auf Seite 1von 68

Chapter 6

Example 1 : 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
{
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);
}
// 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'

void ShortTerm::ShortTerm(int a, char* b, int c, char* d, char*


e, int f)
: DepositAccount(a,b,c,d,e)
{
no_of_months = f;
}
void ShortTerm::display()
{
cout << "Short Term Deposit Account Details --- \n";
cout << "Number : " << number << "\n";
cout << "Name : " << name << "\n";
cout << "Amount : " << amount << "\n";
cout << "Maturity Date : " << maturity_date << "\n";
cout << "Date of Opening : " << opening_date << "\n";
cout << "Duration in Months : " << no_of_months << "\n";
}
// member function definitions for 'LongTerm'
void LongTerm::LongTerm(int a, char* b, int c, char* d, char* e,
int f, int g)
: DepositAccount(a,b,c,d,e)
{
no_of_years = f;
loan_taken = g;
}
void LongTerm::display()
{
cout << "Long Term Deposit Account Details --- \n";
cout << "Number : " << number << "\n";
cout << "Name : " << name << "\n";
cout << "Amount : " << amount << "\n";
cout << "Maturity Date : " << maturity_date << "\n";
cout << "Date of Opening : " << opening_date << "\n";
cout << "Duration in Years : " << no_of_years << "\n";
cout << "Loan Taken
: " << loan_taken << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
Account *top1;
// base class pointer
top1 = new SavingsAccount(1323, "Stavan", 10000);
top1->display();
delete top1;
cout << "\n";
DepositAccount *top2;

// base class pointer

cout << "Deposit Display using pointer";


cout <<
" of type base class 'DepositAccount' - \n";
top2 = new ShortTerm(17099, "Kush", 25000, "12/05/02", "12/02/02", 3);
top2->display();
delete top2;
cout << "\n";
top2 = new LongTerm(17169, "Samarth", 30000, "15/03/04",
"15/03/02", 2, 1000);
top2->display();
delete top2;
cout << "\n";
}
Output :
Savings Account Details --Number : 1323
Name : Stavan
Balance : 10000
Deposit Display using pointer of type base class 'DepositAccount' Short Term Deposit Account Details --Number : 17099
Name : Kush
Amount : 25000
Maturity Date : 12/05/02
Date of Opening : 12/02/02
Duration in Months : 3
Long Term Deposit Account Details --Number : 17169
Name : Samarth
Amount : 30000
Maturity Date : 15/03/04
Date of Opening : 15/03/02
Duration in Years : 2
Loan Taken
: 1000

Example 2 : Array of Pointers to objects

#include <iostream.h>
class A
{
int a1;
public :
int a2;

// private data member


// public data member

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

class derived : public base1,public base2


{
public:
derived()
{
cout<<"Inside derived constructor"<<endl;
}
~derived()
{
cout<<"Inside derived destructor"<<endl;
}
};//end of derived class
void main()
{
clrscr();
derived obj;
}
Output :
Inside base1 constructor
Inside base2 constructor
Inside derived constructor
Inside derived destructor
Inside base2 destructor
Inside base1 destructor
Example 4 : Program : constructor, destructor in multilevel inheritance
#include<iostream.h>
#include<conio.h>
class base
{
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 derived1 : public base
{
protected:
int b;
public:
derived1(int x,int y):base(x)
{
b=y;
cout<<"Inside derived1 constructor"<<endl;
}
~derived1()
{
cout<<"Inside derived1 destructor"<<endl;
}
};//end of derived class
class derived2 : public derived1
{
int c;
public:
derived2(int x,int y,int z):derived1(x,y)
{
c=z;
cout<<"Inside derived2 constructor"<<endl;
}
~derived2()
{
cout<<"Inside derived2 destructor"<<endl;
}
void show()
{
cout<<"a : "<<a<<" "<<"b : "<<b<<" "<<"c : "<<c<<endl;
}
}; //end of derived class
void main()
{
clrscr();
derived2 obj(3,4,5);
obj.show();
}
Output :

Inside base constructor


Inside derived1 constructor
Inside derived2 constructor
a:3b:4 c:5
Inside derived2 destructor
Inside derived1 destructor
Inside base destructor
Example 5 : Program : constructor, destructor in multilevel inheritance
#include<iostream.h>
#include<conio.h>
class base
{
public:
base()
{
cout<<"Inside base constructor"<<endl;
}
~base()
{
cout<<"Inside base destructor"<<endl;
}
};//end of base class
class derived1 : public base
{
public:
derived1()
{
cout<<"Inside derived1 constructor"<<endl;
}
~derived1()
{
cout<<"Inside derived1 destructor"<<endl;
}
};//end of derived class
class derived2 : public derived1
{
public:
derived2()
{
cout<<"Inside derived2 constructor"<<endl;
}

~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()
{

cout<<"Inside base2 destructor"<<endl;


}
}; //end of derived class
class derived : public base1,public base2
{
int c;
public:
derived(int x,int y,int z):base1(y),base2(z)
{
c=x;
cout<<"Inside derived constructor"<<endl;
}
~derived()
{
cout<<"Inside derived destructor"<<endl;
}
void show()
{
cout<<"a : "<<a<<" "<<"b : "<<b<<" "<<"c : "<<c<<endl;
}
}; //end of derived class
void main()
{
clrscr();
derived obj(3,4,5);
obj.show();
}

Example 7 : 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()
{
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 8 : Program : constructor, destructor in single inheritance


#include<iostream.h>
class base
{
public:
base()
{
cout<<"Inside base constructor"<<endl;
}
~base()
{
cout<<"Inside base destructor"<<endl;
}
};//end of base class
class derived : public base
{
public:
derived()
{
cout<<"Inside derived constructor"<<endl;
}
~derived()
{
cout<<"Inside derived destructor"<<endl;
}
}; //end of derived class
void main()
{
derived obj;
}

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( );
}

Example 11 : Program to illustrates Hierarchical Inheritance


#include <iostream.h>
#include <string.h>
/*----------------------Class Interfaces-------------------------*/
class Account
{
int number;
char name[10];
public :
void set(int, char*);
void display();
};
class SavingsAccount : public Account
{
int balance;
public:
void set(int,char*,int);// Overriding functions
void display();
};
class DepositeAccount : public Account
{
int amount;
char opening_date[9];
public:
void set(int,char*,int,char*);// Overriding functions
void display();
};
class ShortTerm : public DepositeAccount
{

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";
}

Example 12 : Program to illustrates Hybrid Inheritance


#include <iostream.h>
#include <string.h>
/*----------------------Class Interfaces-------------------------*/
class Account
{
int number;
char name[10];
public :
void set(int, char*);
void display();
};
class SavingsAccount : public Account
{
int balance;
public:
void set(int,char*,int);// Overriding functions
void display();
};
class Deposite
{
int amount;
char maturity_date[9];
public:
void set(int, char*);
void display();
};
class DepositeAccount : public Account, public Deposite
{
char opening_date[9];
public:
void set(int,char*,int,char*,char*);// Overriding functions
void display();
};
class ShortTerm : public DepositeAccount
{

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";

cout << "Maturity Date : " << maturity_date << "\n";


}
// member function definitions for 'DepositeAccount'
void DepositeAccount::set(int a, char* b, int c, char* d, char* e)
{
Account::set(a,b);
Deposite::set(c,d);
strcpy(opening_date, e);
}
void DepositeAccount::display()
{
Account::display();
Deposite::display();
cout << "Date of Opening : " << opening_date << "\n";
}
// member function definitions for 'ShortTerm'
void ShortTerm::set(int a, char* b, int c, char* d, char* e, int f)
{
DepositeAccount::set(a,b,c,d,e);
no_of_months = f;
}
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, char* e, int f, int g)
{
DepositeAccount::set(a,b,c,d,e);
no_of_years = f;
loan_taken = g;
}
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/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);
}

// 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'
void ShortTerm::ShortTerm(int a, char* b, int c, char* d, char*
e, int f)
: DepositAccount(a,b,c,d,e)
{
no_of_months = f;
}
void ShortTerm::display()
{
cout << "Short Term Deposit Account Details --- \n";
cout << "Number : " << number << "\n";
cout << "Name : " << name << "\n";
cout << "Amount : " << amount << "\n";
cout << "Maturity Date : " << maturity_date << "\n";
cout << "Date of Opening : " << opening_date << "\n";
cout << "Duration in Months : " << no_of_months << "\n";
}
// member function definitions for 'LongTerm'
void LongTerm::LongTerm(int a, char* b, int c, char* d, char* e, int f, int g)
: DepositAccount(a,b,c,d,e)
{
no_of_years = f;
loan_taken = g;
}
void LongTerm::display()
{
cout << "Long Term Deposit Account Details --- \n";
cout << "Number : " << number << "\n";
cout << "Name : " << name << "\n";
cout << "Amount : " << amount << "\n";
cout << "Maturity Date : " << maturity_date << "\n";
cout << "Date of Opening : " << opening_date << "\n";
cout << "Duration in Years : " << no_of_years << "\n";
cout << "Loan Taken
: " << loan_taken << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
Account *top1;
// base class pointer

top1 = new SavingsAccount(1323, "Vikram", 10000);


top1->display();
delete top1;
cout << "\n";
cout << "Deposit Display using pointer";
cout <<
" of type base class 'Account' - \n";
top1 = new ShortTerm(17099, "Vilas", 25000, "12/05/02",
"12/02/02", 3);
top1->display();
delete top1;
cout << "\n";
top1 = new LongTerm(17169, "Vishwas", 30000, "15/03/04",
"15/03/02", 2, 1000);
top1->display();
delete top1;
cout << "\n";
Deposit *top2;

// base class pointer

cout << "Deposit Display using pointer";


cout <<
" of type second base class 'Deposit' - \n";
top2 = new ShortTerm(17099, "Vilas", 25000, "12/05/02",
"12/02/02", 3);
top2->display();
delete top2;
cout << "\n";
top2 = new LongTerm(17169, "Vishwas", 30000, "15/03/04",
"15/03/02", 2, 1000);
top2->display();
delete top2;
cout << "\n";
}
Example 17 : Program to illustrates Multi-level Inheritance with public derivation
#include <iostream.h>
#include <string.h>
/*----------------------Class Interfaces-------------------------*/
class employee
// base class

{
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();

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, "Stavan", 5, "New York");
A.display();
}
Example 18 : Program to illustrates Multi-level Inheritance with private derivation
#include <iostream.h>
#include <string.h>
/*----------------------Class Interfaces-------------------------*/
class employee
// base class
{
char name[10];
int empno;
public :
void set(int, char*);
void display();
};
class manager : private employee // Level - 1
{
int no_of_projects;
public:
void set(int, char*, int);
// overriding functions
void 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";
}

/*----------------------Class definitions ends here---------------*/


void main(void)
{
area_manager A;
A.set(1001, "Kush", 7, "New York");
A.display();
}
Example 19 : Program to illustrates Multi-level Inheritance protected derivation
#include <iostream.h>
#include <string.h>
/*----------------------Class Interfaces-------------------------*/
class employee
// base class
{
char name[10];
int empno;
public :
void set(int, char*);
void display();
};
class manager : protected employee // Level - 1
{
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)
{
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()
{

//protected members of the parent are directly available


cout << "Employee No. : " << empno << "\n";
cout << "Name
: " << name << "\n";
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)
{
//protected members of the grand-parent are directly available
empno = c;
strcpy(name, s);
//protected member of the parent is directly available
no_of_projects = p;
strcpy(location, l);
}
void area_manager::display()
{
//protected members of the grand-parent are directly available
cout << "Employee No. : " << empno << "\n";
cout << "Name
: " << name << "\n";
//protected member of the parent is directly available
cout << "No of Projects currently handling : ";
cout << no_of_projects << "\n";
cout << "Location : ";
cout << location << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
area_manager A;
A.set(1001, "Samarth", 7, "New York");
A.display();
}
Example 21 : Program to illustrates Multiple Inheritance
#include <iostream.h>
#include <string.h>
/*----------------------Class Interfaces-------------------------*/

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;
}

// member function definitions for 'word'


void word::set_word(char* a)
{
length = strlen(a);
for (int i=0; i<length; i++)
w[i].set_char(a[i]);
}
int word::get_length()
{
return length;
}
void word::display_word()
{
for (int i=0; i<length; i++)
w[i].display_char();
cout << " ";
}
// member function definitions for 'line'
void line::set_line(char** a)
{
for (int i=0; i<6; i++)
l[i].set_word(a[i]);
}
void line::display_line()
{
cout << "The Line of text is :\n";
for (int i=0; i<6; i++)
l[i].display_word();
cout << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
static char* a[6] = {"abc", "def", "ghij", "klmn", "op", "qr"};
line L;
L.set_line(a);
L.display_line();
}

Example 24 : Program Overriding Member Functions


#include <iostream.h>
#include <string.h>

/*----------------------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();
}

Example 25 : Pointers to Derived Class Objects


#include <iostream.h>
/*----------------------Class Interfaces-------------------------*/
class A
{
int a1;
// private data member
public :
int a2;
// public data member
void set(int);
void display();
};
class B : public A
{
int a3;
public :
int a4;

// private data member


// public data member

void set(int); // overriding functions


void display();
};
/*----------------------Class Implementations---------------------*/
// member function definitions for class A
void A::set(int x)
{
a1 = x;
}
void A::display()

{
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();
}

Example 26 : Pointers to Class Members


#include <iostream.h>
/*----------------------Class Interfaces-------------------------*/
class A
{

int a1;
public :
int a2;
A(int, int);
void display_a1a2();

// private data member


// public data member

};
/*----------------------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)();

// call to member function through pointer

Example 27 : Pointers to Class Members with Pointers to Objects


#include <iostream.h>
/*----------------------Class Interfaces-------------------------*/
class A
{
int a1;
// private data member
public :
int a2;
// public data member

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
}

Example 28 : Pointers to Objects


#include <iostream.h>
/*----------------------Class Interfaces-------------------------*/
class A
{
int a1;
// private data member
public :
int a2;
// public data member
void set_a1(int);
// public member functions
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";
}
/*----------------------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()'
}

Example 29 : Program to illustrates Virtual Function Empty in the Base Class


#include <iostream.h>
#include <string.h>
/*----------------------Class Interfaces-------------------------*/
class vehicle
// base class
{
protected :
char make[20];
int milage;
public :
vehicle(char*, int);
virtual void display() { }
};
class two_wheeler : public vehicle
{
char is_auto_clutch;
public :
two_wheeler(char*, int, char);
void display();

// empty virtual function

};
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();
}

Example 30 : Program to illustrates Pure Virtual Function


#include <iostream.h>
#include <string.h>
/*----------------------Class Interfaces-------------------------*/
class vehicle
// Abstract base class
{
protected :
char make[20];
int milage;
public :
vehicle(char*, int);
virtual void display() = 0;

// pure 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;
}
// 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();
}

Example 31 : Program to illustrates Single Inheritance by PUBLIC Derivation


#include <iostream.h>
/*----------------------Class Interfaces-------------------------*/

class A
{
int a1;
public :
int a2;
void set_a1(int);
void display_a1a2();

// private data member


// public data member
// public member functions

};
// 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;

// accessing public member function of the base directly


obj.set_a1(10);
obj.set_a2_and_b(20,30);
// accessing public member function of the base directly
obj.display_a1a2();
obj.display_b();
}
/*
a1 = 10
a2 = 20
b = 30
*/

Example 32 : Program to illustrates Single Inheritance by PRIVATE Derivation


#include <iostream.h>
/*----------------------Class Interfaces-------------------------*/
class A
{
int a1;
// private data member
public :
int a2;
// public data member
void set_a1a2(int, int);// public member functions
int get_a1();
};
// B as an extension of A
class B : private A
// private derivation
{
int b;
// private data memeber
public :
void set(int, int, int);// public member functions
void display();
};
/*----------------------Class Implementations---------------------*/
// member function definitions for class A
void A::set_a1a2(int x, int y)
{
a1 = x;
a2 = y;
}

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

Example 33 : Program to illustrates Single Inheritance by PROTECTED Derivation


#include <iostream.h>
/*----------------------Class Interfaces-------------------------*/
class A
{
int a1;
// private data member
protected :
int a2;
// protected data member
public :
int a3;
// public data member
void set_a1(int);
int get_a1();

// public member functions

};
// 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

cout<<endl<<" My object's address is "<<this<< endl;


cout<<this->i;
//another way to display data
}
};
void main( )
{
example e1;
e1.setdata(10);
e1.showdata( );
getch( );
}
Example 35 : Program to illustrates Virtual base class
#include<iostream.h>
#include<conio.h>
class student
{
protected :
int roll_number;
public:
void get_number(int a)
{
roll_number = a;
}
void put_number(void)
{
cout<<"Roll No. : "<<roll_number<<endl;
}
};
class test : virtual public student
{
protected :
float sem1, sem2;
public:
void get_marks (float s1, float s2)
{
sem1 = s1 ; sem2 = s2;
}
void put_marks(void)
{
cout<<"Marks obtained : "<<"\n"

<<"Sem1 = " <<sem1<<"\n"


<<"Sem2 = " <<sem2<<"\n";
}
};
class sports : public virtual student
{
protected :
float score;
public:
void get_score(float s)
{
score = s;
}
void put_score(void)
{
cout<<" Sports weight : "<<score<<"\n\n";
}
};
class result : public test, public sports
{
float total;
public :
void display(void);
};
void result :: display (void)
{
total = sem1 + sem2 + score;
put_number( );
put_marks( );
put_score( );
cout<<"Total Marks : "<<total<<"\n";
}
void main( )
{
result R1;
R1.get_number(123);
R1.get_marks(1200,1000);
R1.get_score(150);
R1.display( );
getch( );
}

Example 36 : Program to illustrates Dynamic binding through Virtual Functions


#include <iostream.h>
#include <string.h>
/*----------------------Class Interfaces-------------------------*/
class vehicle
// base class
{
char make[20];
int milage;
public :
vehicle(char*, int);
virtual void display();

// 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();
}

Example 37 : Program to Illustrates Virtual Function


#include<iostream.h>
#include<conio.h>
class Base
{
public :
void display( )
{
cout<<"\n Display Base ";
}
virtual void show( )
{

cout<<"\n Show Base ";


}
};
class Derived : public Base
{
public :
void display( )
{
cout<<"\n Display Derived ";
}
void show( )
{
cout<<"\n Show Derived ";
}
};
void main( )
{
Base B;
Derived D;
Base *bptr;
cout<<"\n bptr points to Base ";
bptr = &B;
bptr -> display( );
//calls Base version
bptr -> show( );
// calls Base version
cout<<" \n\n bptr points to Derived ";
bptr = &D;
bptr -> display( );
//calls Base version
bptr -> show( );
// calls Derived version
getch( );
}

Example 38 : Program to Illustrates Virtual Function


#include<iostream.h>
#include<conio.h>
class Base
{
public :
void display( )
{

cout<<"\n Display Base ";


}
virtual void show( )
{
cout<<"\n Show Base ";
}
};
class Derived : public Base
{
public :
void display( )
{
cout<<"\n Display Derived ";
}
void show( )
{
cout<<"\n Show Derived ";
}
};
void main( )
{
Base B;
Derived D;
Base *bptr;
cout<<"\n bptr points to Base ";
bptr = &B;
bptr -> display( );
//calls Base version
bptr -> show( );
// calls Base version
cout<<" \n\n bptr points to Derived ";
bptr = &D;
bptr -> display( );
//calls Base version
bptr -> show( );
// calls Derived version
getch( );
}

Example 39 : Program : constructor, destructor in single inheritance


#include<iostream.h>
#include<conio.h>
class base
{

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( );
}

Das könnte Ihnen auch gefallen