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 call base constructor from derived class in C++

1 Answer

0 votes
#include <iostream>

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

class Base {
protected:
	int i;
public:
	Base(int _i) {
		i = _i;
		cout << "Base constructor" << endl;
	}
	~Base() {
		cout << "Base Destructor" << endl;
	}
};

class Derived : public Base {
	int j;
public:
	Derived(int _j, int _i) : Base(_i) {
		j = _j;
		cout << "Derived constructor" << endl;
	}

	~Derived() {
		cout << "Derived Destructor" << endl;
	}
	void print() {
		cout << i << " " << j << endl;
	}
};

int main()
{
	Derived o(87, 23);

	o.print();

	return 0;
}

/*
run:

Base constructor
Derived constructor
23 87
Derived Destructor
Base Destructor

*/

 



answered Mar 23, 2018 by avibootz

Related questions

1 answer 202 views
1 answer 145 views
1 answer 219 views
1 answer 160 views
1 answer 154 views
1 answer 174 views
...