C++ Inheritance | Find output programs | Set 2

This section contains the C++ find output programs with their explanations on C++ Inheritance (set 2).
Submitted by Nidhi, on July 04, 2020

Program 1:

#include <iostream>
#include <string.h>
using namespace std;

class Person {
    char name[15];
    int age;

protected:
    void SetPerson(int age, char* name)
    {
        this->age = age;
        strcpy(this->name, name);
    }
    void PrintPerson()
    {
        cout << "Name: " << name << endl;
        cout << "Age:  " << age << endl;
    }
};

class Student {
    int student_id;
    int fees;

public:
    Student(int id, int fee, int age, char* name)
    {
        student_id = id;
        fees = fee;

        SetPerson(age, name);
    }
    void Print()
    {
        cout << "Student id: " << student_id << endl;
        cout << "Fees: " << fees << endl;
        PrintPerson();
    }
};

int main()
{
    Student S(101, 5000, 5, "Shaurya");
    S.Print();
    return 0;
}

Output:

main.cpp: In constructor ‘Student::Student(int, int, int, char*)’:
main.cpp:32:28: error: ‘SetPerson’ was not declared in this scope
         SetPerson(age, name);
                            ^
main.cpp: In member function ‘void Student::Print()’:
main.cpp:38:21: error: ‘PrintPerson’ was not declared in this scope
         PrintPerson();
                     ^

Explanation:

It will generate errors because, we did not inherit Person class in Student class and accessing the protected members of Person class in the Student class.

Program 2:

#include <iostream>
#include <string.h>
using namespace std;

class Class1 {
public:
    Class1()
    {
        cout << "Class1 ctor called " << endl;
    }
};

class Class2 : public Class1 {
public:
    Class2()
    {
        cout << "Class2 ctor called " << endl;
    }
};

int main()
{
    Class2 OB;
    return 0;
}

Output:

Class1 ctor called
Class2 ctor called

Explanation:

Here, we created two classes Class1 and Class2. Both classes contain constructors.

Class2 inherits Class1, in the main() function, we created the object OB of Class2. Then it called the constructor of Class1 and then it called the constructor of Class2.

In C++, the base class constructor always called before the derived class constructor.

Program 3:

#include <iostream>
#include <string.h>
using namespace std;

class Class1 {
public:
    Class1()
    {
        cout << "Class1 ctor called " << endl;
    }
};

class Class2 : public Class1 {
public:
    Class2()
    {
        cout << "Class2 ctor called " << endl;
    }
};

typedef class Class3 : public Class2 {
public:
    Class3()
    {
        cout << "Class3 ctor called " << endl;
    }
} DerivedClass;

int main()
{
    DerivedClass* PTR;
    PTR = new DerivedClass();
    return 0;
}

Output:

Class1 ctor called
Class2 ctor called
Class3 ctor called

Explanation:

Here, in the above program, we created three classes Class1, Class2, and Class3, each of them are containing a default constructor.

Here, Class1 is inherited by Class2 and Class2 inherited by Class2. We also created alias using the typedef of Class3 that is Derived class. This type of inheritance is known as multi-level inheritance.

In the main() function, we created the pointer of DerivedClass and pointer of the DerivedClass initialized using the new operator.

Then, Class1 constructor called before Class2 and Class2 constructor called before Class3.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.