zoukankan      html  css  js  c++  java
  • C++ Polymorphism practice

    在上一题的基础上

    For the Product hierarchy of PE 4.12, suppose that,once a month,the chain runs an application that contains the loop

                for(int i = 0;i<productCount;i++) 

                       products[i]->adjustPrice();

    where products is an array of pointers to the base class Produst ,which has a virtual method adjustPrice.The virtual method adjusts the price of each Produst at least 10 classes from Produst,overriding the sdjustPrice in each.Implement a test alient that uses sample data to test the hierarchy.

    多态是一种运行期绑定机制,也是讲函数名称动态的绑定到函数入口地址的运行期绑定机制

    也就是说这题需要使用多态 因为有很多产品 要调用priceChange()函数 

    多态的使用前提条件:

    1.必须存在一个继承体系结构。

    2.继承体系结构中的一些类必须具有同名virtual成员函数(virtual is a keyword)。

    3.至少有一个基类类型的指针或者基类类型的引用,这个指针或引用可用来对virtual成员函数进行调用。

    * 当声明了基类的一个成员函数为虚函数后,该成员函数在所有派生类中也将自动成为虚函数。

    ----------------------------------------------------------------------------------------------------------------------------------------

    代码如下:

      1 #include<iostream>
      2 #include<string>
      3 
      4 using namespace std;
      5 
      6 class Product{
      7 public:
      8     int supply,demand;
      9     double p;
     10   
     11     Product(){};
     12         Product(string na):name(na){
     13         };
     14         Product(string na,double pri):name(na),price( pri){
     15         };
     16         Product(string na,double pri,int da):name( na),price( pri),date(da){
     17         };
     18         void Input();
     19         void Output();        
     20         void setSupply();
     21         void setDemand();
     22         void setPrice();
     23         virtual void adjustPrice(); 
     24         
     25 
     26 private:
     27         string name;
     28         double price;
     29         int date;
     30         string manuf;
     31         
     32 
     33 };
     34 
     35 
     36 
     37 class Fruit: public Product{
     38     
     39 };
     40 class Dairy:public Product{
     41     
     42 };
     43 class Meat:public Product{
     44 };
     45 
     46 
     47 class Cuke:public Fruit{
     48     public:
     49     
     50         void adjustPrice()
     51         {
     52             
     53                 if(supply>demand)
     54             {
     55             p=p*0.95;
     56                 cout<<"output the changed price:"<<endl;
     57             cout << p<<endl;
     58             }
     59             if(supply=demand)
     60             {
     61                 p=p*1.0;
     62                     cout<<"output the changed price:"<<endl;
     63                 cout<< p<<endl;
     64             }
     65             
     66             if(supply<demand)
     67             {
     68                 p=p*2.0;
     69                     cout<<"output the changed price:"<<endl;
     70                 cout<<p<<endl;
     71             }    
     72         }
     73        
     74 
     75     };
     76 class Apple:public Fruit{
     77      public:
     78     
     79        
     80          void adjustPrice()
     81         {
     82             
     83                 if(supply>demand)
     84             {
     85             p=p*0.95;
     86                 cout<<"output the changed price:"<<endl;
     87             cout << p<<endl;
     88             }
     89             if(supply=demand)
     90             {
     91                 p=p*1.0;
     92                     cout<<"output the changed price:"<<endl;
     93                 cout<< p<<endl;
     94             }
     95             
     96             if(supply<demand)
     97             {
     98                 p=p*2.0;
     99                     cout<<"output the changed price:"<<endl;
    100                 cout<<p<<endl;
    101             }    
    102         }
    103        
    104 };
    105 class Lemon:public Fruit{
    106      public:
    107     
    108          void adjustPrice()
    109         {
    110             
    111                 if(supply>demand)
    112             {
    113             p=p*0.95;
    114                 cout<<"output the changed price:"<<endl;
    115             cout << p<<endl;
    116             }
    117             if(supply=demand)
    118             {
    119                 p=p*1.0;
    120                     cout<<"output the changed price:"<<endl;
    121                 cout<< p<<endl;
    122             }
    123             
    124             if(supply<demand)
    125             {
    126                 p=p*2.0;
    127                     cout<<"output the changed price:"<<endl;
    128                 cout<<p<<endl;
    129             }    
    130         }
    131        
    132 };
    133 class Orange:public Fruit{
    134      public:
    135     
    136          void adjustPrice()
    137         {
    138             
    139                 if(supply>demand)
    140             {
    141             p=p*0.95;
    142                 cout<<"output the changed price:"<<endl;
    143             cout << p<<endl;
    144             }
    145             if(supply=demand)
    146             {
    147                 p=p*1.0;
    148                     cout<<"output the changed price:"<<endl;
    149                 cout<< p<<endl;
    150             }
    151             
    152             if(supply<demand)
    153             {
    154                 p=p*2.0;
    155                     cout<<"output the changed price:"<<endl;
    156                 cout<<p<<endl;
    157             }    
    158         }
    159        
    160 };
    161 class Butter:public Dairy{
    162     public:
    163     
    164        void adjustPrice()
    165         {
    166             
    167                 if(supply>demand)
    168             {
    169             p=p*0.95;
    170                 cout<<"output the changed price:"<<endl;
    171             cout << p<<endl;
    172             }
    173             if(supply=demand)
    174             {
    175                 p=p*1.0;
    176                     cout<<"output the changed price:"<<endl;
    177                 cout<< p<<endl;
    178             }
    179             
    180             if(supply<demand)
    181             {
    182                 p=p*2.0;
    183                     cout<<"output the changed price:"<<endl;
    184                 cout<<p<<endl;
    185             }    
    186         }
    187        
    188         
    189         
    190 };
    191 class Milk:public Dairy{
    192     public:
    193     
    194         void adjustPrice()
    195         {
    196             
    197                 if(supply>demand)
    198             {
    199             p=p*0.95;
    200                 cout<<"output the changed price:"<<endl;
    201             cout << p<<endl;
    202             }
    203             if(supply=demand)
    204             {
    205                 p=p*1.0;
    206                     cout<<"output the changed price:"<<endl;
    207                 cout<< p<<endl;
    208             }
    209             
    210             if(supply<demand)
    211             {
    212                 p=p*2.0;
    213                     cout<<"output the changed price:"<<endl;
    214                 cout<<p<<endl;
    215             }    
    216         }
    217         
    218 };
    219 class Yogurt:public Dairy{
    220      public:
    221     
    222         void adjustPrice()
    223         {
    224             
    225                 if(supply>demand)
    226             {
    227             p=p*0.95;
    228                 cout<<"output the changed price:"<<endl;
    229             cout << p<<endl;
    230             }
    231             if(supply=demand)
    232             {
    233                 p=p*1.0;
    234                     cout<<"output the changed price:"<<endl;
    235                 cout<< p<<endl;
    236             }
    237             
    238             if(supply<demand)
    239             {
    240                 p=p*2.0;
    241                     cout<<"output the changed price:"<<endl;
    242                 cout<<p<<endl;
    243             }    
    244         }
    245        
    246 };
    247 class Beef:public Meat{
    248     public:
    249         
    250         void adjustPrice()
    251         {
    252             
    253                 if(supply>demand)
    254             {
    255             p=p*0.95;
    256                 cout<<"output the changed price:"<<endl;
    257             cout << p<<endl;
    258             }
    259             if(supply=demand)
    260             {
    261                 p=p*1.0;
    262                     cout<<"output the changed price:"<<endl;
    263                 cout<< p<<endl;
    264             }
    265             
    266             if(supply<demand)
    267             {
    268                 p=p*2.0;
    269                     cout<<"output the changed price:"<<endl;
    270                 cout<<p<<endl;
    271             }    
    272         }
    273       
    274         
    275 };
    276 class Pork:public Meat{ 
    277     public:
    278     
    279          void adjustPrice()
    280         {
    281             
    282                 if(supply>demand)
    283             {
    284             p=p*0.95;
    285                 cout<<"output the changed price:"<<endl;
    286             cout << p<<endl;
    287             }
    288             if(supply=demand)
    289             {
    290                 p=p*1.0;
    291                     cout<<"output the changed price:"<<endl;
    292                 cout<< p<<endl;
    293             }
    294             
    295             if(supply<demand)
    296             {
    297                 p=p*2.0;
    298                     cout<<"output the changed price:"<<endl;
    299                 cout<<p<<endl;
    300             }    
    301         }
    302        
    303 };
    304 class Fish:public Meat{
    305      public:
    306     
    307     void adjustPrice()
    308         {
    309             
    310                 if(supply>demand)
    311             {
    312             p=p*0.95;
    313                 cout<<"output the changed price:"<<endl;
    314             cout << p<<endl;
    315             }
    316             if(supply=demand)
    317             {
    318                 p=p*1.0;
    319                     cout<<"output the changed price:"<<endl;
    320                 cout<< p<<endl;
    321             }
    322             
    323             if(supply<demand)
    324             {
    325                 p=p*2.0;
    326                     cout<<"output the changed price:"<<endl;
    327                 cout<<p<<endl;
    328             }    
    329         }
    330        
    331 };
    332 
    333 
    334 void Product :: Input()
    335 {
    336     cout << "input name:"<< endl;
    337     
    338     getline(cin,name);
    339 
    340     cout << "input price:"<< endl;
    341     
    342     cin >> price;
    343      
    344 
    345     
    346     cout << "input date:" << endl;
    347      
    348     cin >>  date;
    349 
    350 }
    351 void Product :: Output()
    352 {
    353     string st;
    354     cout<< "input the information you want:"<< endl;
    355     cout << "name,price,date "<<endl;
    356     cin >> st;
    357     if(st == "name" )
    358     
    359         cout << name <<endl;
    360     
    361     if(st =="price")
    362     
    363         cout << price << endl;
    364     
    365     if(st == "date")
    366     
    367         cout << date << endl;
    368     
    369 }
    370 
    371 void Product::setSupply()
    372         {
    373     
    374             
    375             cout<<"input the supply:"<<endl;
    376             cout<<"low=-1,medium=0,high=1"<<endl;
    377             cin >> supply;
    378         }
    379 void Product::setDemand()
    380         {
    381         
    382             cout<<"input the demand:"<<endl;
    383             cout<<"low=-1,medium=0,high=1"<<endl;
    384             cin >> demand;
    385             }
    386 void Product::setPrice()
    387 {
    388     cout<<"input the price"<<endl;
    389     cin >> p;
    390 }
    391  void Product::adjustPrice()
    392         {
    393                 if(supply>demand)
    394             {
    395             p=p*0.95;
    396                 cout<<"output the changed price:"<<endl;
    397             cout << p<<endl;
    398             }
    399             if(supply=demand)
    400             {
    401                 p=p*1.0;
    402                     cout<<"output the changed price:"<<endl;
    403                 cout<< p<<endl;
    404             }
    405             
    406             if(supply<demand)
    407             {
    408                 p=p*2.0;
    409                     cout<<"output the changed price:"<<endl;
    410                 cout<<p<<endl;
    411             }    
    412         }
    413 int main()
    414 {
    415     Product p1;
    416     p1.Input();
    417     p1.Output();
    418     Cuke p2;
    419     p2.setDemand();
    420     p2.setSupply();
    421     p2.setPrice();
    422     p2.adjustPrice();
    423    
    424     Product *products[10];//设置一个指向基类的指针数组 
    425     Cuke pro0;//使Pro0具有Cuke属性
    426     Apple pro1;
    427     Lemon pro2;
    428     Orange pro3;
    429     Butter pro4;
    430     Milk pro5;
    431     Yogurt pro6;
    432     Beef pro7;
    433     Pork pro8;
    434     Fish pro9;
    435     
    436     products[0]=&pro0;//将pro0 也就是cuke的地址给products[0]
    437     products[1]=&pro1;
    438     products[2]=&pro2;
    439     products[3]=&pro3;
    440     products[4]=&pro4;
    441     products[5]=&pro5;
    442     products[6]=&pro6;
    443     products[7]=&pro7;
    444     products[8]=&pro8;
    445     products[9]=&pro9;
    446     
    447     for(int i=0;i<10;i++)
    448 449     products[i]->adjustPrice();
    450   
    452 453     return 0;
    454 }
  • 相关阅读:
    C++中的extern "C"【转】
    无题
    MATLAB中文件的读写和数据的导入导出【转】
    逝去的2012
    C/C++语言中Static的作用详述
    C++:源文件与头文件有什么区别【转】
    Bash,后台与nohup
    关于include 和 extern
    python易错点
    android实现点击两次返回键实现退出功能
  • 原文地址:https://www.cnblogs.com/monica-yuki/p/4964360.html
Copyright © 2011-2022 走看看