C++ Program For Inheritance
Master C++ with Real-time Projects and Kickstart Your Career Start Now!!
Program 1
// Program for Inheritance
#include<iostream>
#define clrscr() system("cls")
using namespace std;
class Test
{
private:
int x_pri;
protected:
int y_pro;
public:
int z_pub;
Test()
{
x_pri=100;
y_pro=200;
z_pub=300;
}
};
void main()
{
Test T;
cout<<T.x_pri;
cout<<T.y_pro;
cout<<T.z_pub;
}Program 2
// Program for Inheritance
#include<iostream>
#define clrscr() system("cls")
using namespace std;
class Base
{
private:
int x_pri; // Not Inherit
protected:
int y_pro;
public:
int z_pub;
};
class Derived:protected Base // All Inherited members are private
{
public:
void display()
{
y_pro=100;
z_pub=200;
cout<<y_pro;
cout<<z_pub;
}
};
class Derived1:public Derived
{
public:
void display1()
{
y_pro=100;
z_pub=200;
cout<<y_pro;
cout<<z_pub;
}
};
int main()
{
Derived D;
D.display();
return 0;
}Program 3
// Program for Inheritance
#include<iostream>
#define clrscr() system("cls")
using namespace std;
class ATM1
{
protected:
int pin,amount;
public:
ATM1()
{
pin=1234;
amount=0;
}
void withd(int amt);
};
void ATM1::withd(int amt)
{
if(pin==1234)
{
if(amt>amount)
cout<<"Insufficient balance";
else
{
amount-=amt;
cout<<"Balance: "<<amount;
}
}
}
class ATM2:public ATM1
{
public:
void deposit(int amt);
};
void ATM2::deposit(int amt)
{
if(pin==1234)
{
amount+=amt;
cout<<"Balance: "<<amount;
}
}
int main()
{
ATM2 A;
clrscr();
A.deposit(15000);
cout<<"------------------";
A.withd(3000);
}
Did we exceed your expectations?
If Yes, share your valuable feedback on Google

