C++ Inheritance | Find output programs | Set 1

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

Program 1:

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

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

public:
    void SetPerson(int age, char* name)
    {
        this->age = age;
        strcpy(this->name, name);
    }
};

class Student : public Person {
    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 << "Name: " << name << endl;
        cout << "Age:  " << age << endl;
        cout << "Fees: " << fees;
    }
};

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

    return 0;
}

Output:

main.cpp: In member function ‘void Student::Print()’:
main.cpp:32:29: error: ‘char Person::name [15]’ is private within this context
         cout << "Name: " << name << endl;
                             ^~~~
main.cpp:6:17: note: declared private here
     char name[15];
                 ^
main.cpp:33:29: error: ‘int Person::age’ is private within this context
         cout << "Age:  " << age << endl;
                             ^~~
main.cpp:7:9: note: declared private here
     int age;
         ^~~

Explanation:

Here, we defined two classes, Person and Student, we inherited Person class in the Student class publicly, but we accessed private data members of Person class in the Student class that cannot be accessed. Then the compilation error will be there.

Program 2:

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

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

public:
    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 : public Person {
    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:

Student id: 101
Fees: 5000
Name: Shaurya
Age:  5

Explanation:

Here, we created two classes, Person and Employee. We inherited Person class into the Student class.

Person class contains two data members name, age, and member functions SetPerson(), PrintPerson().

And inherited Person class into Student class.

The Student class contains data member student_id, fees, and a parameterized constructor and Print() function.

Here, we called PrintPerson() inside the Print() function of the Student class.

In the main() function, we created an object of class S with a parameterized constructor and print complete data using the Print() function on the console screen.

Program 3:

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

class Person {
    char name[15];
    int age;
    protect : 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 : public Person {
    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:8:5: error: ‘protect’ does not name a type
     protect : void SetPerson(int age, char* name)
     ^~~~~~~
main.cpp: In constructor ‘Student::Student(int, int, int, char*)’:
main.cpp:30:28: error: ‘SetPerson’ was not declared in this scope
         SetPerson(age, name);
                            ^
main.cpp: In member function ‘void Student::Print()’:
main.cpp:36:9: error: ‘void Person::PrintPerson()’ is private within this context
         PrintPerson();
         ^~~~~~~~~~~
main.cpp:13:10: note: declared private here
     void PrintPerson()
          ^~~~~~~~~~~
main.cpp:36:21: error: ‘void Person::PrintPerson()’ is private within this context
         PrintPerson();
                     ^
main.cpp:13:10: note: declared private here
     void PrintPerson()
          ^~~~~~~~~~~

Explanation:

It will generate errors. Because we used "protect" keyword instead of "protected". So it will generate the error.






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.