Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,851 questions

51,772 answers

573 users

How to initialize two base classes from derived class in C++

1 Answer

0 votes
#include <iostream>

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

class Base1 {
protected:
	int a;
public:
	Base1(int _a) {
		a = _a;
		cout << "class Base1: " << a << endl;
	}
	~Base1() {
		cout << "~Base1()" << endl;
	}
};

class Base2 {
protected:
	int b;
public:
	Base2(int _b) {
		b = _b;
		cout << "class Base2: " << b << endl;
	}
	~Base2() {
		cout << "~Base2()" << endl;
	}
};

class Derived : public Base1, public Base2 {
public:
	Derived(int _a, int _b) : Base1(_a), Base2(_b) {
		cout << "Constructor Derived" << endl;
	}

	~Derived() {
		cout << "Destructor Derived" << endl;
	}
	void print() {
		cout << "Derived: " << a << " " << b << endl;
	}
};

int main()
{
	Derived o(17, 13);

	o.print();

	return 0;
}

/*
run:

class Base1: 17
class Base2: 13
Constructor Derived
Derived: 17 13
Destructor Derived
~Base2()
~Base1()

*/

 



answered Mar 24, 2018 by avibootz

Related questions

1 answer 160 views
1 answer 149 views
1 answer 202 views
1 answer 154 views
1 answer 174 views
1 answer 129 views
2 answers 186 views
...