Inheritance

Inheritance means one class inherits both attributes and methods from another class. Inheritance enables you to reuse and extend existing classes without copy & paste the code or re-invent the wheel.

  • A class that other classes inherit from is called base class.
  • A class that inherits from other class called derived class.

Following is the code in c++ for understanding of inheritance:

#include<iostream>
#include<conio.h>
using namespace std;
class book
{
int bookid;
char name[30];
float price;
public:
void in()
{
cout<<“book id:”<<endl;
cin>>bookid;
cout<<“Enter name:”<<endl;
cin>>name;
cout<<“Enter price:”<<endl;
cin>>price;
}
void show()
{
cout<<bookid<<name<<price;
}
};
class writer:public book
{
char name[30];
int num;
int len[5];
public:
void input()
{
cout<<“Enter name:”<<endl;
cin>>name;
cout<<“number of books:”<<endl;
cin>>num;
}
void out()
{
cout<<name<<num;
}
};
void main()
{
int i;
writer w;
w.input();
w.out();
for(i=0;i<5;i++)
{    w.in();
w.show();
}
getch();
}

Print Friendly

Share this with your friends

One Comment to “Inheritance”

  1. What a pleasure to find someone who iditfenies the issues so clearly

Leave a Reply

Your email address will not be published. Required fields are marked *