刘华世的官方博客

c++语言 覆盖成员函数

//覆盖成员函数
//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;
}
posted @ 2012-11-07 15:15  pythonschool  阅读(196)  评论(0编辑  收藏  举报
刘华世的官方博客