How to use protected inheritance in C++

1 Answer

0 votes
#include <iostream>

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

class Base {
protected:
	int a;
public:
	int b;
};


class Derived : protected Base { 
public:
	void set_a(int _a) {
		a = _a;
	}
	void set_b(int _b) {
		b = _b;
	}
	int get_a() {
		return a;
	}
	int get_b() {
		return b;
	}
};

int main()
{
	Derived o;

	o.set_a(12);
	cout << o.get_a() << endl;

	o.set_b(987);
	cout << o.get_b() << endl;

	return 0;
}



/*
run:

12
987

*/

 



answered Mar 26, 2018 by avibootz

Related questions

1 answer 212 views
1 answer 198 views
2 answers 233 views
...