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
|
class Person {
private:
string name;
int age;
public:
Person (string _name, int _age) {
name = _name;
age = _age;
}
void greet () {
printf ("\n- Hi my name is '%s'", name);
printf (" I have '%d' years old", age);
}
};
class Teacher: public Person {
private:
string course;
public:
Teacher (string _name, int _age, string _course): Person(_name, _age) {
course = _course;
}
void greet () {
Person::greet();
printf (" and I'll teach you the course of '%s'.", course );
}
};
class Student: public Person {
private:
char grade;
public:
Student (string _name, int _age, char _grade): Person(_name, _age) {
grade = _grade;
}
void greet () {
Person::greet();
printf (" and I come from grade %c.", grade);
}
};
/* Person
private: name, age
public: Person(name, age) {...}
greet() {...}
Teacher
private: course
public: Teacher(name, age, course): Person(name, age)
greet() {...} // l say: why do I write all again? instead call the Person.greet()
// and just add the course; same for Student.
Student
private: grade
public: Student(name, age, grade): Person(name, age)
greet() {...}
*/
int main () {
Person unknown ("Unknown",10);
Teacher jackfruit ("Jack Fruit", 213, "Object oriented programming");
Student pineapple ("Pineapple", 179 ,'A');
unknown.greet();
jackfruit.greet();
pineapple.greet();
getch();
return 0;
}
|