-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinheritance.cpp
More file actions
72 lines (66 loc) · 1.61 KB
/
Copy pathinheritance.cpp
File metadata and controls
72 lines (66 loc) · 1.61 KB
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
// Example program
#include <iostream>
#include <string>
#include <vector>
class Human
{
private:
std::string name, gender;
int age;
public:
Human(std::string name, int age, std::string gender)
{
this->name = name;
this->age = age;
this->gender = gender;
}
void displayData()
{
std::cout<<"Name: "<<this->name<<"\n";
std::cout<<"Age: "<<this->age<<"\n";
std::cout<<"Gender: "<<this->gender<<"\n";
}
};
class Student: public Human
{
private:
std::vector<int> marks;
public:
Student(std::string name, int age, std::string gender, std::vector<int> marks): Human(name, age, gender)
{
this->marks = marks;
};
float get_average_mark()
{
// Number of marks
unsigned int count_marks = this->marks.size();
// Summ of all marks
unsigned int sum_marks = 0;
// Average mark.
float average_mark;
for (unsigned int i = 0; i < count_marks; ++i) {
sum_marks += this->marks[i];
}
average_mark = (float) sum_marks / (float) count_marks;
return average_mark;
};
};
int main()
{
//Human *human = new Human("Pavlo", 27, "Male");
//human->displayData();
std::vector<int> marks;
marks.push_back(5);
marks.push_back(4);
marks.push_back(4);
marks.push_back(3);
marks.push_back(5);
marks.push_back(4);
marks.push_back(4);
marks.push_back(2);
marks.push_back(4);
marks.push_back(3);
Student *student = new Student("Ivan", 22, "Male", marks);
student->displayData();
std::cout<<"Average mark: "<<student->get_average_mark();
}