C++编程问题【使用Rectangle类创建一个派生类Square】

【问题描述】声明一个基类Shape,在此基础上派生出Rectangle和Circle,两者都有GetArea()函数计算对象的面积。使用Rectangle类创建一个派生类... 【问题描述】声明一个基类Shape,在此基础上派生出Rectangle和Circle,两者都有GetArea()函数计算对象的面积。使用Rectangle类创建一个派生类Square。Circle半径是5,Rectangle长宽分别为4,6,Square半径为5
【输入形式】
【输出形式】
The area of the Circle is 78.5
The area of the Rectangle is 24
The area of Square is 25
展开
 我来答
慕_贵族
2015-04-20 · 超过57用户采纳过TA的回答
知道小有建树答主
回答量:114
采纳率:100%
帮助的人:67.6万
展开全部
//
#ifndef q_C
#define q_C
#include "stdafx.h"
#include "shape.h"

class circle : public shape
{
private:
int R;
public:
circle();
~circle();
void Set();
void GetArea();
};

#endif

#ifndef q_R
#define q_R
#include "stdafx.h"
#include "shape.h"
#include <iostream>
using namespace std;

class rectangle : public shape
{
private:
int width;
int length;
public:
rectangle();
~rectangle();
void Set();
void GetArea();
};

#endif

#ifndef q_Q
#define q_Q
#include "stdafx.h"
#include "rectangle.h"
#include <iostream>
using namespace std;
class square : public rectangle
{
private:
int r;
public:
square();
~square();
void Set();
void GetArea();
};

#endif

#ifndef q_S
#define q_S
#include "stdafx.h"
#include <iostream>
using namespace std;
class shape
{
private:
int i;

public:
shape();
~shape();
void get();
};

#endif

cpp:
#include "stdafx.h"
#include "circle.h"
#include <iostream>
using namespace std;

circle::circle()
{
R = 5;
}

circle::~circle()
{

}

void circle::GetArea()
{
float area = 3.14*R*R;
cout << "the area of circle is " << area << endl;
}

#include "stdafx.h"
#include "rectangle.h"

rectangle::rectangle()
{
width = 6;
length = 4;
}

rectangle::~rectangle()
{

}

void rectangle::GetArea()
{
int area = width*length;
cout << "the area of square is " << area << endl;
}

#include "stdafx.h"
#include "square.h"

square::square()
{
r = 5;
}

square::~square()
{

}

void square::GetArea()
{
int area = r*r;
cout << "the area of square is " << area << endl;
}

#include "stdafx.h"
#include "shape.h"

shape::shape()
{
i = 0;
}

shape::~shape()
{

}

void shape::get()
{
cout << "you are right" << endl;
}

// test.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "circle.h"
#include "rectangle.h"
#include "shape.h"
#include "square.h"

int main(void)
{
circle C;
C.GetArea();

rectangle r;
r.GetArea();

square q;
q.GetArea();

system("pause");

return 0;
}

分别建立头文件与源文件运行即可
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
达内
2021-03-31 · 百度认证:教育领域创作者
达内
向TA提问
展开全部

本视频主要从Java语言基础、JavaSE核心、WEB全栈及数据库、Servlet/Jsp核心、 Java框架五个阶段进行讲解。在本教程中,会让大家从零基础快速掌握Java知识。想要配套学习资料的小伙伴可以联系我哦!

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式