How to use three levels public inheritance in C++

1 Answer

0 votes
#include <iostream>

using std::cout;
using std::endl;

class Base {
protected:
	int a, b;
public:
	void set_ab(int _a, int _b) {
		a = _a;
		b = _b;
	}
	void print_base() {
		cout << a << " " << b << endl;
	}
};

class Derived1 : public Base {
	int c;
public:
	void set_c(int _c) {
		c = _c;
	}
	void print_derived1() {
		cout << c << endl;
	}
};


class Derived2 : public Derived1 {
	int d;             
public:
	void set_d(int _d) {
		d = _d;
	}
	void print_derived2() {
		cout << d << endl;
	}
};

int main()
{
	Derived1 od1;

	od1.set_ab(15, 18);
	od1.print_base();
	od1.set_c(23);
	od1.print_derived1();

	Derived2 od2;

	od2.set_ab(867, 999);
	od2.print_base();
	od2.set_c(725);
	od2.print_derived1();
	od2.set_d(189);
	od2.print_derived2();

	return 0;
}



/*
run:

15 18
23
867 999
725
189

*/

 



answered Mar 26, 2018 by avibootz

Related questions

1 answer 252 views
1 answer 155 views
1 answer 142 views
142 views asked Mar 25, 2018 by avibootz
1 answer 196 views
2 answers 229 views
...