| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 523 人关注过本帖
标题:[求助]关于虚函数的内容
只看楼主 加入收藏
静思
Rank: 3Rank: 3
来 自:沈阳
等 级:新手上路
威 望:8
帖 子:630
专家分:0
注 册:2006-2-28
收藏
 问题点数:0 回复次数:3 
[求助]关于虚函数的内容
#include<iostream>
using namespace std;
const float PI=(float)3.1416
class Shape()
{
public:
Shape();
~Shape();
virtual float GetArea(){return 0;}
};
class Rectangle:public Shape
{
public:
Rectangle(int wid,int len):width(wid),lenth(len){}
~Rectangle(){}
float GetArea(){return width*lenth;}
private:
float width;
float lenth;
};
class Circle:public Shape
{
public:
Circle(float r):raduis(r){}
~Circle(){}
float GetArea(){return PI*raduis*raduis;}
private:
float raduis;
};
class Square:public Rectangle
{
public:
Square(float len):Rectangle(len,len){}
~Square(){}
};
void main()
{
Shape *ptr;
ptr=new Rectangle(5,9);
cout<<"The area of the Rectangle is "<<ptr->GetArea()<<endl;
delete ptr;
ptr=new Circle(5);
cout<<"The area of the Circle is "<<ptr->GetArea()<<endl;
delete ptr;
ptr=new Square(6);
cout<<"The area of the Square is "<<ptr->GetArea()<<endl;
delete ptr;
}
有哪位看一下,里面的错误查不出来....




搜索更多相关主题的帖子: 函数 
2006-05-01 00:40
gototheworld
Rank: 1
等 级:新手上路
帖 子:218
专家分:0
注 册:2006-3-24
收藏
得分:0 
以下是引用静思在2006-5-1 0:40:00的发言:
#include<iostream>
using namespace std;
const float PI=(float)3.1416;
class Shape//()
{
public:
Shape(){};
~Shape(){};
virtual float GetArea(){return 0;}
};
class Rectangle:public Shape
{
public:
Rectangle(int wid,int len):width(wid),lenth(len){}
~Rectangle(){}
float GetArea(){return width*lenth;}
private:
float width;
float lenth;
};
class Circle:public Shape
{
public:
Circle(float r):raduis(r){}
~Circle(){}
float GetArea(){return PI*raduis*raduis;}
private:
float raduis;
};
class Square:public Rectangle
{
public:
Square(float len):Rectangle(len,len){}
~Square(){}
};
void main()
{
Shape *ptr;
ptr=new Rectangle(5,9);
cout<<"The area of the Rectangle is "<<ptr->GetArea()<<endl;
delete ptr;
ptr=new Circle(5);
cout<<"The area of the Circle is "<<ptr->GetArea()<<endl;
delete ptr;
ptr=new Square(6);
cout<<"The area of the Square is "<<ptr->GetArea()<<endl;
delete ptr;
}
有哪位看一下,里面的错误查不出来....





路漫漫其修远兮 吾将上下而求索
2006-05-01 01:37
wfpb
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:2188
专家分:0
注 册:2006-4-2
收藏
得分:0 
以下是引用静思在2006-5-1 0:40:00的发言:
#include<iostream>
using namespace std;
const float PI=(float)3.1416
class Shape//()
{
public:
//Shape(); 反正也没用.系统会给添加的.
//~Shape();

virtual float GetArea(){return 0;}
};
class Rectangle:public Shape
{
public:
Rectangle(int wid,int len):width(wid),lenth(len){} //既然是求面积,干脆都用float类型撒. 此处无错.无数据丢失.
~Rectangle(){}
float GetArea(){return width*lenth;}
private:
float width;
float lenth;
};
class Circle:public Shape
{
public:
Circle(float r):raduis(r){}
~Circle(){}
float GetArea(){return PI*raduis*raduis;}
private:
float raduis;
};
class Square:public Rectangle
{
public:
Square(float len):Rectangle(len,len){}
~Square(){}
};
void main()
{
Shape *ptr;
ptr=new Rectangle(5,9);
cout<<"The area of the Rectangle is "<<ptr->GetArea()<<endl;
delete ptr;
ptr=new Circle(5);
cout<<"The area of the Circle is "<<ptr->GetArea()<<endl;
delete ptr;
ptr=new Square(6);
cout<<"The area of the Square is "<<ptr->GetArea()<<endl;
delete ptr;
}
有哪位看一下,里面的错误查不出来....





[glow=255,red,2]wfpb的部落格[/glow] 学习成为生活的重要组成部分!
2006-05-01 02:57
roy_guo
Rank: 1
等 级:新手上路
帖 子:107
专家分:0
注 册:2006-4-27
收藏
得分:0 

#include<iostream>
using namespace std;
const float PI=(float)3.1416;

class Shape
{
public:
Shape(){};
~Shape(){};
virtual float GetArea(){return 0;}
};

class Rectangle:public Shape
{
public:
Rectangle(float wid,float len):width(wid),lenth(len){}
~Rectangle(){}
float GetArea(){return width*lenth;}
private:
float width;
float lenth;
};

class Circle:public Shape
{
public:
Circle(float r):raduis(r){}
~Circle(){}
float GetArea(){return PI*raduis*raduis;}
private:
float raduis;
};

class Square:public Rectangle
{
public:
Square(float len):Rectangle(len,len){}
~Square(){}
};

void main()
{
Shape *ptr;
ptr=new Rectangle(5,9);
cout<<"The area of the Rectangle is "<<ptr->GetArea()<<endl;
delete ptr;
ptr=new Circle(5);
cout<<"The area of the Circle is "<<ptr->GetArea()<<endl;
delete ptr;
ptr=new Square(6);
cout<<"The area of the Square is "<<ptr->GetArea()<<endl;
delete ptr;
}


彪悍的人生不需要解释~~~
2006-05-01 17:15
快速回复:[求助]关于虚函数的内容
数据加载中...
 
   



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

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