求C++高人指点,请编写一个抽象类Shape,在此基础上派生出类Triangle(三角形)和Circle(圆),二者都有
请编写一个抽象类Shape,在此基础上派生出类Triangle(三角形)和Circle(圆),二者都有计算对象面积的函数GetArea()和计算对象周长的函数GetPer...
请编写一个抽象类Shape,在此基础上派生出类Triangle(三角形)和Circle(圆),二者都有计算对象面积的函数GetArea()和计算对象周长的函数GetPerim()。
主函数中分别定义两个派生类的对象,通过对象的构造函数可以输入数据,然后可以输出对象的面积和周长。
要求:
1)抽象类Shape中必须统一两个派生类中计算面积和周长的函数接口。
2)派生类中计算面积和计算周长函数对数据的合法性能够做出判定。 展开
主函数中分别定义两个派生类的对象,通过对象的构造函数可以输入数据,然后可以输出对象的面积和周长。
要求:
1)抽象类Shape中必须统一两个派生类中计算面积和周长的函数接口。
2)派生类中计算面积和计算周长函数对数据的合法性能够做出判定。 展开
1个回答
展开全部
shape.h
#ifndef __SHAPE_H
#define __SHAPE_H
#include<iostream>
class Shape {
public:
double area() {
return 0;
}
double girth() {
return 0;
}
void show() {
std::cout << "物锋Shape Object:\n";
}
};
class Triangle:public Shape{
private:
double a,b,c;
public:
void set_side(double side_a,double side_b,double side_c) {
a=side_a,b=side_b;c=side_c;
}
double area();
double girth();
void show();
};
class Rectangle:public Shape{
private:
double a,b;
public:
void set_side(double side_a,double side_b){
a=side_a,b=side_b;
}
double area();
double girth();
void show();
};
class Circle:public Shape{
private:
double r;
public:
void set_side(double radius) {
r=radius;
}
double area();
double girth();
void show();
};
#endif
shape.cpp
#include"shape.h"
#define PI 3.1415927
double Triangle::area(){
return 0.5*(a+b+c);
}
double Triangle::girth(){
return a+b+c;
}
void Triangle::show() {
Shape::show();
std::cout<<"Triangle\n";
}
double Rectangle::area(){
return a*b;
}
double Rectangle::girth(){
return 2*(a+b);
}
void Rectangle::show() {
Shape::show();
std::cout<<"冲唯Rectangle\n";
}
double Circle::area(){
return PI*r*r;
}
double Circle::girth(){
return 2*PI*r;
}
void Circle::show() {
Shape::show();
std::cout<<"Circle\n";
}
main.cpp
#include"shape.h"
#include<iostream>
using namespace std;
int main() {
Triangle tri;
Rectangle rect;
Circle cir;
double a,b,c;
cout<<"Input Triangle 3 sides:";
cin>>a>>b>>c;
tri.set_side(a,b,c);
tri.show();
cout<<"area:"<<tri.area()<<endl;
cout<<"girth:"<<tri.girth()<<endl<<endl;
cout<<"Input Rectangle 2 sides:";
cin>>a>>b;
rect.set_side(a,b);
rect.show();
cout<<"area:"<<rect.area()<<endl;
cout<<"girth:"<<rect.girth()<<endl<<endl;
cout<<"Input Circle 1 radius:";
cin>>a;
cir.set_side(a);
cir.show();
cout<<"area:"<<cir.area()<<endl;
cout<<"girth:"<<cir.girth()<<endl<<endl;
system("散蚂培pause");
}
#ifndef __SHAPE_H
#define __SHAPE_H
#include<iostream>
class Shape {
public:
double area() {
return 0;
}
double girth() {
return 0;
}
void show() {
std::cout << "物锋Shape Object:\n";
}
};
class Triangle:public Shape{
private:
double a,b,c;
public:
void set_side(double side_a,double side_b,double side_c) {
a=side_a,b=side_b;c=side_c;
}
double area();
double girth();
void show();
};
class Rectangle:public Shape{
private:
double a,b;
public:
void set_side(double side_a,double side_b){
a=side_a,b=side_b;
}
double area();
double girth();
void show();
};
class Circle:public Shape{
private:
double r;
public:
void set_side(double radius) {
r=radius;
}
double area();
double girth();
void show();
};
#endif
shape.cpp
#include"shape.h"
#define PI 3.1415927
double Triangle::area(){
return 0.5*(a+b+c);
}
double Triangle::girth(){
return a+b+c;
}
void Triangle::show() {
Shape::show();
std::cout<<"Triangle\n";
}
double Rectangle::area(){
return a*b;
}
double Rectangle::girth(){
return 2*(a+b);
}
void Rectangle::show() {
Shape::show();
std::cout<<"冲唯Rectangle\n";
}
double Circle::area(){
return PI*r*r;
}
double Circle::girth(){
return 2*PI*r;
}
void Circle::show() {
Shape::show();
std::cout<<"Circle\n";
}
main.cpp
#include"shape.h"
#include<iostream>
using namespace std;
int main() {
Triangle tri;
Rectangle rect;
Circle cir;
double a,b,c;
cout<<"Input Triangle 3 sides:";
cin>>a>>b>>c;
tri.set_side(a,b,c);
tri.show();
cout<<"area:"<<tri.area()<<endl;
cout<<"girth:"<<tri.girth()<<endl<<endl;
cout<<"Input Rectangle 2 sides:";
cin>>a>>b;
rect.set_side(a,b);
rect.show();
cout<<"area:"<<rect.area()<<endl;
cout<<"girth:"<<rect.girth()<<endl<<endl;
cout<<"Input Circle 1 radius:";
cin>>a;
cir.set_side(a);
cir.show();
cout<<"area:"<<cir.area()<<endl;
cout<<"girth:"<<cir.girth()<<endl<<endl;
system("散蚂培pause");
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询