Inheritance

Inheritance is one of the key features of Object-oriented programming in C++. It allows us to create a new class (derived class) from an existing class (base class). 

The derived class inherits the features from the base class and can have additional features of its own.

Advantage of C++ Inheritance

Code reusability: Now you can reuse the members of your parent class. So, there is no need to define the member again. So less code is required in the class.


Types of Inheritance 

C++ supports five types of inheritance:

Ø  Single inheritance

Ø  Multiple inheritance

Ø  Hierarchical inheritance

Ø  Multilevel inheritance

Ø  Hybrid inheritance


Syntax of Derived class:

class derived_class_name :: visibility-mode base_class_name  

{  

    // body of the derived class.  

}  


Where,

derived_class_name: It is the name of the derived class.

visibility mode: The visibility mode specifies whether the features of the base class are publicly inherited or privately inherited. It can be public or private.

base_class_name: It is the name of the base class.

  • When the base class is privately inherited by the derived class, public members of the base class becomes the private members of the derived class. Therefore, the public members of the base class are not accessible by the objects of the derived class only by the member functions of the derived class.
  • When the base class is publicly inherited by the derived class, public members of the base class also become the public members of the derived class. Therefore, the public members of the base class are accessible by the objects of the derived class as well as by the member functions of the base class.

Note:

  • In C++, the default mode of visibility is private.
  • The private members of the base class are never inherited.

 


Visibility modes can be classified into three categories:

  • Public: When the member is declared as public, it is accessible to all the functions of the program.
  • Private: When the member is declared as private, it is accessible within the class only.
  • Protected: When the member is declared as protected, it is accessible within its own class as well as the class immediately derived from it.

 

Single Inheritance

Single inheritance is defined as the inheritance in which a derived class is inherited from the only one base class.





Where 'A' is the base class, and 'B' is the derived class.

 

When one class inherits another class, it is known as single level inheritance.

 

Example

#include <iostream>

using namespace std;

// Base class: Student

class Student {

protected:

    string name;

    int rollNumber;

public:

    // Constructor to initialize name and roll number

    Student(string n, int r) {

        name = n;

        rollNumber = r;

    }

    // Method to display student details

    void displayStudentInfo() {

        cout << "Name: " << name << endl;

        cout << "Roll Number: " << rollNumber << endl;

    }

};

// Derived class: Result

class Result : public Student {

private:

    int marks[3];  // Array to store marks of 3 subjects

public:

    // Constructor to initialize student info and marks

    Result(string n, int r, int m1, int m2, int m3) : Student(n, r) {

        marks[0] = m1;

        marks[1] = m2;

        marks[2] = m3;

    }

    // Method to display the result

    void displayResult() {

        displayStudentInfo();  // Calling base class method to display student info

        cout << "Marks in 3 subjects: ";

        for (int i = 0; i < 3; i++) {

            cout << marks[i] << " ";

        }

        cout << endl;

        int total = marks[0] + marks[1] + marks[2];

        double average = total / 3.0;

        cout << "Total Marks: " << total << endl;

        cout << "Average Marks: " << average << endl;

    }

};

 

int main() {

    // Create an object of Result class (which also contains student info)

    Result student1("Prasanjeet", 101, 85, 90, 88);

    // Display student information and result

    student1.displayResult();

    return 0;

}


Output:

Name: Prasanjeet

Roll Number: 101

Marks in 3 subjects: 85 90 88

Total Marks: 263

Average Marks: 87.6667


Process returned 0 (0x0)   execution time : 2.715 s

Press any key to continue.


Multilevel Inheritance

Multilevel inheritance is a process of deriving a class from another derived class.


When one class inherits another class which is further inherited by another class, it is known as multi level inheritance in C++. Inheritance is transitive so the last derived class acquires all the members of all its base classes.


Example

#include <iostream>

using namespace std;

// Base class: Student

class Student {

protected:

    string name;

    int rollNumber;

public:

    // Constructor to initialize student details

    Student(string n, int r) {

        name = n;

        rollNumber = r;

    }

    // Method to display student details

    void displayStudentInfo() {

        cout << "Name: " << name << endl;

        cout << "Roll Number: " << rollNumber << endl;

    }

};


// Derived class: Sport (inherits from Student)

class Sport : public Student {

protected:

    string sportName;

    int sportMarks;

public:

    // Constructor to initialize sport details and student info

