Home »
» Multilevel inheritance in C++
Multilevel inheritance:
- Multilevel inheritance has derived from the class itself and
derives the subclass further.
- It means one base class derived from intermediate
class, that intermediate class derived the derived class.
Syntax:
class a
{
//Base class
}
class b: class a
{
//Intermediate class
}
class c: class b
{
//Derived class
}
Object creation:
last_class_name object_name;
object_name.function(parameter list);
Example:
#include<iostream.h>
#include<conio.h>
class get_data //Base class
{
public:
int pin;
void get_value()
{
cout<<"\n\t
Welcome to XXX Bank ";
cout<<"\n\n
Enter your pin number: ";
cin>>pin;
}
};
class compare: public get_data //Intermediate class
{
public:
void compare_data()
{
if (pin==1234)
{
cout<<"\n Your pin number is valid. ";
}
else
{
cout<<"\n Your pin number is invalid. ";
}
}
};
class cash_withdrawal: public compare //Derived class
{
public:
int amt;
int cash;
void get_cash()
{
amt=2000;
if (pin==1234)
{
cout<<"\n\n
Enter an amount for cash withdrawal: ";
cin>>cash;
amt=amt-cash;
cout<<"\n
Your current balance : "<<amt;
}
else
{
cout<<"\n Please enter correct pin
number...";
}
}
};
void main()
{
clrscr();
cash_withdrawal c;
c.get_value();
c.compare_data();
c.get_cash();
getch();
}
Output:
If condition:
Else condition:
No comments:
Post a Comment