乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      c++虛函數(shù)例子解析

       頭號(hào)碼甲 2020-05-30
      #include <iostream>
      using namespace std;
      class Shape
      {
      public:
      //普通函數(shù)前加virtual,普通函數(shù)變成了虛函數(shù)
        // =0表示抽象虛函數(shù),子類必須實(shí)現(xiàn)該函數(shù)
      virtual void printarea()=0; void hello(){cout<<"Shape hello function"<<endl;}; }; class Circle:public Shape { public: Circle(int a):r(a){} void hello(){cout<<"Circle hello function"<<endl;}; void printarea(){cout<<"圓形面積為:"<<r*r*3.14<<endl;} private: int r; }; class Rectangle:public Shape { public: Rectangle(int a,int b):x(a),y(b){} void hello(){cout<<"Rectangle hello function"<<endl;}; void printarea(){cout<<"矩形面積為:"<<x*y<<endl;} private: int x; int y; }; class Square:public Shape { public: Square(int a):l(a){} void hello(){cout<<"Square hello function"<<endl;}; void printarea(){cout<<"正方形面積為:"<<l*l<<endl;} private: int l; }; void prints(Shape *s) {
        //基類指針s指向子類對(duì)象
      //s調(diào)用的函數(shù)printarea是虛函數(shù),所以會(huì)調(diào)用子類實(shí)例的printarea s
      ->printarea();
      //s調(diào)用的函數(shù)hello不是虛函數(shù),所以只會(huì)調(diào)用基類的hello s
      ->hello(); } int main() { Circle a(1); prints(&a); Square b(2); prints(&b); Rectangle c(1,2); prints(&c); return 0; }

       

      輸出:

      圓形面積為:3.14
      Shape hello function
      正方形面積為:4
      Shape hello function
      矩形面積為:2
      Shape hello function

       

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多