It is my understanding that in C++ objects of derived classes invoke the derived class' constructor but before the body is executed the base class' constructor is invoked and takes effect and then the derived class' constructor's body is executed. If this knowledge is correct, then what happens if the base class' constructor initializes a private variable of the base class that would not be inherited to the derived class, when a new objecct of the derived class is declared.

Recommended Answers

All 7 Replies

It is my understanding that in C++ objects of derived classes invoke the derived class' constructor but before the body is executed the base class' constructor is invoked and takes effect and then the derived class' constructor's body is executed. If this knowledge is correct, then what happens if the base class' constructor initializes a private variable of the base class that would not be inherited to the derived class, when a new objecct of the derived class is declared.

The variable still exists. You'd have to provide a means of access to it either via protected or public mechanisms, or means of altering it from a function declared in the base class that allows you to do so. The access restrictions obviously still apply.

It is my understanding that in C++ objects of derived classes invoke the derived class' constructor but before the body is executed the base class' constructor is invoked and takes effect and then the derived class' constructor's body is executed. If this knowledge is correct, then what happens if the base class' constructor initializes a private variable of the base class that would not be inherited to the derived class, when a new objecct of the derived class is declared.

What's a strange question (or I don't understand it)!
No selective inheritance in C++: the derivided class object contains all base class(es) data members! Besides that, a derived class has no an access to base class private variables so it's no importance what a base class doing with its private members...

Well when a derived object is declared then the derived class inherits the public and protected variables and functions, not the private variables or functions of the base class. I was wondering what would happen if the constructor of the base class(which is inherited always) initializes some of those private variables that weren't inherited. It would be trying to modify a variable that was not part of the derived class, not part of the derived class object. Would it modify them or just produce errors...? Anyways I am going to test this with a program (always the best solution to a tough question) and see what happens.

Well when a derived object is declared then the derived class inherits the public and protected variables and functions, not the private variables or functions of the base class. I was wondering what would happen if the constructor of the base class(which is inherited always) initializes some of those private variables that weren't inherited. It would be trying to modify a variable that was not part of the derived class, not part of the derived class object. Would it modify them or just produce errors...? Anyways I am going to test this with a program (always the best solution to a tough question) and see what happens.

...

So your base class is doing its job and initializing values that were defined in its scope. Even if you define a derived class of the base class, if you are calling the constructor(s) that initialize data in the base class, it is still valid because you are passing data from the derived class to the base class.

Because you are passing data from the derived class as a parameter for the base class, and because the private variable is accessible within the scope of the base class, the derived class can indirectly modify values in the base class.

To make things easier to understand, think of the Constructor of the base class as a public means of initializing data defined in the base class, and the derived class is using that constructor itself (which should be valid since the constructor is publicly accessible).

Also there is a flaw in your statement. There is such a thing as a private constructor, which means the derived class wouldn't be capable of accessing it. It has many special purposes, but that's beyond this topic.

Well when a derived object is declared then the derived class inherits the public and protected variables and functions, not the private variables or functions of the base class. I was wondering what would happen if the constructor of the base class(which is inherited always) initializes some of those private variables that weren't inherited. It would be trying to modify a variable that was not part of the derived class, not part of the derived class object. Would it modify them or just produce errors...? Anyways I am going to test this with a program (always the best solution to a tough question) and see what happens.

Now I see that you don't understand a derived object structure. A derived object has ALL members of its base class(es) - all public, protected and private data and function members.

Don't mix up members accessibility (controlled by these specifiers) and member existence. Don't worry: base class constructors have a job because every base class private member is a member of a derived class too ;).

Try to understand an inheritance in C++. Now you have incomplete and muddle ideas on this topic...

Okay I finished the program to test all the theories running amuck:
inheritance.h

#include<iostream>
using namespace std;
class base
{
public:
	base()
	{
		priv_var=0;
	}
	void out_var()
	{
		cout<<priv_var;	
	}
private:
	int priv_var;
};
class derived:public base
{
public:
	void out_var_two()
	{
		/*cout<<priv_var;
		// if this was not commented out then this error would appear:
		// error C2248: 'base::priv_var' : cannot access private member declared in class 'base'
		//		:see declaration of 'base::priv_var'
		//		:see declaration of 'base'*/
	}
	derived()
	{}
};

main.cpp

#include<iostream>
#include"Base and derived class.h"
using namespace std;
int main()
{
	class derived derived;
	derived.out_var();
	derived.out_var_two();
	system("pause");
	return 0;
}

What this code reveals is that the priv_var is inherited however the derived class cannot access it with its own member functions, only those of the base class. Therefore it is important to have the protected scope around so that the derived class can have member functions that accesses the private variable. I was wrong in thinking that the private variables weren't inherited, and for my original question of the constructor of the base class running for the derived object, it IS a member function of the base class so therefore it can access the private variable priv_var so no problems would occur.

I can't understand what's your problem. Of course, you can't access base class private members from derived class functions (directly). It's a simple paraphrase of a private access specification.

Try to understand private members pragmatic role: you DON'T WANT to permit any direct access to these members from this class descendants. Only THIS class member functions (and this class friends) can manipulate with private variables directly.

In other words: forget parents private members in derived classes, let parents will been troubled about them.

It's so simple ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.