1
Program 1
Aim (Write on the ruler page)
write a program in C++ to implement the following class hierarchy class student to obtain roll number, class test to obtain marks scored in two different subjects, Class sports to obtain weightage (marks) in sports and class result to calculate the marks.
Program (Start from new page after AIM)
#include<iostream.h>
#include<conio.h>
class student {
int roll_no;
public:
void get_no(int a) {
roll_no = a;
}
void put_no(void) {
cout << "roll_no: " << roll_no;
}
};
class test : public student {
public:
float s1, s2;
public:
void get_marks(float x, float y) {
s1 = x;
s2 = y;
}
void put_marks(void) {
cout << "marks obtained: s1=" << s1 << " s2=" << s2;
}
};
class sports {
public:
int score;
void get_score(int x) {
score = x;
}
void put_score(void) {
cout << "sports marks: " << score;
}
};
class result : public test, public sports {
float tot;
public:
void display(void) {
tot = s1 + s2 + score;
put_no();
put_marks();
put_score();
cout << " total score=" << tot;
}
};
void main() {
result st1;
st1.get_no(101);
st1.get_marks(39, 65);
st1.get_score(29);
st1.display();
getch();
}
Output (Stick on blank page opposite to code)
Download Output