Advertisement
Mastercpp

polimorfismo

Aug 17th, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <sstream>
  5.  
  6. using namespace std;
  7.  
  8.  
  9. class persona{
  10.     protected:
  11.         string nombre;
  12.         int edad;
  13.     public:
  14.         persona(){}
  15.         persona(string nom , int ed){
  16.             nombre = nom;
  17.             edad = ed;
  18.         }  
  19.         ~persona(){}
  20.        
  21.         virtual string tostring(){
  22.             stringstream stm;
  23.             stm << nombre << " : " << edad;
  24.             return stm.str();
  25.         }
  26. };
  27.  
  28. class estudiante : public persona{
  29.     private:
  30.         string ID;
  31.         double c1 , c2;
  32.     public:
  33.         estudiante();
  34.         estudiante(string nombre , int edad , string id , double ac1, double ac2 ) : persona(nombre,edad){
  35.            
  36.             ID = id;
  37.             c1 = ac1;
  38.             c2 = ac2;
  39.         }  
  40.         ~estudiante(){}
  41.        
  42.         double calcularpromedio(double c1 , double c2){
  43.             return (c1+c2)/2;
  44.         }
  45.        
  46.        
  47.         virtual string tostring(){
  48.            
  49.             stringstream tmd;
  50.             tmd << nombre << " : " << edad << endl
  51.                 <<"Id estudiante: " << ID << " Promedio total " << calcularpromedio(c1,c2) ;   
  52.             return tmd.str();
  53.         }
  54.        
  55.            
  56. };
  57.  
  58.  
  59. int main(){
  60.    
  61.  
  62.     vector<persona*> pers;
  63.    
  64.     persona mipersona("tito",16);
  65.    
  66.     estudiante miestudiante("Josue",29,"25558",89,77);
  67.    
  68.     pers.push_back(&mipersona);
  69.     pers.push_back(&miestudiante);
  70.    
  71.     for(unsigned int i =0; i<pers.size(); i++){
  72.         cout << pers[i]->tostring() << endl;
  73.     }
  74.    
  75.     cin.get();
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement