{ads}

6/recent/ticker-posts

Inheritance in c++


Introduction to code reuse:

All programming languages provide a block of code to use again and again. However, the function provides limited reuse. In object-oriented programming, reusability is the main feature. we can reuse the code by following methods.

  • Containership
  • Inheritance

Containership:

C++ allows a class to contain another class object. This activity establishes 'has a' relationship among objects. This 'has a ' relationship is also called container ship. The main advantage of containership is that it excellently implemented the encapsulation feature of oop. The reason being, that the contained objects are only accessible through their container objects that too only through their interface. Therefore it achieves complete hiding.

class Address
{
private:
int homeno;
char city[10];
char district[10];
int pincode;
public:
void get_data();
};
class Person
{
private:
char nam[20];
int age[10];
Address obj1;
public:
 void read_data();
};
The above code has established has a relationship: A person has an Address. Now an object of class person always contains an object of the class Address type.

The containership can be precisely defined as a composition of an objects.in which a contained object is hidden.

Inheritance: 

The containership works well when a class has two clearly defined sections: Private and Public. Inheritance is the most central feature of object-oriented design programming languages. Consider a situation where some important implementation of an object is needed to a programmer who is a member of the same project i.e programmer wants to reuse the implementation of his class. This situation can be solved by inheritance.
A class provides three different visibility modes:

  • Private
  • Public
  • Protected
We can access features and data members from one class to another. Inheritance has two main element
  • Parent class: The  class whose properties are inherited to the child class
  • Child class:  The class that inherits the properties of another class.

Types of inheritance:

Single Inheritance:

In single inheritance, only one class inherit in another class only. The derived class is able to use data members and member functions according to the access specifier.


Syntax:

Class Base

{
    //Block of code
};

Class Derived: Access specifier Base
{
   //Block of code
};
Example Program:
Write a program that uses the classes university and college to input and displays data of N number of students.


#include<iostream>
using namespace std;
class university
{
private:
int rollno[100];
int i , flag;
int intermarks[100];      
int theorymarks[100];
void set_theory_marks();
protected:
int numstud;
void set_inter_marks();
void issue_rollno();
public:
void prep_result();
void show_result(char colgname[]);
};
class college: public university
{
private:
char collegename[50];
public:
college(int n) //constructor
{
numstud = n;
issue_rollno();
}
void input_inter_marks();
void display_result();
};
void university:: issue_rollno()
{
for(int i = 0 ; i < numstud ; i++)
{
rollno[i] = 100 + i;
}
}
void university :: set_theory_marks()
{
cout<<"Enter theory marks for roll number mention:"<<endl;
for(int i = 0 ; i <= numstud ; i++)
{
cout<<"Roll"<<rollno[i]<<":"<<endl;
cin>>theorymarks[i];
}
}
void university :: set_inter_marks()
{
cout<<"Enter Internal marks for roll number mention:"<<endl;
for(int i = 0 ; i <= numstud ; i++)
{
cout<<"Roll"<<rollno[i]<<":"<<endl;
cin>>intermarks[i];
}
flag=1;
}
void university :: prep_result()
{
if( flag == 1 )
set_theory_marks();
else
cout<<"Input internal marks"<<endl;
}
void university :: show_result(char colgname[])
{
cout<<"College :"<<colgname;
cout<<"The result"<<endl;
cout<<"\nRoll\tInternal\tTheory"<<endl;
for( int i =0 ; i< numstud ; i++)
{
cout<<"\n"<<rollno[i]<<"\t"<<intermarks[i]<<"\t\t"<<theorymarks[i];
}
}
void college :: input_inter_marks()
{
cout<<"Enter the college name"<<endl;
cin>>collegename;
set_inter_marks();
prep_result();
}
void college :: display_result()
{
show_result(collegename);
}
int main()
{
int n;
cout<<"Enter number of students"<<endl;
cin>>n;
college co1(n);
co1.input_inter_marks();
co1.display_result();
}
It may be noted that the college class is able to use the protected and public members of the university class after inheritance.

Multiple Inheritance:

A class that can inherit properties from more than one base class is known as multiple inheritances.

Syntax:

Class Father

{
    //Block of code
};

Class Mother

{
    //Block of code
};

Class Derived: Access specifier Father, Access specifier  Mother
{
   //Block of code
};

Example Program:
Write a program in which father class and mother class properties inherit in a son class.

#include<iostream>
using namespace std;
class father
{
protected:
char fathenam[20];
char gender[10];
char city[20];
char cast[20];
public:
void father_data()
{
cout<<"Enter Father name:"<<endl;
cin>>fathenam;
cout<<"Enter Gender:"<<endl;
cin>>gender;
cout<<"Enter City:"<<endl;
cin>>city;
cout<<"Enter cast:"<<endl;
cin>>cast;
}
};
class mother
{
protected:
char mothername[20];
public:
void mother_data()
{
cout<<"Enter Mother name:"<<endl;
cin>>mothername;
}
};
class son : public father , public mother
{
private:
char name[20];
public:
void son_data()
{
 cout<<"Enter Name:"<<endl;
cin>>name;
}
void display_data()
{
father_data();
mother_data();
son_data();
cout<<"Name :"<<name<<endl;
cout<<" Father name"<<fathenam<<endl;
cout<<" Mother name"<<mothername<<endl;
cout<<" Cast :"<<cast<<endl;
cout<<" City :"<<city<<endl;
}
};
int main()
{
son s1;
s1.display_data();
}

Multilevel Inheritance:

The derived class can also become a base class for some other derived class. This type of chain of driving class can also become a base class for some other derived class..This type of chai of driving classes can go as many levels as needed. The inheritance of this type is known as multilevel inheritance.



Example Program:
Write a program in which father class and grand father class properties inherit in a son class.


#include<iostream>
using namespace std;
class grandfather
{
    protected:
char grandfathername[20];
char gender[10];
char city[20];
char cast[20];
public:
    void grand_father_data()
{
    cout<<"Enter Grand Father name:"<<endl;
cin>>grandfathername;
cout<<"Enter Gender:"<<endl;
cin>>gender;
cout<<"Enter City:"<<endl;
cin>>city;
    cout<<"Enter cast:"<<endl;
cin>>cast;
}
};
class father
{
protected:
char fathername[20];
public:
    void father_data()
{
cout<<"Enter father name:"<<endl;
cin>>fathername;
}
};
class son : public grandfather , public father
{
private:
char name[20];
public:
    void son_data()
    {
    cout<<"Enter Name:"<<endl;
    cin>>name;
    }
void display_data()
{
grand_father_data();
father_data();
son_data();
cout<<"Name :"<<name<<endl;
cout<<" Grand Father name"<<grandfathername<<endl;
cout<<" father name"<<fathername<<endl;
cout<<" Cast :"<<cast<<endl;
cout<<" City :"<<city<<endl;
}
};
int main()
{
son s1;
s1.display_data();
}

Post a Comment

0 Comments