0% found this document useful (0 votes)
12 views4 pages

C++ Constructor and Inheritance Guide

The document discusses constructors and inheritance in C++. It explains how base class constructors are called by derived classes and how to specify which constructor to call. It also covers overriding base class functions in derived classes and calling the overridden version. The document concludes by explaining virtual base classes and how they avoid duplication when inheriting from multiple classes.

Uploaded by

fadhil mohd
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

C++ Constructor and Inheritance Guide

The document discusses constructors and inheritance in C++. It explains how base class constructors are called by derived classes and how to specify which constructor to call. It also covers overriding base class functions in derived classes and calling the overridden version. The document concludes by explaining virtual base classes and how they avoid duplication when inheriting from multiple classes.

Uploaded by

fadhil mohd
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

[Link].

com

Constructor and inheritance


The compiler automatically calls a base class constructor before executing the
derived class constructor. The compiler’s default action is to call the default
constructor in the base class. You can specify which of several base class
constructors should be called during the creation of a derived class object.

This is done by specifying the arguments to the selected base class constructor
in the definition of the derived class constructor.

class Rectangle
{
private :
float length;
float width;
public:
Rectangle ()
{
length = 0;
width = 0;
}

Rectangle (float len, float wid)


{
length = len;
width = wid;
}

float area()
{
return length * width ;
}
};

class Box : public Rectangle


{
private :
float height;
public:
Box ()
{
height = 0;
}
Box (float len, float wid, float ht) : Rectangle(len, wid)
{
height = ht;
}

float volume()
{
return area() * height;
}
};

int main ()
{
Box bx;
Box cx(4,8,5);
cout << [Link]() << endl;
cout << [Link]() << endl;
return 0;
}

output :

0
160

Overriding Base Class Functions

A derived class can override a member function of its base class by defining a
derived class member function with the same name and parameter list.
It is often useful for a derived class to define its own version of a member
function inherited from its base class. This may be done to specialize the
member function to the needs of the derived class. When this happens, the
base class member function is said to be overridden by the derived class.
class mother
{
public:
void display ()
{
cout << "mother: display function\n";
}
};

class daughter : public mother


{
public:
void display ()
{
cout << "daughter: display function\n\n";
}
};

int main ()
{
daughter rita;
[Link]();
return 0;
}

output:
daughter: display function

Gaining Access to an Overridden Function

It is occasionally useful to be able to call the overridden version. This is done by


using the scope resolution operator to specify the class of the overridden
member function being accessed.

class daughter : public mother


{
public:
void display ()
{
cout << "daughter: display function\n\n";
mother::display();
}
};
output:

daughter: display function

mother: display function

Virtual Base Class

Multipath inheritance may lead to duplication of inherited members from a


grandparent base class. This may be avoided by making the common base class
a virtual base class. When a class is made a virtual base class, C++ takes
necessary care to see that only one copy of that class is inherited.

class A
{
.....
.....
};

class B1 : virtual public A


{
.....
.....
};

class B2 : virtual public A


{
.....
.....
};

class C : public B1, public B2


{
.....// only one copy of A
.....// will be inherited
};

Common questions

Powered by AI

The rationale for using virtual base classes in C++ inheritance is to solve the diamond problem, where multiple inheritance paths lead to the duplication of a base class. Without virtual inheritance, an object inheriting from multiple classes that share a common ancestor can end up with multiple copies of the shared ancestor's state, leading to ambiguity and inefficiency. By declaring a base class as virtual, C++ ensures that only one copy of the class's state is inherited, thus addressing this multiple copies problem effectively .

In the Box class, the constructor explicitly calls the base class constructor by using a member initializer list. Specifically, in the line `Box(float len, float wid, float ht) : Rectangle(len, wid) { height = ht; }`, the constructor of the Rectangle base class that takes length and width as parameters is called. This ensures that the Rectangle part of a Box object is properly initialized with these values before proceeding with the Box-specific initialization .

A derived class can enhance or alter the functionality of an overridden member function by adding supplementary code in its own version of the function, while still possibly utilizing the base class's version through explicit calls. This allows the derived class to extend the functionality provided by the base class, incorporating domain-specific logic that is pertinent to the derived class's context. For instance, after overriding a display function, the derived class can first output its own message and then call the base class's method to append its behavior .

Virtual inheritance helps manage complexities associated with multipath inheritance by ensuring that only one instance of a virtual base class is present in the inheritance hierarchy, regardless of how many times it is inherited. This avoids redundancy and potential ambiguity associated with having multiple copies of a base class when multiple paths in the hierarchy lead to the same base. For example, if class C inherits from both classes B1 and B2, and both are derived from A using virtual inheritance, C will only inherit a single copy of A. This reduces complexity and prevents conflicts in accessing members of A .

When a derived class overrides a function from a base class and subsequently calls that base class function within its own overridden method, it effectively extends the behavior of the base class function while also adding additional specific behavior. By using the scope resolution operator (i.e., `BaseClassName::FunctionName()`), the derived class can explicitly call the base class's implementation, thus combining the functionalities of both the base and derived class functions .

To override a base class function in a derived class, you define a member function in the derived class with the same name and parameter list as the function in the base class. This allows the derived class to provide its own implementation of the function, which might better serve the specific needs of the derived class. Overriding is beneficial because it allows for polymorphism, enabling the program to decide at runtime which function to invoke based on the type of object .

In the Box class example, the constructor specifies which base class constructor to call by using a member initializer list. The line `Box(float len, float wid, float ht) : Rectangle(len, wid) { height = ht; }` shows the derived class Box specifying the constructor of the Rectangle base class that takes two arguments, length and width, to initialize its part of the Box object. This technique allows precise control over which base class constructor initializes the base part of the object .

C++ prevents the duplication of inherited members in the context of multiple inheritance by using virtual base classes. When a base class is declared as virtual, only one copy of the base class is inherited, regardless of how many times it appears in the hierarchy. This ensures that a single instance of the base class is shared among all derived classes that inherit from it. The syntax for this is to use the `virtual` keyword when declaring inheritance, as in `class B1 : virtual public A` and `class B2 : virtual public A`, ensuring class C, which inherits from both B1 and B2, only has one instance of A .

When a derived class object is created without specifying which base class constructor to use, C++ automatically calls the default constructor of the base class. This default behavior ensures that the base class part of the derived class object is always initialized, which is necessary for maintaining the integrity of objects. If no default constructor is present in the base class, and no specific constructor is called in the derived class, a compilation error will occur .

A developer uses the `mother::display()` syntax within an overridden function to access the version of `display()` defined in the base class rather than the overridden version in the derived class. This is useful when you want to execute code in the overridden function of the base class, perhaps to extend its functionality before or after running the derived class's specific logic. It allows the derived class to access and utilize base class behavior that may be essential for maintaining consistency or for some specific tasks required by the application .

You might also like