zoukankan      html  css  js  c++  java
  • oo的开始 分类: VC++ 20071228 14:31 176人阅读 评论(0) 收藏

     转载:

    //OO 设计, 多态

    // 例程3  
    #include <iostream >     
    using  namespace  std;   
       class  Vehicle 
    {   
    public :   
        Vehicle(float  speed,int  total) 
        { 
            Vehicle::speed =  speed; 
            Vehicle::total =  total; 
        } 
        virtual  void  ShowMember()//
    虚函数  
        { 
            cout <<speed<<"|"<<total<<endl; 
        } 
    protected :   
        float  speed; 
        int  total; 
    };   
    class  Car:public  Vehicle   
    {   
    public :   
        Car(int  aird,float  speed,int  total):Vehicle(speed,total)   
        {   
            Car::aird =  aird;   
        } 
        virtual  void  ShowMember()//
    虚函数 , 在派生类中,由于继承的关系,这里的 virtual 也可以不加  
        { 
            cout <<speed<<"|"<<total<<"|"<<aird<<endl; 
        } 
    public :   
        int  aird; 
    }; 
     
    void  test(Vehicle &temp) 

        temp.ShowMember(); 

     
    int  main ()   
    {   
        Vehicle a(120,4); 
        Car b(180,110,4); 
        test(a); 
        test(b); 
        cin .get(); 
    }

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    【面积并】 Atlantis
    【动态前k大 贪心】 Gone Fishing
    【复杂枚举】 library
    【双端队列bfs 网格图建图】拯救大兵瑞恩
    【奇偶传递关系 边带权】 奇偶游戏
    【权值并查集】 supermarket
    CF w4d3 A. Pythagorean Theorem II
    CF w4d2 C. Purification
    CF w4d2 B. Road Construction
    CF w4d2 A. Cakeminator
  • 原文地址:https://www.cnblogs.com/deman/p/4716628.html
Copyright © 2011-2022 走看看