Skip to content

Instantly share code, notes, and snippets.

@tabvn
Created June 14, 2019 13:27
Show Gist options
  • Select an option

  • Save tabvn/ffe9d28a3858dfb2777f8f783eb2a8f8 to your computer and use it in GitHub Desktop.

Select an option

Save tabvn/ffe9d28a3858dfb2777f8f783eb2a8f8 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Person{
private:
string name;
int age;
public:
const string &getName() const {
return name;
}
int getAge() const {
return age;
}
void setName(const string &name) {
Person::name = name;
}
void setAge(int age) {
Person::age = age;
}
};
class Student : public Person{
private:
double toan, ly,hoa;
public:
void setToan(double x){
Student::toan = x;
}
void setLy(double x){
Student::ly = x;
}
void setHoa(double x){
Student::hoa = x;
}
double getToan(){
return Student::toan;
}
double getLy(){
return Student::ly;
}
double getHoa(){
return Student::hoa;
}
double TB(){
return (Student::getLy() + Student::getToan() + Student::getHoa()) / 3;
}
};
class Teacher : public Person{
private:
vector<Student> s;
public:
void setStudent(Student student){
Teacher::s.push_back(student);
}
vector<Student> getStudents(){
return Teacher::s;
}
Student getGoodStudent(){
Student good;
if(Teacher::s.size() == 0){
cout << "Ko co SV nao";
return good;
}
good = Teacher::s[0];
double tb = good.TB();
for(int i = 1; i < Teacher::s.size(); i++){
if(Teacher::s[i].TB() > tb){
good = Teacher::s[i];
}
}
return good ;
}
};
int main() {
Student s1, s2;
s1.setName("Toan Nguyen Dinh");
s1.setAge(32);
s1.setToan(9);
s1.setLy(9);
s1.setHoa(8);
Teacher t;
t.setName("Thay Cong");
t.setAge(30);
t.setStudent(s1);
s2.setName("Tom");
s2.setAge(2);
s2.setToan(9);
s2.setLy(8);
s2.setHoa(7);
t.setStudent(s2);
Student good = t.getGoodStudent();
cout << "Good student is: " << good.getName() << ":" << good.TB();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment