下载此文档

丽水学院期末c 复习---信息09-史杨杨.doc


文档分类:医学/心理学 | 页数:约12页 举报非法文档有奖
1/12
下载提示
  • 1.该资料是网友上传的,本站提供全文预览,预览什么样,下载就什么样。
  • 2.下载该文档所得收入归上传者、原创者。
  • 3.下载的文档,不会出现我们的网址水印。
1/12 下载此文档
文档列表 文档介绍
实验一 C++类的定义及成员引用
一、实验目的:
掌握类和对象的概念及特性;
掌握C++类和对象的定义;
掌握对象成员的基本引用方法;
掌握简单的面向对象程序的编写;
二、实验任务
题目1 图形类设计
设计三个图形类:Circle(圆)、Rectangle(矩形)、Triangle(三角形);
1、Cirlce 类基本信息:圆心坐标、半径;
Rectangle 类基本信息:长、宽;
Triangle 类基本信息:三个顶点坐标;
其中:成员变量为 private 属性,成员函数为public 属性;
2、每个图形类有计算图形的面积GetArea(),显示图形的基本信息函数Show(),修改基本信息的函数Set(形参)。以Circle 类为例:通过GetArea()计算圆的面积,Show()函数中显示圆心坐标、直径、周长、面积等基本信息;Set(int x,int y, int r)函数可以修改圆心坐标和半径。
3、当输入数据为不合理数据时(例如:输入的三角形的顶点是否能组成一个三角形),提示用户输入错误;
[测试数据]
1、Circle 类测试数据:
圆心:(20,30) 半径:5
(-20,40) 30
(40,-10) -5
2、Rectangle 类测试数据:
顶点坐标:20,50
40,5
-9,10
43,-8
3、Triangle 类测试数据:
顶点:(20,30)、(40,50)、(40,20)
(10,10)、(50,10)、(35,10)
源程序:
#include<iostream>
#include<>
#include<>
using namespace std;
//圆的面积
class Circle
{
private:
int x;
int y;
int r;
public:
Circle ()
{
int x=0;
int y=0;
int r=0;
}


float Circle_GetArea();
void Circle_show();
void Circle_set(int a,int b,int c)
{
x=a;
y=b;
r=c;
}

};
float Circle::Circle_GetArea()
{
float s;
s=*r*r;
return s;
}
void Circle::Circle_show()
{ float s;
if(r>0)
cout<<"("<<x<<","<<y<<")"<<setw(6)<<r<<setw(6)<<2*r<<setw(6)<<2**r<<setw(6)<<s<<endl;
else
cout<<"输入的数据错误!"<<endl;
}
//矩形的面积
class Rectangle
{
private:
int length;
int width;
public :
Rectangle()
{
int length=0;
int width=0;
}
float Rectangle_GetArea();
void Rectangle_show();
void Rectangle_set(int x1,int y1,int x2,int y2,int x3 ,int y3,int x4,int y4)
{
int a,b,c,t;
a=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
b=sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
c=sqrt((x1-x4)*(x1-x4)+(y1-y4)*(y1-y4));
if(a>=b)
{a=b;

}
if(a>=c)
{
a=c;
}
if(b>=c)
{
b=c;
}
if((x1-x4)*(x1-x2)+(y1-y4)*(y1-y2)==0&&(x1-x2==x4-x3)&&(y1-y2==y4-y3))
{ length=a;
width=b;
}
}

};
float Rectangle:: Rectangle_GetArea()
{ float z;
z=length*width;
retur

丽水学院期末c 复习---信息09-史杨杨 来自淘豆网www.taodocs.com转载请标明出处.

非法内容举报中心
文档信息
  • 页数12
  • 收藏数0 收藏
  • 顶次数0
  • 上传人977562398
  • 文件大小150 KB
  • 时间2021-01-23