Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Person{
- //...
- public:
- Person(){
- name = "noName";
- }
- Person(string n){
- name = n;
- }
- virtual void talk(){
- cout << "You shouldn't see this sentence.'" << endl;
- }
- protected:
- string name;
- };
- class Student : public Person{
- public:
- Student(){
- }
- Student(string n) : Person(n){
- }
- string getName(){
- return name;
- }
- void talk(){
- cout << "Studying sucks!" << endl;
- }
- //...
- };
- class Teacher : public Person{
- public:
- Teacher(){
- //name = "NoName_Student";
- }
- Teacher(string n) : Person(n){
- }
- void talk(){
- cout << "Studying makes your life better." << endl;
- }
- };
- int main() {
- Student s1("Sam"), s3("Jamie");
- Teacher t2("David"), t4("Tommmy");
- // if you can know where I am; then, you can be me (pointer)
- Person *p[4] = {&s1, &t2, &s3, &t4};
- for (int i = 0 ; i < 4; i++){
- p[i]->talk();
- }
- /*
- p[0]->talk();
- p[1]->talk();
- p[2]->talk();
- p[3]->talk();
- */
- /*
- s1.talk();
- t2.talk();
- s3.talk();
- t4.talk();
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement