SabirSazzad

Vector

Feb 23rd, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3.  
  4. using namespace std;
  5. class Person{
  6. protected:
  7.     string name;
  8.     int age;
  9. public:
  10.     Person(string name, int age){this->name = name;this->age = age;}
  11.     virtual void showInfo(){
  12.     cout<<"Person: "<<this->name<<"\t"<<this->age<<endl;
  13.     }
  14. };
  15. class Student: public Person{
  16. public:
  17.     Student(string name, int age):Person(name, age){}
  18.     void showInfo(){
  19.      cout<<"Student: "<<this->name<<"\t"<<this->age<<endl;
  20.     }
  21. };
  22. void showVectior(vector<Person*> v){
  23.     for(vector<Person*>::iterator it = v.begin(); it!=v.end(); it++)
  24.         (*it)->showInfo();
  25.  
  26. }
  27.  
  28. int main(){
  29. vector<Person*> v;
  30. v.push_back(new Person("P1", 10));
  31. v.push_back(new Person("P2", 20));
  32. v.push_back(new Student("S1", 20));
  33.  
  34. v[2]->showInfo();
  35.  
  36. cout<<"\n\n";
  37. }
Advertisement
Add Comment
Please, Sign In to add comment