用C++类中的继承?声明一个Shape(形状)基类,它有两个派生类:Circle(圆)和Square(正方形),要求:(
声明一个Shape(形状)基类,它有两个派生类:Circle(圆)和Square(正方形),要求:(1)根据给出的圆心坐标和半径计算圆的面积;(2)根据给出的正方形中心坐...
声明一个Shape(形状)基类,它有两个派生类:Circle(圆)和Square(正方形),要求:(1)根据给出的圆心坐标和半径计算圆的面积;(2)根据给出的正方形中心坐标和一个顶点坐标计算正方形的面积。
求代码? 展开
求代码? 展开
2个回答
展开全部
#include <iostream>
#include <math.h>
const double PI=3.14;
using namespace std;
class Shape
{
public:
Shape(){};
virtual double area()const{return 0.0;}
};
class Circle:public Shape
{
public:
Circle(double myr){R=myr;}
virtual double area(){return PI*R*R;}
protected:
double R;
};
class Square:public Shape
{
public:
Square(double x,double y,double a,double b)
{
X=x;Y=y;
A=a;B=b;
}
virtual double area()
{
L=sqrt((X-A)*(X-A)+(Y-B)*(Y-B));
return (L*L);
}
protected:
double X,Y,A,B,L;
};
void func(Shape &p)
{
cout<<p.area()<<endl;
}
void main()
{
double k,x,y,m,n;
cout<<"请输入圆的半径:"<<endl;
cin>>k;
Circle c(k);
cout<<"圆的面积为:"<<endl;
func(c);
cout<<"请输入正方形定点和正方形中心坐标:"<<endl;
cin>>x>>y>>m>>n;
Square s(x,y,m,n);
cout<<"正方形的面积为:"<<endl;
func(s);
}
大致的代码就是这样了 可以运行 但是没有仔细检测过
你自己检测下吧 希望对你有帮助
#include <math.h>
const double PI=3.14;
using namespace std;
class Shape
{
public:
Shape(){};
virtual double area()const{return 0.0;}
};
class Circle:public Shape
{
public:
Circle(double myr){R=myr;}
virtual double area(){return PI*R*R;}
protected:
double R;
};
class Square:public Shape
{
public:
Square(double x,double y,double a,double b)
{
X=x;Y=y;
A=a;B=b;
}
virtual double area()
{
L=sqrt((X-A)*(X-A)+(Y-B)*(Y-B));
return (L*L);
}
protected:
double X,Y,A,B,L;
};
void func(Shape &p)
{
cout<<p.area()<<endl;
}
void main()
{
double k,x,y,m,n;
cout<<"请输入圆的半径:"<<endl;
cin>>k;
Circle c(k);
cout<<"圆的面积为:"<<endl;
func(c);
cout<<"请输入正方形定点和正方形中心坐标:"<<endl;
cin>>x>>y>>m>>n;
Square s(x,y,m,n);
cout<<"正方形的面积为:"<<endl;
func(s);
}
大致的代码就是这样了 可以运行 但是没有仔细检测过
你自己检测下吧 希望对你有帮助
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<iostream>
#include<math.h>
const double PI=3.14;
using namespace std;
class Shape
{
public:
Shape()
{
};
virtual double area()
{return 0.0;}
};
class Circle:public Shape
{
public:
Circle(double myr){R = myr;}
virtual double area()
{
return PI*R*R;
}
protected:
double R;
};
class Square:public Shape
{
public:
Square(double x,double y,double a,double b)
{
X=x;Y=y;
A=a,B=b;
}
virtual double area()
{
L=sqrt((X-A)*(X-A)+(Y-B)*(Y-B));
return (L*L);
}
protected:
double X,Y,A,B,L;
};
void func (Shape &p)
{
cout<<p.area()<<endl;
}
void main()
{
double k,x,y,m,n;
cout << "请输入圆的半径: " << endl;
cin >>k;
Circle c(k);
cout <<"圆的面积为: " <<endl;
func(c);
cout <<"请输入正方形定点和正方形中心坐标: " << endl;
cin >>x>>y>>m>>n;
Square s(x,y,m,n);
cout <<"正方形的面积为: " <<endl;
func(s);
}
把一楼的程序的第11行的const去掉即可:
#include<math.h>
const double PI=3.14;
using namespace std;
class Shape
{
public:
Shape()
{
};
virtual double area()
{return 0.0;}
};
class Circle:public Shape
{
public:
Circle(double myr){R = myr;}
virtual double area()
{
return PI*R*R;
}
protected:
double R;
};
class Square:public Shape
{
public:
Square(double x,double y,double a,double b)
{
X=x;Y=y;
A=a,B=b;
}
virtual double area()
{
L=sqrt((X-A)*(X-A)+(Y-B)*(Y-B));
return (L*L);
}
protected:
double X,Y,A,B,L;
};
void func (Shape &p)
{
cout<<p.area()<<endl;
}
void main()
{
double k,x,y,m,n;
cout << "请输入圆的半径: " << endl;
cin >>k;
Circle c(k);
cout <<"圆的面积为: " <<endl;
func(c);
cout <<"请输入正方形定点和正方形中心坐标: " << endl;
cin >>x>>y>>m>>n;
Square s(x,y,m,n);
cout <<"正方形的面积为: " <<endl;
func(s);
}
把一楼的程序的第11行的const去掉即可:
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询