Mechanism, where derived classes derived from other
derived classes is known as Multi-Level
inheritance.
Example : Multi-Level Inheritance using Parameterize
constructor
#include
<iostream.h>
class
Base
{
protected:
int
a;
public:
Base()
{
a=0;
}
Base(int
x)
{
a=x;
}
void
Disp()
{
cout<<"\n\tFirst
Number : "<<a;
}
};
class
Derived_1:public Base
{
protected:
int
b;
public:
Derived_1(int
n,int x):Base(n)
{
b=x;
}
void
Print()
{
cout<<"\n\tSecond
Number : "<<b;
}
};
class
Derived_2:public Derived_1
{
protected:
int
c;
public:
Derived_2(int
n,int x,int n1):Derived_1(n,x)
{
c=n1;
}
void
Show()
{
cout<<"\n\tThird
Number : "<<c;
cout<<"\n\n\t\tSum
= "<<a+b+c;
}
};
int main()
{
Derived_2 Der(50,45,35);
Der.Disp();
Der.Print();
Der.Show();
return
0;
}
Example : Deposit class derived from Bank class and where
Withdra class derived from Deposit.
#include
<iostream.h>
class
Bank
{
protected:
char
name[20][20];
int
ac_no[20];
float
balance[20];
int
N;
public:
Bank(){}
void
GetData();
void
DispData();
};
void
Bank::GetData()
{
cout<<"\n\tEnter
Size? [Below 20] ";
cin>>N;
for(int i=0;i<N;i++)
{
cout<<"\n\tA/c
No: ";
cin>>ac_no[i];
cin.ignore();
cout<<"\n\tAccount
Holder Name : ";
cin.getline(name[i],20);
cout<<"\n\tCurrent
Balance : ";
cin>>balance[i];
}
}
void Bank::DispData()
{
cout<<"\n\tA/C
No\tName\t\tBalance";
cout<<"\n\t==============================";
for(int i=0;i<N;i++)
{
cout<<"\n\t"<<ac_no[i]<<"\t"<<name[i]<<"\t\t"<<balance[i];
}
cout<<"\n\t==============================";
}
class
Deposit:public Bank
{
protected:
float
damt;
public:
Deposit()
{
damt=0;
}
void
Debit(int,float);
};
void Deposit::Debit(int ac,float dm)
{
damt=dm;
int i,flag=0;
for(i=0;i<N;i++)
{
if(ac_no[i]==ac)
{
flag=1;
break;
}
}
if(flag==1)
{
balance[i]=balance[i]+damt;
cout<<"\n\tDeposited,
Current Balance "<<balance[i];
}
else
{
cout<<"\n\tInvalid
Account No. ";
}
}
class
Withdraw:public Deposit
{
protected:
float
wamt;
public:
Withdraw()
{
wamt=0;
}
void
Credit(int,float);
};
void
Withdraw::Credit(int ac,float wm)
{
wamt=wm;
int i,flag=0;
for(i=0;i<N;i++)
{
if(ac_no[i]==ac)
{
flag=1;
break;
}
}
if(flag==1)
{
if(balance[i]<wamt)
{
cout<<"\n\tNot
Enough Balance";
}
else
{
balance[i]=balance[i]-wamt;
cout<<"\n\tCurrent
Balance "<<balance[i];
}
}
else
{
cout<<"\n\tInvalid
Account No. ";
}
}
int main()
{
float
amt;
Withdraw
W;
W.GetData();
int
c=0,ac;
while(c!=4)
{
cout<<"\n\n\t1.
Deposit";
cout<<"\n\t2.
Withdraw";
cout<<"\n\t3.
Display";
cout<<"\n\t4.
Exit";
cout<<"\n\tChoice? ";
cin>>c;
switch(c)
{
case
1:
cout<<"\n\tAcoount
No? ";
cin>>ac;
cout<<"\n\tAmount?
";
cin>>amt;
W.Debit(ac,amt);
break;
case
2:
cout<<"\n\tAcoount
No? ";
cin>>ac;
cout<<"\n\tAmount?
";
cin>>amt;
W.Credit(ac,amt);
break;
case
3:
cout<<"\n\tCurrent
Statement : ";
W.DispData();
break;
case
4:
break;
default:
cout<<"\n\tInvalid
Option ";
}
}
return
0;
}
Example : SoftDrink
class Derived from Sold and Sold class derived from SoftName class.
#include
<iostream.h>
class
SoftName
{
protected:
char
name[20][20];
int
size;
public:
void
GetName();
};
void
SoftName::GetName()
{
cout<<"\n\tNumber
of Products : ";
cin>>size;
cout<<"\n\tName
of the Softdrink:";
for(int i=0;i<size;i++)
{
cout<<"\n\t";
cin>>name[i];
}
}
class
Sold:public SoftName
{
protected:
int
bot[20][20],Syear,Nyear;
public:
void
GetBottle();
};
void
Sold::GetBottle()
{
int
z;
cout<<"\n\tStarting
Year : ";
cin>>Syear;
cout<<"\n\tNumbers
of Year:";
cin>>Nyear;
for(int j=0;j<Nyear;j++)
{
cout<<"\n\tYear=
"<<Syear+j;
for(int k=0;k<size;k++)
{
cout<<"\n\tBottles
for Product= "<<name[k]<<" ";
cin>>bot[j][k];
}
}
}
class
SoftDrink:public Sold
{
public:
void
display();
};
void
SoftDrink::display()
{
for(int j=0;j<Nyear;j++)
{
cout<<"\n\t\t\tYear=
"<<Syear+j;
for(int k=0;k<size;k++)
{
cout<<"\n\tProduct=
"<<name[k]<<" "<<bot[j][k];
}
}
}
int
main()
{
SoftDrink
S;
S.GetName();
S.GetBottle();
S.display();
return
0;
}
No comments:
Post a Comment