EX.
NO:3
SINGLE INHERITANCE
DATE:
AIM
To write a program using Single inheritance.
SINGLE INHERITANCE
ALGORITHM FOR MAIN() FUNCTION
Step1: Start the program execution.
Step2: Create the object x for class abc.
Step3: call the member function getdata() to get the values.
Step4: call the member function display() to display the values.
Step5: Stop the program execution.
ALGORITHM FOR GETDATA() FUNCTION:
Step1: Get the values name and age.
Step2: Get the values height and weight.
ALGORITHM FOR SHOW() FUNCTION:
Step1: Display the values of name and age.
Step2: Display the values of height and weight.
7
SINGLE INHERITANCE
#include<iostream.h>
#include<constream.h>
class ABC
{
protected:
char name[15];
int age;
};
class abc:public ABC
{
float height;
float weight;
public:
voidgetdata()
{
cout<<"\n Enter name and age:";
cin>>name>>age;
cout<<"\n Enter height and weight:";
cin>>height>>weight;
}
void show()
{
cout<<"\n name:"<<name<<"\n age:"<<age<<"year";
cout<<"\n height:"<<height<<"feets"<<"\n weight"<<weight<<"kg";
}};
void main()
{
clrscr();
abc x;
[Link]();
[Link]();
getch();
}
OUTPUT
SINGLE INHERITANCE
***************************
Enter name and age : bhuvana 18
Enter height and weight : 59.45 50
Name : Bhuvana
Age : 18year
Height : 59.450001feets
Weight : 50kg
8
MULTIPLE INHERITANCE
AIM
To write a program using multiple inheritance.
ALGORITHM FOR MAIN() FUNCTION
Step1: Start the program execution.
Step2: Create the object x for class for E.
Step3: call the member function getdata() to get the values.
Step4: call the member function display().
Step5: Stop the program execution.
ALGORITHM FOR GETDATA() FUNCTION
Step1: Get the values a, b, c, d and e.
ALGORITHM FOR SHOW() FUNCTION
Step1: Display the values of a, b, c, d and e.
9
MULTIPLE INHERITANCE
#include<iostream.h>
#include<constream.h>
class A{protected:int a;};
class B{protected:int b;};
class C{protected:int c;};
class D{protected:int d;};
//class E publicA,publicB,publicC,public D
class E:public A,B,C,D
{
int e;
public:
void getdata()
{
cout<<"\ Enter values of a,b,c, d&e\n:";
cin>>a>>b>>c>>d>>e;
}
void showdata()
{
cout<<”\t\tThe a, b, c, d and e values are:\n”;
cout<<"\n a="<<a<<”\n”<<"b="<<b<<”\n”<<"c="<<c<<”\n”;
cout<<"d="<<d<<”\n”<<"e="<<e;
}};
void main()
{
clrscr(); E x;
[Link]();
[Link]();
getch();
}
OUTPUT
Enter values of a,b,c, d & e: 1 2 3 4 5
The a, b, c, d and e values are:
a=1
b=2
c=3
d=4
e=5
10
HIERARCHICAL INHERITANCE
AIM
To write a program using hierarchical inheritance.
ALGORITHM FOR MAIN() FUNCTION
Step1: Start the program execution.
Step2: Create the object x for class B and y for C.
Step3: call the member function getdata() to get the values.
Step4: call the member function display().
Step5: Stop the program execution.
ALGORITHM FOR GETDATA() FUNCTION
Step1: Get the values a, b, e and f.
ALGORITHM FOR SHOW() FUNCTION
Step1: Display the values of a, b, e and f.
11
HIERARCHICAL INHERITANCE
#include<iostream.h>
#include<conio.h>
class A
{
protected :
int a,b;
};
class B : public A
{
int c,d;
public:
void getdata();
void display();
};
void B::getdata()
{
cout<<”Enter a,b,c &d values”;
cin>>a>>b>>c>>d;
}
void b::display()
{
cout<<”the a,b,c and d values”;
cout<<”a=”<<a;
cout<<”b=”<<b;
cout<<”c=”<<c;
cout<<”d=”<<d;
}
class C:public A
{
int e,f;
public:
void getdata()
void display();
};
void C::getdata()
{
cout<<”Enter a,b,e & f values”;
cin>>a>>b>>e>>f;
}
12
void C::display()
{
cout<<”the a,b,e & f values”;
cout<<”a=”<<a;
cout<<”b=”<<b;
cout<<”e=”<<e;
cout<<”f=”<<f;
void main()
{
clrscr();
B x;
[Link]();
[Link]();
C y;
[Link]();
[Link]();
getch();
}
OUTPUT
Enter a,b,c & d values 2 2 4 5
a=2
b=2
c=4
d=5
Enter a,b,e & f values 2 3 4 5
a=2
b=2
c=4
d=5
RESULT
Thus, the c++ program was verified and executed successfully.
13