Sat Nov 23 2019

Multilevel Inheritance

C++ Programming1247 views

File Name: multilevel-inheritance.cpp

#include<iostream>
using namespace std;

class student {

	/* Protected variable */
	protected:
		int enrolment_no;

	public:
		inline void get_enrol(int enrol) {
			enrolment_no = enrol;
		}

		inline void display_enrol() {
			cout << "Enrolment No: " << enrolment_no << endl;
		}
};

/* First level derivation */
class test : public student {

	/* Protected variable */
	protected:
		int subject1, subject2;

	public:
		inline void get_marks(int sub1, int sub2) {
			subject1 = sub1;
			subject2 = sub2;
		}

		inline void display_marks() {
			cout << "Marks in Subject1: " << subject1 << endl;
			cout << "Marks in Subject2: " << subject2 << endl;
		}
};

/* Second level derivation */
class result : public test {
	int total;

	public:
		void display_result() {
			total = subject1 +  subject2;
			display_enrol();
			display_marks();
			cout << "Total Marks: " << total << endl;
			cout << "------------------------------------\n" << endl;
		}
};


int main() {
	result student1, student2;

	/* Call member function from class 'student' */
	student1.get_enrol(4);
	student2.get_enrol(13);

	/* Call member function from class 'test' */
	student1.get_marks(50,70);
	student2.get_marks(60,50);

	/* Call member function from class 'result' */
	student1.display_result();
	student2.display_result();
}


/* Output */
Enrolment No: 4
Marks in Subject1: 50
Marks in Subject2: 70
Total Marks: 120

------------------------------------

Enrolment No: 13
Marks in Subject1: 60
Marks in Subject2: 50
Total Marks: 110
Reference:

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.