| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 695 人关注过本帖
标题:[求助]有关派生类的
只看楼主 加入收藏
静思
Rank: 3Rank: 3
来 自:沈阳
等 级:新手上路
威 望:8
帖 子:630
专家分:0
注 册:2006-2-28
收藏
 问题点数:0 回复次数:6 
[求助]有关派生类的
#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类传递了参数,但为啥还显示错误呢??
我不太明白,麻烦哪位看一下。
谢谢!!!
搜索更多相关主题的帖子: 派生类 
2006-04-27 22:05
ot1122
Rank: 1
等 级:新手上路
帖 子:31
专家分:0
注 册:2006-4-20
收藏
得分:0 
太麻烦了,看不懂
2006-04-27 22:11
静思
Rank: 3Rank: 3
来 自:沈阳
等 级:新手上路
威 望:8
帖 子:630
专家分:0
注 册:2006-2-28
收藏
得分:0 
该程序先声明一个基类,再在此基础上派生出Rectangle类和Circle类,再使用Rectangle类创建一个Square类。

英者自知,雄者自胜
2006-04-27 22:39
freshman42
Rank: 1
等 级:新手上路
威 望:1
帖 子:94
专家分:0
注 册:2005-12-4
收藏
得分:0 
以下是引用静思在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类传递了参数,但为啥还显示错误呢??
我不太明白,麻烦哪位看一下。
谢谢!!!


2006-04-27 22:50
freshman42
Rank: 1
等 级:新手上路
威 望:1
帖 子:94
专家分:0
注 册:2005-12-4
收藏
得分:0 
Circle(float r=0.0,int c):Shape(c){radius=r;}
如果这样写就不会错Circle(float r,int c):Shape(c){radius=r;}
这里没必要用到带默认形参值的函数

2006-04-27 22:53
guang
Rank: 4
来 自:广东深圳
等 级:贵宾
威 望:13
帖 子:1414
专家分:285
注 册:2006-4-3
收藏
得分:0 
我就是想不明白那个shape类有什么用

不相信未作牺牲竟先可拥有,只相信靠双手找到我的欲求!!
我的博客:http://liao5930.blog.
2006-04-28 15:05
wfpb
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:2188
专家分:0
注 册:2006-4-2
收藏
得分:0 

是没什么用


[glow=255,red,2]wfpb的部落格[/glow] 学习成为生活的重要组成部分!
2006-04-28 15:39
快速回复:[求助]有关派生类的
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015515 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved