[ Home Page ] [ Back ] [ Content of Contributions ] [ Movement: Green Leaf World ]
In this article, single inheritance has been demonstrated.
Syntax:
Syntax:
class
DerivedClassName:
AccessSpecifier BaseClassName
{
};
Program Reference: Cpp_Programs/ Day_06/ Inheritance_Public_01.cpp
#include<stdlib.h>
#include<iostream>
class A
{
private:
int a;
protected:
int b;
public:
int c;
void reada(void)
{
std::cout<<"\nInitialize A: \n\tPlease Enter two Values: ";
std::cin>>a>>b;
c=a*10+b;
std::cout<<"Returning From Class A....";
}
void writea(void)
{
std::cout<<"\nDisplay A:\n\t A = "<<a<<"\tB= "<<b<<"\tC= "<<c<<"\nReturning From A....";
}
};
class B: public A
{
private:
int a;
protected:
int b;
public:
int c;
void readb(void)
{
std::cout<<"\nInitialize B: \n\tPlease Enter two Values: ";
std::cin>>a>>b;
c=a*10+b;
std::cout<<"\nReturning From B";
}
void writeb(void)
{
std::cout<<"\nDisplay B: \n\tA = "<<a<<"\tB= "<<b<<"\tC= "<<c; std::cout<<"\nReturning From B....\n";
}
};
int main(void)
{
B obb;
system("clear"); //TO CLEAR THE SCREEN [use clrscr(); in windows]
std::cout<<"\n\t\t\tDemonstration of Inheritance(Public)\n";
obb.reada();
obb.writea();
obb.writeb();
obb.readb();
obb.writea();
obb.writeb();
std::cout<<"\n\nAssigning A::C=1 From B";
obb.c=1;
obb.writea();
obb.writeb();
std:: cout<<"\n\nEnd of the Program....\n\n\n";
}
Output
Demonstration of Inheritance(Public)
Initialize A:
Please Enter two Values: 10 20
Returning From Class A....
Display A:
A = 10 B= 20 C= 120
Returning From A....
Display B:
A = -1217048576 B= 134515211 C= -1218256896
Returning From B....
Initialize B:
Please Enter two Values: 11 22
Returning From B
Display A:
A = 10 B= 20 C= 120
Returning From A....
Display B:
A = 11 B= 22 C= 132
Returning From B....
Assigning A::C=1 From B
Display A:
A = 10 B= 20 C= 120
Returning From A....
Display B:
A = 11 B= 22 C= 1
Returning From B....
End of the Program....
Program Reference: Cpp_Programs/ Day_06/ Inheritance_Public_02.cpp
#include<stdlib.h>
#include <iostream>
class A
{
private:
int a;
public:
int b;
void inita(void) {std::cout<<"\nInitialize A \n"; a=10;b=20; std::cout<<"Returning From A....";}
void writea(void){std::cout<<"\nDisplay A: \n\tA = "<<a<<"\tB= "<<b; std::cout<<"\nReturning From A....";}
};
class B: public A
{
private:
int x;
public:
int y;
void initb(void) {std::cout<<"\nInitialize B....\n"; x=b; y=30; std::cout<<"Returning From B....";}
void writeb(void){std::cout<<"\nDisplay B: \n\tX = "<<x<<"\tY= "<<y<<"\tB= "<<b; std::cout<<"\nReturning From B....\n\n";}
};
int main(void)
{
B obb;
system("clear"); //TO CLEAR THE SCREEN [use clrscr(); in windows]
std::cout<<"\n\t\t\tDemonstration of Inheritance(Public)\n";
obb.inita();
obb.writea();
obb.writeb();
obb.initb();
obb.writea();
obb.writeb();
std::cout<<"\n\nSet B::Y=1 From B";
obb.y=1;
//obb.a=0;
// obb.x=0;
obb.writea();
obb.writeb();
std::cout<<"\n\nSet A::B=1 From B";
obb.b=1;
obb.writea();
obb.writeb();
std:: cout<<"\n\nEnd of the Program....\n\n\n";
}
Output
Demonstration of Inheritance(Public)
Initialize A
Returning From A....
Display A:
A = 10 B= 20
Returning From A....
Display B:
X = 134515019 Y= -1218433024 B= 20
Returning From B....
Initialize B....
Returning From B....
Display A:
A = 10 B= 20
Returning From A....
Display B:
X = 20 Y= 30 B= 20
Returning From B....
Set B::Y=1 From B
Display A:
A = 10 B= 20
Returning From A....
Display B:
X = 20 Y= 1 B= 20
Returning From B....
Set A::B=1 From B
Display A:
A = 10 B= 1
Returning From A....
Display B:
X = 20 Y= 1 B= 1
Returning From B....
End of the Program....
Program Reference: Cpp_Programs/ Day_06/ Inheritance_Public_03.cpp
#include<stdlib.h>
#include <iostream>
class A
{
private:
int a;
protected:
int b;
public:
int c;
void inita(void) {std::cout<<"\nInitialize A "; a=1;b=2;c=3; std::cout<<"\nReturning From A.... ";}
void writea(void){std::cout<<"\nDisplay Class A: \n\tA = "<<a<<"\tB= "<<b<<"\tC= "<<c; std::cout<<"\nReturning From A.... ";}
};
class B: public A
{
private:
int x;
protected:
int y;
public:
int z;
void initb(void) {std::cout<<"\nInitialize B ";x=10;y=20;z=30; c=40; std::cout<<"\nReturning From B.... ";}
void writeb(void){std::cout<<"\nDisplay Class B: \n\tX = "<<x<<"\tY= "<<y<<"\tZ= "<<z<<"\tC= "<<c; std::cout<<"\nReturning From B.... \n\n";}
};
int main(void)
{
B obb;
system("clear"); //TO CLEAR THE SCREEN [use clrscr(); in windows]
std::cout<<"\n\t\t\tDemonstration of Public Inheritance\n\t[Private: Not Inheritable\tProtected->Protected\tPublic->Public]\n";
obb.inita();
obb.writea();
obb.writeb();
obb.initb();
obb.writea();
obb.writeb();
std::cout<<"\n\nAssigning A::C=1 From B";
obb.c=1;
obb.writea();
obb.writeb();
std:: cout<<"\n\nEnd of the Program....\n\n\n";
}
Output
Demonstration of Public Inheritance
[Private: Not Inheritable Protected->Protected Public->Public]
Initialize A
Returning From A....
Display Class A:
A = 1 B= 2 C= 3
Returning From A....
Display Class B:
X = -1216704512 Y= 134515083 Z= -1217912832 C= 3
Returning From B....
Initialize B
Returning From B....
Display Class A:
A = 1 B= 2 C= 40
Returning From A....
Display Class B:
X = 10 Y= 20 Z= 30 C= 40
Returning From B....
Assigning A::C=1 From B
Display Class A:
A = 1 B= 2 C= 1
Returning From A....
Display Class B:
X = 10 Y= 20 Z= 30 C= 1
Returning From B....
End of the Program....
Program Reference: Cpp_Programs/ Day_06/ Inheritance_Protected.cpp
#include<stdlib.h>
#include <iostream>
using namespace std;
class A
{
private:
int a;
protected:
int b;
public:
int c;
void inita(void);
void writea(void){std::cout<<"\n\tDisplay Class A: \n\t\tA= "<<a<<"\tB= "<<b<<"\tC= "<<c<<"\n\tReturning From Class A....\n";}
};
void A::inita(void)
{std::cout<<"\n\tInitializing Class A\n\t"; a=1;b=2;c=3;std::cout<<"Returning From Class A...."; }
class B: protected A
{
private:
int x;
protected:
int y;
public:
int z;
void initb(void) {std::cout<<"\nInitializing Class B";inita();writea(); x=10; y=20; z=30; b=11; c=22; std::cout<<"Returning From Class B....\n\n";}
void writeb(void){std::cout<<"\nDisplay Class B:";writea(); std::cout<<"\tX = "<<x<<"\tY= "<<y<<"\tZ= "<<z<<"\tC= "<<c;std::cout<<"\nReturning From B....\n\n";}
};
int main(void)
{
B obb;
system("clear"); //TO CLEAR THE SCREEN [use clrscr(); in windows]
cout<<"\n\t\t\tDemonstration of Protected Inheritance\n\t[Private: Not Inheritable\tProtected->Protected\tPublic->Protected]\n";
cout<<"\nInitially: ";
obb.writeb();
obb.initb();
obb.writeb();
// obb.c=0; //NOT ACCESSIBLE
std:: cout<<"\n\nEnd of the Program....\n\n\n";
}
Output
[Private: Not Inheritable Protected->Protected Public->Public]
Initially:
Display Class B:
Display Class A:
A= -1074907844 B= -1219234643 C= -1217686588
Returning From Class A....
X = -1216479232 Y= 134515067 Z= -1217687552 C= -1217686588
Returning From B....
Initializing Class B
Initializing Class A
Returning From Class A....
Display Class A:
A= 1 B= 2 C= 3
Returning From Class A....
Returning From Class B....
Display Class B:
Display Class A:
A= 1 B= 11 C= 22
Returning From Class A....
X = 10 Y= 20 Z= 30 C= 22
Returning From B....
End of the Program....
Program Reference: Cpp_Programs/ Day_06/ Inheritance_Private.cpp
#include <iostream>
class A
{
private:
int a;
protected:
int b;
public:
int c;
void inita(void)
{
std::cout<<"\n\tInitializing Class A\n\t";
a=1;
b=2;
c=3;
std::cout<<"Returning From Class A....";
}
void writea(void)
{
std::cout<<"\n\tDisplay Class A: \n\t\tA= "<<a<<"\tB= "<<b<<"\tC= "<<c;
std::cout<<"\n\tReturning From Class A....\n";
}
};
class B: private A
{
private:
int x;
protected:
int y;
public:
int z;
void initb(void)
{
std::cout<<"\nInitializing Class B";
inita();writea();
x=10;y=20;z=30;c=40;
std::cout<<"Returning From Class B....\n\n";
}
void writeb(void){std::cout<<"\nDisplay Class B:";writea(); std::cout<<"\tX = "<<x<<"\tY= "<<y<<"\tZ= "<<z<<"\tC= "<<c;std::cout<<"\nReturning From B....\n\n";}
};
int main(void)
{
B obb;
system("clear"); //TO CLEAR THE SCREEN [use clrscr(); in windows]
std::cout<<"\n\t\t\tDemonstration of Private Inheritance\n\t[Private: Not Inheritable\tProtected->Private\tPublic->Private]\n";
std::cout<<"\nInitially: ";
obb.writeb();
obb.initb();
obb.writeb();
std:: cout<<"\n\nEnd of the Program....\n\n\n";
}
Output
Demonstration of Private Inheritance
[Private: Not Inheritable Protected->Protected Public->Public]
Initially:
Display Class B:
Display Class A:
A= -1074519076 B= -1220090707 C= -1218542652
Returning From Class A....
X = -1217335296 Y= 134515067 Z= -1218543616 C= -1218542652
Returning From B....
Initializing Class B
Initializing Class A
Returning From Class A....
Display Class A:
A= 1 B= 2 C= 3
Returning From Class A....
Returning From Class B....
Display Class B:
Display Class A:
A= 1 B= 2 C= 40
Returning From Class A....
X = 10 Y= 20 Z= 30 C= 40
Returning From B....
End of the Program....
No comments:
Post a Comment