期末考试了,一道C++的题目跪求高手!万分紧急,4:30开考!
定义一个抽象类CShape,它有一个纯虚函数GetArea();派生出四边型类CSquare和圆类CCircle,在派生类中重新定义函数GetArea(),用于求图形的面...
定义一个抽象类CShape,它有一个纯虚函数GetArea ();派生出四边型类CSquare和圆类CCircle,在派生类中重新定义函数GetArea (),用于求图形的面积。在类CShape中增加静态累加器m_sCount,在类CShape的构造函数中对m_sCount进行累加,在类CShape中增加静态成员函数ShowNum()用于显示对象个数,在派生类中增加拷贝构造函数和重载赋值运算符,编写测试程序进行测试。
miss__dan - 你是一个好无聊的人! 展开
miss__dan - 你是一个好无聊的人! 展开
5个回答
2009-07-03
展开全部
按原题要求编写,程序在dev-c++和vs2005下编译通过。
#include <iostream>
using namespace std;
const double PI=3.1415926;
class CShape{//定义抽象类
public:
static int m_sCount;//定义静态累加器
virtual double GetArea()=0;//声明纯虚函数
CShape()
{
m_sCount++;
}
~CShape()
{
m_sCount--;
}
void static ShowNum(){//定义静态成员函数
if(m_sCount==1)
cout<<endl<<"There are "<<m_sCount<<" shape"<<endl;
else
cout<<endl<<"There are "<<m_sCount<<" shapes"<<endl;
}
};
int CShape::m_sCount = 0;//静态数据成员初始化
class CSquare:public CShape{//公用继承
public:
CSquare(double width=0.0,double height=0.0):w(width),h(height){};
CSquare(CSquare &s)//定义拷贝构造函数
{
w=s.w;h=s.h;
}
void operator=(CSquare &s)//重载赋值运算符
{
w=s.w;h=s.h;
}
double GetArea()//重新定义基类中的纯虚函数
{
return w*h;
}
private:
double w,h;
};
class CCircle:public CShape{
public:
CCircle(double radius=0.0):r(radius){};
CCircle(CCircle &c)//定义拷贝构造函数
{
r=c.r;
}
void operator=(CCircle &c)//重载赋值运算符
{
r=c.r;
}
double GetArea()//重新定义基类中的纯虚函数
{
return r*r*PI;
}
private:
double r;
};
int main()
{
CSquare s1(10,20);
s1.ShowNum();
cout<<"Area of new shape is "<<s1.GetArea()<<endl;
CSquare s2(s1);
s2.ShowNum();
cout<<"Area of new shape is "<<s2.GetArea()<<endl;
CSquare s3=s1;
s3.ShowNum();
cout<<"Area of new shape is "<<s3.GetArea()<<endl;
CCircle c1(10);
c1.ShowNum();
cout<<"Area of new shape is "<<c1.GetArea()<<endl;
CCircle c2(c1);
c2.ShowNum();
cout<<"Area of new shape is "<<c2.GetArea()<<endl;
CCircle c3=c1;
c3.ShowNum();
cout<<"Area of new shape is "<<c3.GetArea()<<endl;
system("pause");
return 0;
}
#include <iostream>
using namespace std;
const double PI=3.1415926;
class CShape{//定义抽象类
public:
static int m_sCount;//定义静态累加器
virtual double GetArea()=0;//声明纯虚函数
CShape()
{
m_sCount++;
}
~CShape()
{
m_sCount--;
}
void static ShowNum(){//定义静态成员函数
if(m_sCount==1)
cout<<endl<<"There are "<<m_sCount<<" shape"<<endl;
else
cout<<endl<<"There are "<<m_sCount<<" shapes"<<endl;
}
};
int CShape::m_sCount = 0;//静态数据成员初始化
class CSquare:public CShape{//公用继承
public:
CSquare(double width=0.0,double height=0.0):w(width),h(height){};
CSquare(CSquare &s)//定义拷贝构造函数
{
w=s.w;h=s.h;
}
void operator=(CSquare &s)//重载赋值运算符
{
w=s.w;h=s.h;
}
double GetArea()//重新定义基类中的纯虚函数
{
return w*h;
}
private:
double w,h;
};
class CCircle:public CShape{
public:
CCircle(double radius=0.0):r(radius){};
CCircle(CCircle &c)//定义拷贝构造函数
{
r=c.r;
}
void operator=(CCircle &c)//重载赋值运算符
{
r=c.r;
}
double GetArea()//重新定义基类中的纯虚函数
{
return r*r*PI;
}
private:
double r;
};
int main()
{
CSquare s1(10,20);
s1.ShowNum();
cout<<"Area of new shape is "<<s1.GetArea()<<endl;
CSquare s2(s1);
s2.ShowNum();
cout<<"Area of new shape is "<<s2.GetArea()<<endl;
CSquare s3=s1;
s3.ShowNum();
cout<<"Area of new shape is "<<s3.GetArea()<<endl;
CCircle c1(10);
c1.ShowNum();
cout<<"Area of new shape is "<<c1.GetArea()<<endl;
CCircle c2(c1);
c2.ShowNum();
cout<<"Area of new shape is "<<c2.GetArea()<<endl;
CCircle c3=c1;
c3.ShowNum();
cout<<"Area of new shape is "<<c3.GetArea()<<endl;
system("pause");
return 0;
}
展开全部
#include <iostream>
using namespace std;
#define PI 3.1415926
virtual class CShape{
private:
int static m_sCount;
public:
CShape(){m_sCount++;}
~CShape(){m_sCount--;}
virtual double GetArea (){return 0;}
void static ShowNum(){
if(m_sCount==1)
cout<<endl<<"There are "<<m_sCount<<" shape"<<endl;
else
cout<<endl<<"There are "<<m_sCount<<" shapes"<<endl;
}
};
int CShape::m_sCount = 0;
class CSquare:public CShape{
private:
double w,h;
public:
CSquare(double width=1.0,double height=1.0):w(width),h(height){};
CSquare(CSquare &s){
w=s.w;h=s.h;
}
void operator=(CSquare &s){
w=s.w;h=s.h;
}
double GetArea(){
return w*h;
}
};
class CCircle:public CShape{
private:
double r;
public:
CCircle(double radius=1.0):r(radius){};
CCircle(CCircle &c){
r=c.r;
}
void operator=(CCircle &c){
r=c.r;
}
double GetArea(){
return r*r*PI;
}
};
void main()
{
CSquare s1(2,1);
s1.ShowNum();
cout<<"Area of new shape is "<<s1.GetArea()<<endl;
CSquare s2(s1);
s2.ShowNum();
cout<<"Area of new shape is "<<s2.GetArea()<<endl;
CSquare s3=s1;
s3.ShowNum();
cout<<"Area of new shape is "<<s3.GetArea()<<endl;
CCircle c1(1);
c1.ShowNum();
cout<<"Area of new shape is "<<c1.GetArea()<<endl;
CCircle c2(c1);
c2.ShowNum();
cout<<"Area of new shape is "<<c2.GetArea()<<endl;
CCircle c3=c1;
c3.ShowNum();
cout<<"Area of new shape is "<<c3.GetArea()<<endl;
}
using namespace std;
#define PI 3.1415926
virtual class CShape{
private:
int static m_sCount;
public:
CShape(){m_sCount++;}
~CShape(){m_sCount--;}
virtual double GetArea (){return 0;}
void static ShowNum(){
if(m_sCount==1)
cout<<endl<<"There are "<<m_sCount<<" shape"<<endl;
else
cout<<endl<<"There are "<<m_sCount<<" shapes"<<endl;
}
};
int CShape::m_sCount = 0;
class CSquare:public CShape{
private:
double w,h;
public:
CSquare(double width=1.0,double height=1.0):w(width),h(height){};
CSquare(CSquare &s){
w=s.w;h=s.h;
}
void operator=(CSquare &s){
w=s.w;h=s.h;
}
double GetArea(){
return w*h;
}
};
class CCircle:public CShape{
private:
double r;
public:
CCircle(double radius=1.0):r(radius){};
CCircle(CCircle &c){
r=c.r;
}
void operator=(CCircle &c){
r=c.r;
}
double GetArea(){
return r*r*PI;
}
};
void main()
{
CSquare s1(2,1);
s1.ShowNum();
cout<<"Area of new shape is "<<s1.GetArea()<<endl;
CSquare s2(s1);
s2.ShowNum();
cout<<"Area of new shape is "<<s2.GetArea()<<endl;
CSquare s3=s1;
s3.ShowNum();
cout<<"Area of new shape is "<<s3.GetArea()<<endl;
CCircle c1(1);
c1.ShowNum();
cout<<"Area of new shape is "<<c1.GetArea()<<endl;
CCircle c2(c1);
c2.ShowNum();
cout<<"Area of new shape is "<<c2.GetArea()<<endl;
CCircle c3=c1;
c3.ShowNum();
cout<<"Area of new shape is "<<c3.GetArea()<<endl;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
定义一个抽象类CShape,它有一个纯虚函数GetArea ();派生出四边型类CSquare和圆类CCircle,在派生类中重新定义函数GetArea (),用于求图形的面积。在类CShape中增加静态累加器m_sCount,在类CShape的构造函数中对m_sCount进行累加,在类CShape中增加静态成员函数ShowNum()用于显示对象个数,在派生类中增加拷贝构造函数和重载赋值运算符,编写测试程序进行测试。
就是不会编程啊,你要是问硬件的来找我
就是不会编程啊,你要是问硬件的来找我
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#define PI 3.14
#include<iostream>
//抽象类
class CShape
{
public:
virtual float GetArea() =0 ;//纯虚函数
static int m_sCount ;
CShape()
{
++m_sCount ;
}
static int ShowNum()
{
return m_sCount ;
}
};
class CSquare:public CShape
{
private:
float m_Length;
float m_Height ;
public:
float GetArea()
{
return m_Length * m_Height ;
}
}
class CCircle:public CShape
{
public float GetArea()
{
return PI *r *r ;
}
}
写不动了
#include<iostream>
//抽象类
class CShape
{
public:
virtual float GetArea() =0 ;//纯虚函数
static int m_sCount ;
CShape()
{
++m_sCount ;
}
static int ShowNum()
{
return m_sCount ;
}
};
class CSquare:public CShape
{
private:
float m_Length;
float m_Height ;
public:
float GetArea()
{
return m_Length * m_Height ;
}
}
class CCircle:public CShape
{
public float GetArea()
{
return PI *r *r ;
}
}
写不动了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询