主题:虚函数问题
头文件:
#include <iostream>
class CShape
{
public:
void setcolor(int color){m_icolor = color;}
int getcolor(){return m_icolor;}
virtual void display(){std::cout<<"sadasd!"<<std::endl;}
private:
int m_icolor;
};
class CRect:public CShape
{
public:
virtual void display();
};
class CEllipse:public CShape
{
public:
virtual void display();
};
class CTriangle:public CShape
{
public:
virtual void display();
};
class CSquare:public CShape
{
public:
virtual void display();
};
class CCircle:public CShape
{
public:
virtual void dispaly();
};
cpp文件:
#include <iostream>
#include "shape.h"
using namespace std;
void CRect::display()
{
cout<<"This is a RECT and color is:"<<CShape::getcolor()<<endl;
}
void CEllipse::display()
{
cout<<"This is a ELLIPSE and color is:"<<CShape::getcolor()<<endl;
}
void CTriangle::display()
{
cout<<"This is a TRIANGLE and color is:"<<CShape::getcolor()<<endl;
}
void CSquare::display()
{
cout<<"This is a SQUARE and color is:"<<CShape::getcolor()<<endl;
}
void CCircle::dispaly()
{
cout<<"This is a CIRCLE and color is:"<<CShape::getcolor()<<endl;
}
int main(void)
{
CSquare square;
CRect rect1,rect2;
CCircle circle;
square.setcolor(1);
square.display();
rect1.setcolor(2);
rect1.display();
rect2.setcolor(3);
rect2.display();
circle.setcolor(4);
circle.dispaly();
CShape* tt[4]={&square,&rect1,&circle,&rect2};
for (int i = 0; i != 4;++i)
{
tt[i]->display();//问题:tt[2]->display()
}
return 0;
}
---------------------------------------------------------------------
问题就是tt[2]->display()执行的是基类的display,而不是CCircle类的display,实在想不通哪里出问题了!
#include <iostream>
class CShape
{
public:
void setcolor(int color){m_icolor = color;}
int getcolor(){return m_icolor;}
virtual void display(){std::cout<<"sadasd!"<<std::endl;}
private:
int m_icolor;
};
class CRect:public CShape
{
public:
virtual void display();
};
class CEllipse:public CShape
{
public:
virtual void display();
};
class CTriangle:public CShape
{
public:
virtual void display();
};
class CSquare:public CShape
{
public:
virtual void display();
};
class CCircle:public CShape
{
public:
virtual void dispaly();
};
cpp文件:
#include <iostream>
#include "shape.h"
using namespace std;
void CRect::display()
{
cout<<"This is a RECT and color is:"<<CShape::getcolor()<<endl;
}
void CEllipse::display()
{
cout<<"This is a ELLIPSE and color is:"<<CShape::getcolor()<<endl;
}
void CTriangle::display()
{
cout<<"This is a TRIANGLE and color is:"<<CShape::getcolor()<<endl;
}
void CSquare::display()
{
cout<<"This is a SQUARE and color is:"<<CShape::getcolor()<<endl;
}
void CCircle::dispaly()
{
cout<<"This is a CIRCLE and color is:"<<CShape::getcolor()<<endl;
}
int main(void)
{
CSquare square;
CRect rect1,rect2;
CCircle circle;
square.setcolor(1);
square.display();
rect1.setcolor(2);
rect1.display();
rect2.setcolor(3);
rect2.display();
circle.setcolor(4);
circle.dispaly();
CShape* tt[4]={&square,&rect1,&circle,&rect2};
for (int i = 0; i != 4;++i)
{
tt[i]->display();//问题:tt[2]->display()
}
return 0;
}
---------------------------------------------------------------------
问题就是tt[2]->display()执行的是基类的display,而不是CCircle类的display,实在想不通哪里出问题了!