    Sport(string n, int r, string sName, int sMarks) : Student(n, r) {

        sportName = sName;

        sportMarks = sMarks;

    }

    // Method to display sport details

    void displaySportInfo() {

        cout << "Sport: " << sportName << endl;

        cout << "Sport Marks: " << sportMarks << endl;

    }

};

// Derived class: Result (inherits from Sport)

class Result : public Sport {

private:

    int marks[3];  // Marks for 3 subjects


public:

    // Constructor to initialize student, sport, and marks

    Result(string n, int r, string sName, int sMarks, int m1, int m2, int m3)

        : Sport(n, r, sName, sMarks) {

        marks[0] = m1;

        marks[1] = m2;

        marks[2] = m3;

    }

    // Method to display the result

    void displayResult() {

        displayStudentInfo();  // Calling base class method to display student info

        displaySportInfo();    // Calling Sport class method to display sport info

        // Display marks and calculate total and average

        cout << "Marks in 3 subjects: ";

        for (int i = 0; i < 3; i++) {

            cout << marks[i] << " ";

        }

        cout << endl;

        int total = marks[0] + marks[1] + marks[2] + sportMarks;

        double average = total / 4.0; // Total of 3 subject marks and sport marks

        cout << "Total Marks (Including Sport): " << total << endl;

        cout << "Average Marks: " << average << endl;

    }

};

int main() {

    // Create an object of Result class (which includes student, sport, and result details)

    Result student1("Prasanjeet", 101, "Cricket", 60, 85, 90, 88);


    // Display student details and result

    student1.displayResult();

    return 0;

}


Output:

Name: Prasanjeet
Roll Number: 101
Sport: Cricket
Sport Marks: 60
Marks in 3 subjects: 85 90 88
Total Marks (Including Sport): 323
Average Marks: 80.75

Process returned 0 (0x0)   execution time : 2.766 s
Press any key to continue.



Multiple Inheritance

Multiple inheritance is the process of deriving a new class that inherits the attributes from two or more classes.


Example

#include <iostream>

using namespace std;


// Base class: Student

class Student {

protected:

    string name;

    int rollNumber;


public:

    // Constructor to initialize student details

    Student(string n, int r) {

        name = n;

        rollNumber = r;

    }


    // Method to display student details

    void displayStudentInfo() {

        cout << "Name: " << name << endl;

        cout << "Roll Number: " << rollNumber << endl;

    }

};


// Base class: Sport

class Sport {

protected:

    string sportName;

    int sportMarks;


public:

    // Constructor to initialize sport details

    Sport(string sName, int sMarks) {

        sportName = sName;

        sportMarks = sMarks;

    }


    // Method to display sport details

    void displaySportInfo() {

        cout << "Sport: " << sportName << endl;

        cout << "Sport Marks: " << sportMarks << endl;

    }

};


// Derived class: Result (inherits from both Student and Sport)

class Result : public Student, public Sport {

private:

    int marks[3];  // Marks for 3 subjects


public:

    // Constructor to initialize student, sport, and result details

    Result(string n, int r, string sName, int sMarks, int m1, int m2, int m3)

        : Student(n, r), Sport(sName, sMarks) {

        marks[0] = m1;

        marks[1] = m2;

        marks[2] = m3;

    }

    // Method to display result

    void displayResult() {

        displayStudentInfo();  // Call method from Student class

        displaySportInfo();    // Call method from Sport class


        // Display marks and calculate total and average

        cout << "Marks in 3 subjects: ";

        for (int i = 0; i < 3; i++) {

            cout << marks[i] << " ";

        }

        cout << endl;

        int total = marks[0] + marks[1] + marks[2] + sportMarks;

        double average = total / 4.0; // Including sport marks

        cout << "Total Marks (Including Sport): " << total << endl;

        cout << "Average Marks: " << average << endl;

    }

};


int main() {

    // Create an object of Result class

    Result student1("Prasanjeet", 101, "Cricket", 50, 85, 90, 88);

    // Display the details and result

    student1.displayResult();

    return 0;

}

Output

Name: Prasanjeet

Roll Number: 101

Sport: Cricket

Sport Marks: 50

Marks in 3 subjects: 85 90 88

Total Marks (Including Sport): 313

Average Marks: 78.25


Process returned 0 (0x0)   execution time : 2.071 s

Press any key to continue.






No comments:

Post a Comment