Advertisement
uopspop

personStudentTeacher

Apr 18th, 2016
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Person{
  5.     //...
  6.     public:
  7.         Person(){
  8.             name = "noName";
  9.         }
  10.         Person(string n){
  11.             name = n;
  12.         }
  13.     virtual void talk(){
  14.         cout << "You shouldn't see this sentence.'" << endl;
  15.     }
  16.     protected:
  17.         string name;
  18. };
  19. class Student : public Person{
  20.     public:
  21.         Student(){
  22.         }
  23.         Student(string n) : Person(n){
  24.         }
  25.    
  26.     string getName(){
  27.         return name;
  28.     }
  29.  
  30.     void talk(){
  31.         cout << "Studying sucks!" << endl;
  32.     }
  33.  
  34.     //...
  35. };
  36.  
  37. class Teacher : public Person{
  38.     public:
  39.         Teacher(){
  40.             //name = "NoName_Student";
  41.         }
  42.         Teacher(string n) : Person(n){
  43.         }  
  44.     void talk(){
  45.         cout << "Studying makes your life better." << endl;
  46.     }
  47.    
  48. };
  49.  
  50. int main() {
  51.     Student s1("Sam"), s3("Jamie");
  52.     Teacher t2("David"), t4("Tommmy");
  53.  
  54. // if you can know where I am; then, you can be me (pointer)
  55.     Person *p[4] = {&s1, &t2, &s3, &t4};
  56.     for (int i = 0 ; i < 4; i++){
  57.         p[i]->talk();
  58.     }
  59. /*
  60.     p[0]->talk();  
  61.     p[1]->talk();  
  62.     p[2]->talk();  
  63.     p[3]->talk();  
  64. */
  65. /*
  66.     s1.talk();
  67.     t2.talk();
  68.     s3.talk();
  69.     t4.talk();
  70. */
  71.  
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement