|
以下是引用静思在2006-4-27 22:05:00的发言:
#include<iostream>
using namespace std;
const float PI=(float)3.14159;
class Shape
{
public:
Shape(int a){sh=a;}
private:
int sh;
};
class Rectangle:public Shape
{
public:
Rectangle(float wid,float len,int b):Shape(b){width=wid;lenth=len;}
float GetArea();
void display();
private:
float width;
float lenth;
};
float Rectangle::GetArea()
{
return width*lenth;
}
void Rectangle::display()
{
cout<<"Its area is: "<<GetArea()<<endl;
}
class Circle:public Shape
{
public:
Circle(float r=0.0,int c):Shape(c){radius=r;}//在编译时显示 : error C2548: 'Circle::Circle' : missing default parameter for parameter 2 //你在这边用到了默认形参,但是规定默认值的定义必须遵守从右到左的顺序,既在这里 (float r=0.0,int c)错了 必须是(int c,float r=0.0)
void display();
float GetArea();
private:
float radius;
};
float Circle::GetArea()
{
return PI*radius*radius;
}
void Circle::display()
{
cout<<"Its area is: "<<GetArea()<<endl;
}
class Square:public Rectangle
{
public:
Square(float a=0.0,float b=0.0){a=wid;b=len;}//这里也错了 应该是Square(int c,float d,float e,float a=0.0,float b=0.0):Rectangle(c,d,e){wid=a;len=b;}
private:
float wid;
float len;
};
void main()
{
Circle A(3.0,2);
A.display();
Rectangle R(2.0,3.0,2);
}
我已经在类Circle中为Shape类传递了参数,但为啥还显示错误呢??
我不太明白,麻烦哪位看一下。
谢谢!!! |
|