C++ Inheritance Programming Question Solution

C++ Oops


1. Create a class student which stores name, roll number and age of a student. Derive a class test from student class, which stores marks in 5 subjects. Input and display the details of a student

Program


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

class student
{
    char name[20];
    int roll, age;

public:
    student(char *n, int r, int a)
    {
        strcpy(name, n);
        roll = r;
        age = a;
    }
    void show()
    {
        cout << "Name : " << name << endl;
        cout << "Roll no : " << roll << endl;
        cout << "Age : " << age << endl;
    }
};

class test : public student
{
    float mark[5];

public:
    test(char *n, int r, int a, int m[5]) : student(n, r, a)
    {
        for (int i = 0; i < 5; i++)
            mark[i] = m[i];
    }
    void show()
    {
        student::show();
        for (int i = 0; i < 5; i++)
            cout << "Mark of subject " << i + 1 << " : " << mark[i] << endl;
    }
};

int main()
{
    int mark[5] = {88, 85, 90, 84, 96};
    test t("CodingSoln", 930, 19,mark);
    t.show();
    return 0;
}

Output

Name : CodingSoln
Roll no : 930
Age : 19
Mark of subject 1 : 88
Mark of subject 2 : 85
Mark of subject 3 : 90
Mark of subject 4 : 84
Mark of subject 5 : 96


2. Extend the program 1. to derive a class from result from class ‘test’ which includes member function to calculate total marks and percentage of a student. Input the data for a student and display his/her grade.

Program

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

class student
{
    char name[20];
    int roll, age;
public:
    student(char *n, int r, int a)
    {
        strcpy(name, n);
        roll = r;
        age = a;
    }
    void show()
    {
        cout<<"Name : "<<name<<endl;
        cout<<"Roll no : "<<roll<<endl;
        cout<<"Age : "<<age<<endl;
    }
};

class test: public student
{
protected:
    float mark[5];
public:
    test(char *n, int r, int a, int m[5]):student(n,r,a)
    {
        for(int i=0;i<5;i++)
            mark[i] = m[i];
    }
    void show()
    {
        student::show();
        for(int i=0;i<5;i++)
            cout<<"Mark of subject "<<i+1<<" : "<<mark[i]<<endl;
    }
};

class result: public test
{
    float total,perc;    
public:
    result(char *n, int r, int a, int m[5]):test(n,r,a,m)
    {

    }

   void show()
   {
        test::show();
        cout<<"Total mark : "<<total<<endl;
        cout<<"Percent : "<<perc<<endl;
   }
   void calculate()
   {
       total=0;
       for(int i=0;i<5;i++)
            total+=mark[i];
        perc = total/5.0;
   }
};

int main()
{
    int mark[5] = {88,85,90,84,96};
    result t("CodingSoln",930,19,mark);
    t.calculate();
    t.show();
    return 0;
}


Output

Name : CodingSoln
Roll no : 930
Age : 19
Mark of subject 1 : 88
Mark of subject 2 : 85
Mark of subject 3 : 90
Mark of subject 4 : 84
Mark of subject 5 : 96
Total mark : 443
Percent : 88.6



3. Extend the program ii. to include a class sports, which stores the marks in sports activity.Derive the result class from the classes ‘test’ and ‘Activities’. Calculate the total marks and percentage of a student

Program


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

class student
{
    char name[20];
    int roll, age;
public:
    student(char *n, int r, int a)
    {
        strcpy(name, n);
        roll = r;
        age = a;
    }
    void show()
    {
        cout<<"Name : "<<name<<endl;
        cout<<"Roll no : "<<roll<<endl;
        cout<<"Age : "<<age<<endl;
    }
};

class test: virtual public student
{
protected:
    float mark[3];
public:
    test(char *n, int r, int a, int m[3]):student(n,r,a)
    {
        for(int i=0;i<3;i++)
            mark[i] = m[i];
    }
    void show()
    {
        for(int i=0;i<3;i++)
            cout<<"Mark of subject "<<i+1<<" : "<<mark[i]<<endl;
    }
};

class activity: virtual public student
{
protected:
    float act[2];    
public:
    activity(char *n, int r, int a, int at[2]):student(n,r,a)
    {
        for(int i=0;i<2;i++)
            act[i] = at[i];
    }
    void show()
    {
        for(int i=0;i<2;i++)
            cout<<"Mark of activity "<<i+1<<" : "<<act[i]<<endl;
    }
};

class result: public test, public activity
{
    float total,perc;    
public:
    result(char *n, int r, int a, int m[3],int at[2]):student(n,r,a),test(n,r,a,m),activity(n,r,a,at)
    {
    }
   void show()
   {
        test::student::show();
        test::show();
        activity::show();
        cout<<"Total mark : "<<total<<endl;
        cout<<"Percent : "<<perc<<endl;
   }
   void calculate()
   {
       total=0;
       for(int i=0;i<3;i++)
            total+=mark[i];
        for(int i=0;i<2;i++)
            total+=act[i];
        perc = total/5.0;
   }
};

int main()
{
    int mark[3] = {80,88,95};
    int act[2] = {90,98};
    result t("CodingSoln",930,19,mark,act);
    t.calculate();
    t.show();
    return 0;
}


Output

Name : CodingSoln
Roll no : 930
Age : 19
Mark of subject 1 : 80
Mark of subject 2 : 88
Mark of subject 3 : 95
Mark of activity 1 : 90
Mark of activity 2 : 98
Total mark : 451
Percent : 90.2


Admin

Hi This is the Admin of CodingSoln. Currently Pursuing B. Tech Computer Science and Engineering form KIIT University India

Post a Comment

Previous Post Next Post