Question

In: Computer Science

C++ Code Required to Show The constructor of parent class executes before child class

C++ Code Required to Show

The constructor of parent class executes before child class

Solutions

Expert Solution

In C++, default constructer can present in all class. From the below example we can see how the constructers are called in inheritance.

Constructers of parent class are always called in the child class constructors. Whenever you create child class object, first the parent class default constructor is executed and then the child class's constructor is executed.

We attributes and functions of parent class are always we can use in the it's child class. So when we create a object of a child class, all of the data menbers and functions of child class must be initialized but the inherited members in child class can only be initialized by the constructor of the parent class because it's deefinition is exist in the parent class. Thus we can say that parent class is called first to initialize all the members and functions of it's inherited class.

You can see it in the below image :

#include <iostream>

using namespace std;
class Parent
{ 
    public:
    // default constructor
    Parent() 
    { 
        cout << "Parent class constructor" << endl; 
    }
};

class Child : public Parent
{ 
    public:
    // default constructor
    Child() 
    { 
        cout << "Child class constructor" << endl; 
    }
};

int main()
{
    cout << "When we call object of parent class" << endl;
    Parent p; 
    cout << endl;
    cout << "When we call object of child class" << endl;
    Child c;    
}

OUTPUT :

Now for parameterized constructor :

we have to mention that if if want to call parameterized constuctor of parent class from child class as shown in the below code. Parameterised constructor of parent class cannot be called in default constructor of child class, it should be called in the parameterised constructor of child class.

#include <iostream>

using namespace std;
class Parent
{ 
    int x;
    public:
    // default constructor
    Parent() 
    { 
        cout << "Parent default constructor" << endl; 
    }
    
    //parameterized constructor
    Parent(int i) 
    {  
        cout << "Parent parameterised constructor" << endl; 
    } 
};

class Child : public Parent
{ 
    public:
    // default constructor
    Child() 
    { 
        cout << "Child default constructor" << endl; 
    }
    
    //parameterized constructor
    Child(int j):Parent(j)
    { 
        cout << "Child Parameterized Constructor" << endl;
    }
};

int main()
{
    cout << "When we call object of parent class" << endl;
    Parent p; 
    cout << endl;
    cout << "When we call object of child class" << endl;
    Child c; 
     cout << endl;
    cout << "When we call object of child class with parameter" << endl;
    Child c1(10); 
}

CODE :

OUTPUT :


Related Solutions

Write a c++ code: 2.2.1 Vehicle Class The vehicle class is the parent class of a...
Write a c++ code: 2.2.1 Vehicle Class The vehicle class is the parent class of a derived class: locomotive. Their inheritance will be public inheritance so react that appropriately in their .h les. The description of the vehicle class is given in the simple UML diagram below: vehicle -map: char** -name: string -size:int -------------------------- +vehicle() +setName(s:string):void +getName():string +getMap():char** +getSize():int +setMap(s: string):void +getMapAt(x:int, y:int):char +vehicle() +operator--():void +determineRouteStatistics()=0:void The class variables are as follows: map: A 2D array of chars, it will...
C++ Code Vehicle Class The vehicle class is the parent class of a derived class: locomotive....
C++ Code Vehicle Class The vehicle class is the parent class of a derived class: locomotive. Their inheritance will be public inheritance so reflect that appropriately in their .h files. The description of the vehicle class is given in the simple UML diagram below: vehicle -map: char** -name: string -size:int -------------------------- +vehicle() +setName(s:string):void +getName():string +getMap():char** +getSize():int +setMap(s: string):void +getMapAt(x:int, y:int):char +~vehicle() +operator−−():void +determineRouteStatistics()=0:void The class variables are as follows: • map: A 2D array of chars, it will represent the...
C++ Code Vehicle Class The vehicle class is the parent class of the derived class: dieselLocomotive....
C++ Code Vehicle Class The vehicle class is the parent class of the derived class: dieselLocomotive. Their inheritance will be public inheritance so reect that appropriately in their .h les. The description of the vehicle class is given in the simple UML diagram below: vehicle -map: char** -name: string -size:int -------------------------- +vehicle() +getSize():int +setName(s:string):void +getName():string +getMap():char** +setMap(s: string):void +getMapAt(x:int, y:int):char +~vehicle() +operator--():void +determineRouteStatistics()=0:void The class variables are as follows: map: A 2D array of chars, it will represent the map...
C++ Code (I just need the dieselLocomotive Class) Vehicle Class The vehicle class is the parent...
C++ Code (I just need the dieselLocomotive Class) Vehicle Class The vehicle class is the parent class of the derived class: dieselLocomotive. Their inheritance will be public inheritance so reflect that appropriately in their .h files. The description of the vehicle class is given in the simple UML diagram below: vehicle -map: char** -name: string -size:int -------------------------- +vehicle() +getSize():int +setName(s:string):void +getName():string +getMap():char** +setMap(s: string):void +getMapAt(x:int, y:int):char +~vehicle() +operator--():void +determineRouteStatistics()=0:void The class variables are as follows: map: A 2D array of...
Using C++, Write a class KeywordsInFile. Create KeywordsInFile.h and KeywordsInFile.cpp. The only required constructor for this...
Using C++, Write a class KeywordsInFile. Create KeywordsInFile.h and KeywordsInFile.cpp. The only required constructor for this class is KeywordsInFile( filename_with_keywords, filename_with_text) filename_with_keywords is a name of a plain text file that contains the list of keywords. For the future reference, the number of words in this file is denoted by N. filename_with_text is a name of a plain text file that contains the text where the keywords must be found. For the future reference, the number of words in this...
Imagine that a parent has brought their child in for a routine vaccination. They say, “Before...
Imagine that a parent has brought their child in for a routine vaccination. They say, “Before you give my child the shot, make sure to sanitize their skin!” Should you sanitize their child’s skin? Why or why not? Make sure to show your understanding of the word sanitize in your answer.
Design You will need to have at least four classes: a parent class, a child class,...
Design You will need to have at least four classes: a parent class, a child class, a component class, and an unrelated class. The component object can be included as a field in any of the other three classes. Think about what each of the classes will represent. What added or modified methods will the child class have? What added fields will the child class have? Where does the component belong? How will the unrelated class interact with the others?...
In C++ 14.22 A dynamic class - party list Complete the Party class with a constructor...
In C++ 14.22 A dynamic class - party list Complete the Party class with a constructor with parameters, a copy constructor, a destructor, and an overloaded assignment operator (=). //main.cpp #include <iostream> using namespace std; #include "Party.h" int main() { return 0; } //party.h #ifndef PARTY_H #define PARTY_H class Party { private: string location; string *attendees; int maxAttendees; int numAttendees;    public: Party(); Party(string l, int num); //Constructor Party(/*parameters*/); //Copy constructor Party& operator=(/*parameters*/); //Add destructor void addAttendee(string name); void changeAttendeeAt(string...
What are the values in arrays a, b, and c after the following code executes (list...
What are the values in arrays a, b, and c after the following code executes (list all of the elements of the arrays)? double[] a = new double[4]; double[] b = {6,4,2}; a[a.length-1] = b[b.length-1]; double[] c = b; c[0] = -1; b[1] = c[2]; c = a; c[0] = -2; a[1] = c[3];
Please write code in java and comment . thanks Item class A constructor, with a String...
Please write code in java and comment . thanks Item class A constructor, with a String parameter representing the name of the item. A name() method and a toString() method, both of which are identical and which return the name of the item. BadAmountException Class It must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it. It must have a default...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT