关于类的派生与构造函数的赋值虚函数调用的问题,希望各位帮忙看一下。
#include "stdafx.h"
#include <iostream>
#include <windows.h>
using namespace std;
class Shape
{
public:
Shape(){};
~Shape(){};
virtual float GetArea()
{
return -1;
}
};
class Circle:public Shape
{
Circle(float radius):isRadius(radius){};
~Circle(){};
virtual float GetArea()
{
return (3.14*isRadius*isRadius);
}
private:
float isRadius;
};
class Rectangle1:public Shape
{
Rectangle1(float len,float idth):islenght(len),iswidth(idth){};
~Rectangle1(){};
virtual float GetArea()
{
return islenght*iswidth;
}
float getlenght()
{
return islenght;
}
float getwidth()
{
return iswidth;
}
private:
float islenght;
float iswidth;
};
class Square:public Rectangle1
{
public:
Square(float len){};
~Square(){}
};
Square::Square(float len):Rectangle1(len,len)
{
}
int _tmain(int argc, _TCHAR* argv[])
{
Shape *sp;
sp=new Circle(5);
printf("圆的面积为%f\n",sp->GetArea());
delete sp;
sp=new Rectangle1(4,6);
printf("矩形的面积为:%f\n",sp->GetArea());
delete sp;
sp=new Shape(5);
printf("正方形的面积是:%f\n",sp->GetArea());
delete sp;
system("pause");
return 0;
}
运行之后错误一大堆。请各位帮忙看一下。都是些最简单最基础的,应该很容易看得出来。错误提示就没有贴出来了。希望各位看了之后指出错在哪里 ,谢谢。