Q:

C++ Inheritance | Find output programs | Set 1

belongs to collection: C++ find output programs

0

This section contains the C++ find output programs with their explanations on C++ Inheritance (set 1).

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;
}

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;
}

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;
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer Program1:

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.

Answer Program 2:

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.

Answer Program 3:

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ find output programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C++ Inheritance | Find output programs | Set 2... >>
<< C++ Enumeration | Find output programs...