//覆盖成员函数
//rect.CShape::display()通过作用域限定符":"指定调用了基类中的成员函数.
#include "stdafx.h"
#include <iostream>
using namespace std;
class CShape
{
private:
    int m_color;
public:
    CShape(int color=10);
    void display();
};
CShape::CShape(int color)
{
    m_color = color;
}
void CShape::display()
{
    cout << "This is a Shape" << endl;
}
class CRect:public CShape
{
private:
    int m_size;
public:
    CRect(int size = 20);
    void display();
};
CRect::CRect(int size)
{
    m_size = size;
}
void CRect::display()
{
    cout << "This is a Rect "<< endl;
}

int main(int argc, char* argv[])
{
    //printf("Hello World!\n");
    CShape shape;
    shape.display();
    CRect rect;
    rect.display();
    rect.CShape::display();
    return 0;
}

相关文章:

  • 2021-07-08
  • 2022-12-23
  • 2022-02-13
  • 2021-07-31
  • 2021-07-21
  • 2021-03-30
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-05-16
  • 2022-12-23
  • 2022-12-23
  • 2021-12-08
  • 2021-11-12
  • 2021-06-20
相关资源
相似解决方案