C++ Inheritance: Single vs. Multiple - A Beginner's Guide

 Introduction of inheritance

·        When one object acquires the property of another is called an inheritance

·        It is the process the using existing class without modifying them

·        Based Class/Parent class – the base class has its own properties and functionality. It has common qualities that all the objects can inherit

·        Derived class/child class – derived class inherits the properties and functionality of the base class

Type of inheritance

Single Level inheritance – one base class and one derived class

#include<iostream>

 

using namespace std;

 

//Base Class

class student {

  private:

    int roll_no;

  char name[20];

  public:

    void input();

  void display();

};

 

void student::input() {

  cout << "Enter Roll no:";

  cin >> roll_no;

  cout << "Enter Name:";

  cin >> name;

}

 

void student::display() {

  cout << "Roll no is:" << roll_no << "\n";

  cout << "Name is:" << name << "\n";

}

//Derived class exam_inhert inherit the data & function of base class student

class exam_inherit: public student {

  private: int sub1;

  int sub2;

  int sub3;

  int total;

  public: void input_exam();

  void display_exam();

};

 

void exam_inherit::input_exam() {

  cout << "Enter marks of subject1:";

  cin >> sub1;

  cout << "Enter marks of subject2:";

  cin >> sub2;

  cout << "Enter marks of subject3:";

  cin >> sub3;

}

 

void exam_inherit::display_exam() {

  total = sub1 + sub2 + sub3;

  cout << "Total is :" << total << "\n";

}

 

int main() {

  exam_inherit ex;

  //here exam_inherit class object call class student function

  ex.input();

  ex.input_exam();

  //here exam_inherit class object call class student function

  ex.display();

  ex.display_exam();

  return 0;

}

Output:-    Enter Roll no:5

Enter Name:test

Enter marks of subject1:10

Enter marks of subject2:10

Enter marks of subject3:10

Roll no is:5

Name is:test

Total is :30

 

 

Multiple level inheritance – more than one base class and one derived class

#include<iostream>

using namespace std;

class student

{

protected:

int roll_no;

char name[20];

};

 

class exam_inherit

{

protected:

int sub1;

int sub2;

int sub3;

};

 // Multiple level inheritance

class grade:public student, public exam_inherit

{

private:

int avg;

public:

void input();

void display();

void average();

int total;

void input_exam();

void display_exam();

};

void grade::input()

{

cout<<"Enter Roll no:";

cin>>roll_no;

cout<<"Enter Name:";

cin>>name;

}

 

void grade::display()

{

cout<<"Roll no is:"<<roll_no<<"\n";

cout<<"Name is:"<<name <<"\n";

}

 

void grade::input_exam()

{

cout<<"Enter marks of subject1:";

cin>>sub1;

cout<<"Enter marks of subject2:";

cin>>sub2;

cout<<"Enter marks of subject3:";

cin>>sub3;

}

 

void grade::display_exam()

{

total=sub1+sub2+sub3;

cout<<"Total is :" <<total<<"\n";

}

 

void grade::average()

{

avg=total/3;

cout<<"Average is :"<<avg<<"\n";

}

 

int main()

{

grade gd;

gd.input();

gd.input_exam();

gd.display();

gd.display_exam();

gd.average();

return 0;

}

 

Output:-     Enter Roll no:5

Enter Name:test

Enter marks of subject1:15

Enter marks of subject2:15

Enter marks of subject3:20

Roll no is:5

Name is:test

Total is :50

Average is :16

 

Hierarchical inheritance – one base class and more than one derived class

#include<iostream>

using namespace std;

//Base class

class student

{

protected:

int roll_no;

char name[20];

int sub1;

int sub2;

int sub3;

int total;

};

 

//Derived class show inherit the data & function of base class student

class show:public student

{

public:

void input();

void display();

};

 

void show::input()

{

cout<<"Enter Roll no:";

cin>>roll_no;

cout<<"Enter Name:";

cin>>name;

}

 

void show::display()

{

cout<<"Roll no is:"<<roll_no<<"\n";

cout<<"Name is:"<<name <<"\n";

}

