Single Inheritance: The inheritance in which the properties of parent class is acquired by single(only one) child class is called single inheritance. i.e only one child class can be formed in it.
syntax: class Parent
{
code related to parent class
}
class Child : visibility mode(public,private,protected) Parent
{
code specific to child class
};
Example:
1 Create class named Person with data members name,address,phone number.Use suitable member function to read the data members . Create a derived class or child class named Student which has data member roll number.Take the input and display the details.(Single Inheritance)
i. Using Public Visibility Mode
#include <iostream.h>
#include <conio.h>
class Person
{
private: char name[20];
protected: char address[20];
char phone[20];
public:void inputP();
void displayP();
};
void Person::inputP()
{
cout<<"Enter name:";
cin>>name;
cout<<"Enter address:";
cin>>address;
cout<<"Enter phone number:";
cin>>phone;
}
void Person::displayP()
{
cout<<"Name:"<<name<<endl<<"Address:"<<address<<endl<<"Phone:"<<phone<<endl;
}
class Student:public Person
{
protected: int roll;
public: void inputS();
void displayS();
};
void Student::inputS()
{
cout<<"Enter roll of student:";
cin>>roll;
}
void Student::displayS()
{
cout<<"Roll:"<<roll<<endl;
}
void main()
{
Student s;
s.inputP();
cout<<endl;
s.inputS();
cout<<endl;
s.displayP();
cout<<endl;
s.displayS();
getch();
}
Output:
ii. Using protected visibility mode
#include <iostream.h>
#include <conio.h>
class Person
{
private: char name[20];
protected: char address[20];
char phone[20];
public: void inputP();
void displayP();
};
void Person::inputP()
{
cout<<"Enter name:";
cin>>name;
cout<<"Enter address:";
cin>>address;
cout<<"Enter phone number:";
cin>>phone;
}
void Person::displayP()
{
cout<<"Name:"<<name<<endl<<"Address:"<<address<<endl<<"Phone:"<<phone<<endl;
}
class Student:protected Person
{
protected:int roll;
public:void inputS();
void displayS();
};
void Student::inputS()
{
cout<<"Enter roll of student:";
cin>>roll;
}
void Student::displayS()
{
cout<<"Roll:"<<roll<<endl;
}
void main()
{
Person p; //protected and public members of Person class are inherited by Student class protected members
Student s;
p.inputP();
cout<<endl;
s.inputS();
cout<<endl;
p.displayP();
cout<<endl;
s.displayS();
getch();
}
Output:
iii.Using private visibility mode
#include <iostream.h>
#include <conio.h>
class Person
{
private: char name[20];
protected: char address[20];
char phone[20];
public: void inputP();
void displayP();
};
void Person::inputP()
{
cout<<"Enter name";
cin>>name;
cout<<"Enter address";
cin>>address;
cout<<"Enter phone number";
cin>>phone;
}
void Person::displayP()
{
cout<<"Name:"<<name<<endl<<"Address:"<<address<<endl<<"Phone:"<<phone<<endl;
}
class Student:private Person
{
protected: int roll;
public: void inputS();
void displayS();
};
void Student::inputS()
{
Person p;
p.inputP();
cout<<"Enter roll of student:";
cin>>roll;
}
void Student::displayS()
{
Person p;
p.displayP();
cout<<"Roll:"<<roll<<endl;
}
void main()
{
Student s;
s.inputS();
s.displayS();
getch();
}
Output:

