Single Inheritance in C++

 

Inheritance In C++



Inheritance In C++

Inheritance is a process of creating new classes from existing classes. Existing class is known as base class and new class is known as derived class. The derived class is also called as sub class or child class and base class is called as super class or parent class. The derived class inherits all the features of base class .
The main advantages of of inheritance are

  • reusability
  • to increase the reliability of the code
  • to add more enhancements to the base class

Types of Inheritance:

  1. Single Inheritance:

Single inheritance is the process of creating new classes from an existing base class. This existing class is known as direct base class and the newly created class is called as singly derived class.




How to define Derived class:
The singly derived class is same as that of ordinary class.  Derived class consist following components,

  • the class keyword
  • the name of the derived class
  • a single colon
  • the type of derivation(private,public,protected)
  • the name of the base class or parent class
  • the remainder of the class definition

The general syntax for derived class
class derived_class_name : private/public/protected base_class_name
{
	private:
		//data member
	public:
		//member functions
};

For example
class basic_info
{
	private:
		char name[20];
		int rollno;
	public:
		void getdata();
		void display();
}; // end of class definition
class fitness : public basic_info
{
	private:
		float height;
		float weight;
	public:
		void get();
		void setdata();
}; //end of class definition
The derived class fitness inherits the properties of  base class basic_info. 

A program to read the data members of base class basic_info and  display the contents of the class on the screen
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class basic_info
{
private:
	char name[20];
	int rollno;
	char sex;
public:
	void getdata();
	void display();
};//end of class definition
class fitness:public basic_info
{
	private:
		float height;
		float weight;
	public:
		void getdata();
		void display();
};//end of class definition
void basic_info::getdata()
{
	cout<<”Enter name”<<endl;
	cin>>name;
	cout<<”Enter rollno”<<endl;
	cin>>rollno;
	cout<<”Enter sex”<<endl;
	cin>>sex;
}
void basic_info::display()
{
	cout<<name <<”   “;
	cout<<rollno<<”    “;
	cout<<sex<<”       “;
}
void fitness::getdata()
{
	basic_info::getdata();
	cout<<”Height”<<endl;
	cin<<height;
	cout<<”weight”<<endl;
	cin>>weight;
}
void fitness::display()
{
	basic_info::display();
	cout<<setprecision(2);
	cout<<height<<”   “;
	cout<<weight<<”    “;
}
void main()
{
fitness f;
cout<<” Enter information”<<endl;
f,getdata();
cout<<”________________________________”<<endl;
cout<<”Name         Rollno          Sex    Height     Weight”<<endl;
cout<<”_______________________________________”<<endl;
f.display();
cout<<endl;
cout<<”________________________”<<endl;
}










टिप्पणी पोस्ट करा

0 टिप्पण्या