//Derived class exam inherit the data & function of base class student

class exam:public student

{

public:

void input_exam();

void total_marks();

};

 

void exam::input_exam()

{

cout<<"Enter marks of subject1:";

cin>>sub1;

cout<<"Enter marks of subject2:";

cin>>sub2;

cout<<"Enter marks of subject3:";

cin>>sub3;

}

 

void exam::total_marks()

{

total=sub1+sub2+sub3;

cout<<"Total is :" <<total<<"\n";

}

 

int main()

{

 

show sw;

exam em;

sw.input();

em.input_exam();

sw.display();

em.total_marks();

return 0;

}

 

Output: -   Enter Roll no:5

Enter Name:test

Enter marks of subject1:10

Enter marks of subject2:10

Enter marks of subject3:10

Roll no is:5

Name is:test

Total is :30

 

Multilevel inheritance – Derived class act as base class for other classes

#include<iostream>

using namespace std;

//Base class

class student

{

private:

int roll_no;

char name[20];

public:

void input();

void display();

};

 

void student::input()

{

cout<<"Enter Roll no:";

cin>>roll_no;

cout<<"Enter Name:";

cin>>name;

}

 

void student::display()

{

cout<<"Roll no is:"<<roll_no<<"\n";

cout<<"Name is:"<<name <<"\n";

}

 

//Derived class exam_inhert inherit the data & function of base class student

class exam_inherit:public student

{

private:

int sub1;

int sub2;

int sub3;

 

public:

int total;

void input_exam();

void display_exam();

};

 

void exam_inherit::input_exam()

{

cout<<"Enter marks of subject1:";

cin>>sub1;

cout<<"Enter marks of subject2:";

cin>>sub2;

cout<<"Enter marks of subject3:";

cin>>sub3;

}

 

void exam_inherit::display_exam()

{

total=sub1+sub2+sub3;

cout<<"Total is :" <<total<<"\n";

}

//Derived class grade inherit the data & function of base class exam_inherit

class grade:public exam_inherit

{

private:

int avg;

public:

void average();

};

 

void grade::average()

{

avg=total/3;

cout<<"Average is :"<<avg<<"\n";

}

 

int main()

{

grade gd;

gd.input();

gd.input_exam();

gd.display();

gd.display_exam();

gd.average();

return 0;

}

 

 

Output:-    Enter marks of subject3:10

Roll no is:5

Name is:test

Total is :30

Average is :10

 

Question: What is inheritance in C++?

Answer: Inheritance is a way for a class (called the derived class) to take on the properties and methods of another class (called the base class) in C++. This allows you to create a new class that is a modified version of an existing class, without having to reimplement all of the code in the new class.


Question: What are the different types of inheritance in C++?

Answer: There are two types of inheritance in C++: single inheritance and multiple inheritance. Single inheritance is the most basic type, where a derived class inherits from a single base class. Multiple inheritance is when a derived class can inherit from multiple base classes.


Question: How do you use inheritance in C++?

Answer: To use inheritance in C++, you use the : symbol followed by the public keyword and the name of the base class. For example: class Derived : public Base { ... }. This creates a derived class called Derived that inherits from the base class called Base. The derived class can access the public members and methods of the base class and can also have its own members and methods.


Question: Can you override methods in C++ inheritance?

Answer: Yes, you can override methods in C++ inheritance. To override a method, you can declare a method with the same name in the derived class. For example: class Derived : public Base { public: void print() { cout << "I am the Derived class" << endl; } }. In this example, the Derived::print() method overrides the Base::print() method. When called, the version of the print() method defined in the derived class will be used.


Question: What is the role of meta keywords in inheritance?

Answer: Meta keywords are HTML elements that can be included in the head section of a webpage to provide information about the content of the page. They are not necessarily used by all search engines and may not have much of an impact on search rankings. It is generally more important to focus on creating high-quality content and using relevant keywords in the actual content of the webpage, rather than relying on meta keywords.

Next Post Previous Post
No Comment
Add Comment
comment url