C++ program to read and print students information using two classes and simple inheritance

Learn, how to read and print students information using two classes and simple inheritance using C++ program?
[Last updated : March 02, 2023]

Reading/Printing students information using two classes and simple inheritance

This program will demonstrate example of read and print students using simple inheritance in c++ programming language.

Read and print students using simple inheritance program in C++

// C++ program to read and print students information
// using two classes and simple inheritance

#include <iostream>
using namespace std;

// Base class
class std_basic_info {
private:
    char name[30];
    int age;
    char gender;

public:
    void getBasicInfo(void);
    void putBasicInfo(void);
};

// function definitions
void std_basic_info::getBasicInfo(void)
{
    cout << "Enter student's basic information:" << endl;
    cout << "Name?: ";
    cin >> name;
    cout << "Age?: ";
    cin >> age;
    cout << "Gender?: ";
    cin >> gender;
}

void std_basic_info::putBasicInfo(void)
{
    cout << "Name: " << name << ",Age: " << age << ",Gender: " << gender << endl;
}

// Derived class
class std_result_info : public std_basic_info {
private:
    int totalM;
    float perc;
    char grade;

public:
    void getResultInfo(void);
    void putResultInfo(void);
};

// Function definitions
void std_result_info::getResultInfo(void)
{
    cout << "Enter student's result information:" << endl;
    cout << "Total Marks?: ";
    cin >> totalM;
    perc = (float)((totalM * 100) / 500);
    cout << "Grade?: ";
    cin >> grade;
}

void std_result_info::putResultInfo(void)
{
    cout << "Total Marks: " << totalM << ",Percentage: " << perc << ",Grade: " << grade << endl;
}

int main()
{
    // Create object of derived class
    std_result_info std;

    // Read student basic and result information
    std.getBasicInfo();
    std.getResultInfo();

    //print student basic and result information
    std.putBasicInfo();
    std.putResultInfo();

    return 0;
}

Output

    Enter student's basic information:
    Name?: Mickey
    Age?: 26
    Gender?: F
    Enter student's result information:
    Total Marks?: 455
    Grade?: A
    Name: Mickey,Age: 26,Gender: F
    Total Marks: 455,Percentage: 91,Grade: A





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.