2 . Create a class called Rectangle with data member to represent length, breadth and area.
Use appropriate member functions to read length and breadth from user to calculate area of the rectangle.Then create a derived class called Box from the class Rectangle.
Use appropriate member function to read height and to calculate the volume of the box.
i. Using Public Mode (Declared Inside the class body)
#include<iostream.h>
#include<conio.h>
class Rectangle
{
protected: int length;
int breadth;
int Area;
public: void inputA()
{
cout<<"Enter length:";
cin>>length;
cout<<"Enter breadth:";
cin>>breadth;
}
void displayA()
{
cout<<"Length:"<<length<<endl<<"Breadth:"<<breadth<<endl;
cout<<endl;
}
void calculateArea()
{
Area=length*breadth;
cout<<"Area:"<<Area<<endl;
}
};
class Box :public Rectangle
{
int length;
int breadth;
int height;
int volume;
public:
void inputV()
{
cout<<"Enter length:";
cin>>length;
cout<<"Enter breadth:";
cin>>breadth;
cout<<"Enter height:";
cin>>height;
}
void displayV()
{
cout<<"Length:"<<length<<endl<<"Breadth:"<<breadth<<endl<<"Height:"<<height<<endl;
cout<<endl;
}
void calculatevolume()
{
volume=length*breadth*height;
cout<<"Volume:"<<volume<<endl;
}
};
void main()
{
Box B;
cout<<"Details for Area"<<endl;;
B.inputA();
B.displayA();
B.calculateArea();
cout<<endl;
cout<<"Details for Volume"<<endl;
B.inputV();
B.displayV();
B.calculatevolume();
getch();
}
Declared Outside the class Body
#include<iostream.h>
#include<conio.h>
class Rectangle
{
protected: int length;
int breadth;
int Area;
public: void inputA();
void displayA();
void calculateArea();
};
void Rectangle::inputA()
{
cout<<"Enter length:";
cin>>length;
cout<<"Enter breadth:";
cin>>breadth;
}
void Rectangle:: displayA()
{
cout<<"Length:"<<length<<endl<<"Breadth:"<<breadth<<endl;
cout<<endl;
}
void Rectangle::calculateArea()
{
Area=length*breadth;
cout<<"Area:"<<Area<<endl;
}
class Box :public Rectangle
{
int length;
int breadth;
int height;
int volume;
public:
void inputV();
void displayV();
void calculatevolume();
};
void Box::inputV()
{
cout<<"Enter length:";
cin>>length;
cout<<"Enter breadth:";
cin>>breadth;
cout<<"Enter height:";
cin>>height;
}
void Box::displayV()
{
cout<<"Length:"<<length<<endl<<"Breadth:"<<breadth<<endl<<"Height:"<<height<<endl;
cout<<endl;
}
void Box::calculatevolume()
{
volume=length*breadth*height;
cout<<"Volume:"<<volume<<endl;
}
void main()
{
Box B;
cout<<"Details for Area"<<endl;;
B.inputA();
B.displayA();
B.calculateArea();
cout<<endl;
cout<<"Details for Volume"<<endl;
B.inputV();
B.displayV();
B.calculatevolume();
getch();
}
ii. Using Protected Mode(declared outside class body)
#include<iostream.h>
#include<conio.h>
class Rectangle
{
protected: int length;
int breadth;
int Area;
public: void inputA();
void displayA();
void calculateArea();
};
void Rectangle::inputA()
{
cout<<"Enter length:";
cin>>length;
cout<<"Enter breadth:";
cin>>breadth;
}
void Rectangle:: displayA()
{
cout<<"Length:"<<length<<endl<<"Breadth:"<<breadth<<endl;
cout<<endl;
}
void Rectangle::calculateArea()
{
Area=length*breadth;
cout<<"Area:"<<Area<<endl;
}
class Box :protected Rectangle
{
int length;
int breadth;
int height;
int volume;
public:
void inputV();
void displayV();
void calculatevolume();
};
void Box::inputV()
{
cout<<"Enter length:";
cin>>length;
cout<<"Enter breadth:";
cin>>breadth;
cout<<"Enter height:";
cin>>height;
}
void Box::displayV()
{
cout<<"Length:"<<length<<endl<<"Breadth:"<<breadth<<endl<<"Height:"<<height<<endl;
cout<<endl;
}
void Box::calculatevolume()
{
volume=length*breadth*height;
cout<<"Volume:"<<volume<<endl;
}
void main()
{
Rectangle R;
Box B;
cout<<"Details for Area"<<endl;;
R.inputA();
R.displayA();
R.calculateArea();
cout<<endl;
cout<<"Details for Volume"<<endl;
B.inputV();
B.displayV();
B.calculatevolume();
getch();
}
Output:

iii. Using private mode
#include<iostream.h>
#include<conio.h>
class Rectangle
{
protected: int length;
int breadth;
int Area;
public: void inputA();
void displayA();
void calculateArea();
};
void Rectangle::inputA()
{
cout<<"Details for area"<<endl;
cout<<"Enter length:";
cin>>length;
cout<<"Enter breadth:";
cin>>breadth;
}
void Rectangle:: displayA()
{
cout<<"Length:"<<length<<endl<<"Breadth:"<<breadth<<endl;
cout<<endl;
}
void Rectangle::calculateArea()
{
Area=length*breadth;
cout<<"Area:"<<Area<<endl;
}
class Box :protected Rectangle
{
int length;
int breadth;
int height;
int volume;
public:
void inputV();
void displayV();
void calculatevolume();
};
void Box::inputV()
{
Rectangle R;
R.inputA();
cout<<endl;
cout<<"Details for Volume"<<endl;
cout<<"Enter length:";
cin>>length;
cout<<"Enter breadth:";
cin>>breadth;
cout<<"Enter height:";
cin>>height;
}
void Box::displayV()
{
Rectangle R;
R.displayA();
cout<<endl;
cout<<"Length:"<<length<<endl<<"Breadth:"<<breadth<<endl<<"Height:"<<height<<endl;
cout<<endl;
}
void Box::calculatevolume()
{
Rectangle R;
R.calculateArea();
volume=length*breadth*height;
cout<<"Volume:"<<volume<<endl;
}
void main()
{
Box B;
B.inputV();
B.displayV();
B.calculatevolume();
getch();
}
Output:
Comments
Post a Comment