找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 1569|回复: 6

[求助]有关派生类的

[复制链接]
发表于 2009-11-3 01:33:33 | 显示全部楼层 |阅读模式 IP:江苏扬州
#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
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;}
private:
float wid;
float len;
};
void main()
{
Circle A(3.0,2);
A.display();
Rectangle R(2.0,3.0,2);
}
我已经在类Circle中为Shape类传递了参数,但为啥还显示错误呢??
我不太明白,麻烦哪位看一下。
谢谢!!!
发表于 2009-11-3 01:33:34 | 显示全部楼层 IP:江苏扬州
太麻烦了,看不懂
回复

使用道具 举报

发表于 2009-11-3 01:33:35 | 显示全部楼层 IP:江苏扬州
该程序先声明一个基类,再在此基础上派生出Rectangle类和Circle类,再使用Rectangle类创建一个Square类。
回复

使用道具 举报

发表于 2009-11-3 01:33:36 | 显示全部楼层 IP:江苏扬州
以下是引用静思在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类传递了参数,但为啥还显示错误呢??
我不太明白,麻烦哪位看一下。
谢谢!!!
回复

使用道具 举报

发表于 2009-11-3 01:33:37 | 显示全部楼层 IP:江苏扬州
Circle(float r=0.0,int c):Shape(c){radius=r;}
如果这样写就不会错Circle(float r,int c):Shape(c){radius=r;}
这里没必要用到带默认形参值的函数
回复

使用道具 举报

发表于 2009-11-3 01:33:38 | 显示全部楼层 IP:江苏扬州
我就是想不明白那个shape类有什么用
回复

使用道具 举报

发表于 2009-11-3 01:33:40 | 显示全部楼层 IP:江苏扬州
是没什么用
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-9-30 05:27 , Processed in 0.374440 second(s), 13 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表