Inheritance

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*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;
}
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