answersLogoWhite

0


Best Answer

Remember that derived classes can access the public and protected members of their base class, but none of the private members. If the member variable in the base class is protected (rather than private), then the derived class can assign it directly. However, this is bad style in OOP as it undermines data-hiding. A protected mutator (set accessor) would be a better option.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

7y ago

We use the class constructor initialiser list to transfer values to the base class constructor.

Consider the following class definitions:

class A {

private:

int m_x;

int m_y;

public:

A (int, int);

// ...

};

class B : public A {

B (int, int);

// ...

};

Class A has two data members. Being built-in types, we must initialise those members during construction otherwise they'd be left in an uninitialised state. We could initialise them via the constructor body:

A::A (int x, int y) {

m_x = x;

m_y = y;

}

However, this is inefficient because both m_x and m_y must exist before we enter the constructor body, so we are actually instantiating both variables first and then initialising them via assignment. Although this costs very little when initialising built-in types, for more complex types initialisation through assignment can add an unacceptable overhead.

It is always more efficient to initialise variables (including member data) at the point of instantiation as soon as we have a value to assign to them. We achieve that in class constructors by using an initialiser list:

A::A (int x, int y): m_x {x}, m_y {y} {}

The constructor initialiser list begins with a colon (:) and is followed by a comma-separated list of initialisers, followed by the (now empty) constructor body. Initialiser lists can also be used to initialise base classes, thus the B class constructor can be implemented in the same way, as follows:

B::B (int x, int y): A {x, y} {}

Note how the initialiser, A {x, y}, invokes the public base class constructor A::A (int, int).

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How can a value from the derived class be assigned to a variable in base class?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What value is assigned to extern variable?

Default initial value of extern integral type variable is zero otherwise null.


What are the difference between private variable and final variable?

A private variable is one that is accessible only to the current class and cannot be accessed by any other class, including the ones that extend from it. A final variable is one that cannot be modified once it is initialized and assigned a value.


What is used as placeholder for an assigned value in a computer memory?

Variable


How do you use define in C?

Just type declare then the variable that you desire to assigned a certain constant value on it. Just type declare then the variable that you desire to assigned a certain constant value on it.


What is an expression with one variable?

3x+2 x is a variable. A variable is a symbol (x, y, etc...) that does not have an assigned value.


What is the constant of the variable?

The definition of constant variable is a variable whose value cannot be changed once it has been assigned a value for an experiment. It is the variable held steady, or constant, for a specific experiment.


What Is similar to a variable except it can be assigned a value only once?

A constant.


What is the decimal value of the letter u?

None. A letter has no numerical value unless it represents a variable and a value is assigned to it.


Which is the default variable storage class type in a function?

Everything is an object, and "typed" based on assignation. Your variable will be given a class when you declare it to be something, and the class will depend on what value you give the variable. It is always an object though, and its class may change if you change its value.


2 plus t7 -1?

And what is the question? - The value of such an expression will depend on the value assigned to the variable, in this case, "t".


Which qualifier is used to declare read-only variable in java?

A variable declared as final can't be modified, once a value is assigned.


What is the default value of register storage class?

Register storage class is a compiler hint that the variable will be often used, and that it should generate code, if it can, to keep the variable's value in a register.