丽水学院期末C++复习信息09史杨杨.docx

上传人:b****7 文档编号:9053848 上传时间:2023-02-03 格式:DOCX 页数:44 大小:100.44KB
下载 相关 举报
丽水学院期末C++复习信息09史杨杨.docx_第1页
第1页 / 共44页
丽水学院期末C++复习信息09史杨杨.docx_第2页
第2页 / 共44页
丽水学院期末C++复习信息09史杨杨.docx_第3页
第3页 / 共44页
丽水学院期末C++复习信息09史杨杨.docx_第4页
第4页 / 共44页
丽水学院期末C++复习信息09史杨杨.docx_第5页
第5页 / 共44页
点击查看更多>>
下载资源
资源描述

丽水学院期末C++复习信息09史杨杨.docx

《丽水学院期末C++复习信息09史杨杨.docx》由会员分享,可在线阅读,更多相关《丽水学院期末C++复习信息09史杨杨.docx(44页珍藏版)》请在冰豆网上搜索。

丽水学院期末C++复习信息09史杨杨.docx

丽水学院期末C++复习信息09史杨杨

实验一C++类的定义及成员引用

一、实验目的:

1.掌握类和对象的概念及特性;

2.掌握C++类和对象的定义;

3.掌握对象成员的基本引用方法;

4.掌握简单的面向对象程序的编写;

二、实验任务

题目1图形类设计

设计三个图形类:

Circle(圆)、Rectangle(矩形)、Triangle(三角形);

1、Cirlce类基本信息:

圆心坐标、半径;

Rectangle类基本信息:

长、宽;

Triangle类基本信息:

三个顶点坐标;

其中:

成员变量为private属性,成员函数为public属性;

2、每个图形类有计算图形的面积GetArea(),显示图形的基本信息函数Show(),修改基本信息的函数Set(形参)。

以Circle类为例:

通过GetArea()计算圆的面积,Show()函数中显示圆心坐标、直径、周长、面积等基本信息;Set(intx,inty,intr)函数可以修改圆心坐标和半径。

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

#include

#include

usingnamespacestd;

//圆的面积

classCircle

{

private:

intx;

inty;

intr;

public:

Circle()

{

intx=0;

inty=0;

intr=0;

}

floatCircle_GetArea();

voidCircle_show();

voidCircle_set(inta,intb,intc)

{

x=a;

y=b;

r=c;

}

};

floatCircle:

:

Circle_GetArea()

{

floats;

s=3.14*r*r;

returns;

}

voidCircle:

:

Circle_show()

{floats;

if(r>0)

cout<<"("<

else

cout<<"输入的数据错误!

"<

}

//矩形的面积

classRectangle

{

private:

intlength;

intwidth;

public:

Rectangle()

{

intlength=0;

intwidth=0;

}

floatRectangle_GetArea();

voidRectangle_show();

voidRectangle_set(intx1,inty1,intx2,inty2,intx3,inty3,intx4,inty4)

{

inta,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;

}

}

};

floatRectangle:

:

Rectangle_GetArea()

{floatz;

z=length*width;

returnz;

}

voidRectangle:

:

Rectangle_show()

{floatz;

if(z>0)

cout<<"矩形的长:

"<

(2)<<"宽:

"<

(2)<<"面积:

"<

else

cout<<"输入数据错误!

"<

}

//三角形的面积

classTriangle

{

private:

intm1,n1,m2,n2,m3,n3;

floatl1,l2,l3;

public:

Triangle()

{

intm1=0,n1=0,m2=0,n2=0,m3=0,n3=0;

floatl1=0,l2=0,l3=0;

}

floatTriangle_GetArea()

{floats,d;

if(l1+l2>l3&&l1+l3>l2&&l2+l3>l1)

{

d=(l1+l2+l3)/2;

s=sqrt(d*(d-l1)*(d-l2)*(d-l3));

}

else

s=0;

}

voidTriangle_show();

voidTriangle_set(intx1,inty1,intx2,inty2,intx3,inty3)

{m1=x1;

n1=y1;

m2=x2;

n2=y2;

m3=x3;

n3=y3;

floata,b,c;

a=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));

b=sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));

c=sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));

l1=a;

l2=b;

l3=c;

}

};

voidTriangle:

:

Triangle_show()

{

floats;

intc;

c=!

s;

switch(c)

{

case0:

cout<<"三角形三点坐标;"<

(2)<<"("<

(2)<<"("<

(2)<<"("<

cout<<"三角形边长:

"<

(2)<

cout<<"三角形的面积:

"<

(2)<

break;

case1:

cout<<"输入数据错误!

"<

break;

}

}

intmain()

{

Circlec1,c2,c3;

Rectangler;

Trianglet1,t2;

intx1,x2,x3;

inty1,y2,y3;

intr1,r2,r3;

inta1,b1,a2,b2,a3,b3,a4,b4;

intk1,l1,k2,l2,k3,l3;

intk4,l4,k5,l5,k6,l6;

cin>>x1>>y1>>r1;

cin>>x2>>y2>>r2;

cin>>x3>>y3>>r3;

cin>>a1>>b1;

cin>>a2>>b2;

cin>>a3>>b3;

cin>>a4>>b4;

cin>>k1>>l1>>k2>>l2>>k3>>l3;

cin>>k4>>l4>>k5>>l5>>k6>>l6;

c1.Circle_set(x1,y1,r1);

c2.Circle_set(x2,y2,r2);

c3.Circle_set(x3,y3,r3);

r.Rectangle_set(a1,b1,a2,b2,a3,b3,a4,b4);

t1.Triangle_set(k1,l1,k2,l2,k3,l3);

t2.Triangle_set(k4,l4,k5,l5,k6,l6);

cout<<"--------------------------------圆----------------------------------"<

cout<<"圆心坐标"<

c1.Circle_GetArea();

c1.Circle_show();

c2.Circle_GetArea();

c2.Circle_show();

c3.Circle_GetArea();

c3.Circle_show();

cout<<"----------------------------------矩形-------------------------------"<

r.Rectangle_GetArea();

r.Rectangle_show();

cout<<"-----------------------------三角形------------------------------------"<

t1.Triangle_GetArea();

t1.Triangle_show();

t2.Triangle_GetArea();

t2.Triangle_show();

return0;

}运行结果:

题目2学生类设计

设计一个Studnet(学生)类

1、基本信息:

学号、姓名、性别、出生日期、年级、班级、院系、专业;

其中:

基本信息为private属性,成员函数为public属性;

2、Student类有多个构造函数:

缺省构造函数、带参数的构造函数、带默认参数的构造

函数

3、基本功能:

1)可以从键盘输入学生的基本信息;

2)SetInfo(形参表)函数可以修改学生的基本信息函数,例如:

班级、专业等;

3)Show()函数显示学生信息;

4)当输入数据为不合理数据时,提示用户输入错误;

[测试数据]

1、学号:

2005001姓名:

zhangesan出生日期:

85年11月年级:

05级

班级:

jy01院系:

computer专业:

application

2、学号:

2005002姓名:

lihuan出生日期:

84年11月

3、学号:

2005003姓名:

wang出生日期:

85年7月年级:

05级

班级:

jy01院系:

computer专业:

software

4、学号:

2005004姓名:

zhan出生日期:

85年11月年级:

05级

班级:

jy02

5、学号:

2005005姓名:

song出生日期:

85年11月

[思考与扩展]

采用什么方式可以让学号自动按输入顺序生成?

源程序代码:

#include

#include

#include

usingnamespacestd;

classStudent

{

private:

intnumber;

stringname;

stringyear;

stringgrade;

stringclas;

stringdepartment;

stringprofession;

public:

Student()

{

grade="05级";

}

Student(stringcla);

//Student:

:

Student(intnum=2005001):

number(num){}

voidsetInto(intb,stringb1,stringb2,stringb3,stringb4,stringb5,stringb6)

{

number=b;

name=b1;

year=b2;

grade=b3;

clas=b4;

department=b5;

profession=b6;

}

voidshow();

};

Student:

:

Student(stringcla)

{

clas=cla;

}

voidStudent:

:

show()

{

cout<<"学号:

"<

"<

"<

"<

"<

"<

"<

}

intmain()

{

Studentstud[5];

inti,j,num;

for(i=0,num=2005001;i<5;i++,num++)

{stringa1,a2,a3,a4,a5,a6;

cin>>a1>>a2>>a3>>a4>>a5>>a6;

stud[i].setInto(num,a1,a2,a3,a4,a5,a6);

stud[i].show();

}

return0;

}

 

实验二C++中的构造函数与析构函数

一、实验目的:

5.掌握C++中使用构造函数创建对象;

6.掌握C++中使用析构函数释放对象;

7.掌握构造函数的重载特性;

8.掌握类相关的指针及数组的使用方法;

9.掌握const成员及static成员的特性;

10.掌握友元的特性。

二、实验任务

题目人员管理

设计某小型公司的employee(人员)类

(1)类

1)employee类:

基本信息:

编号、姓名、性别、出生日期、职位等;

出生日期使用自定义的Date(日期)类;

其中:

基本信息为private属性,成员函数为public属性;

多个构造函数:

缺省构造函数、带参数的构造函数、带默认参数的构造函数;

可以从外部访问类成员的友员函数;

2)Date类:

成员变量:

年、月、日

成员函数:

SetYear(intyear)、SetMonth(intmonth)、SetDay(intday)

GetYear()、GetMonth()、GetDay()

(2)基本功能:

1)职工信息的录入;

2)职工信息的显示;

3)用对象数组保存已输入的职工对象;

4)可以修改人员的基本信息,如:

姓名、职位等;

5)可以通过编号或姓名进行人员查询;

(3)实验提示

1)注意带参数的构造函数和带默认参数的构造函数的声明与定义;

2)定义employee类的成员变量时,应注意变量类型的声明;

3)在查询时,通过申明成employee类的友元函数来访问类的成员变量;

[实验提示]

1、注意带参数的构造函数和带默认参数的构造函数的声明与定义;

2、定义employee类的成员变量时,应注意变量类型的声明;

3、在查询时,通过申明成employee类的友元函数来访问类的成员变量;

[测试数据]

1、编号:

10001姓名:

jack出生日期:

80年11月1日职位:

普通

2、编号:

10002姓名:

andy出生日期:

75年1月9日职位:

经理

3、编号:

10003姓名:

alex出生日期:

81年4月3日职位:

秘书

4、编号:

10005姓名:

lili出生日期:

82年10月1日职位:

技师

源代码:

#include

#include

#defineN100

usingnamespacestd;

classDate

{

private:

intyear;

intmonth;

intday;

public:

voidSetYear(intx){year=x;}

voidSetMonth(intx){month=x;}

voidSetDay(intx){day=x;}

intGetYear(){returnyear;}

intGetMonth(){returnmonth;}

intGetDay(){returnday;}

Date():

year(2000),month

(1),day

(1){}

Date(intx,inty,intz)

{

year=x;

month=y;

day=z;

}

};

classemployee

{

private:

intno;

stringname;

stringsex;

Datebirth;

stringpos;

public:

employee():

no(0),birth(2000,1,1)

{

name="Andy";

sex="male";

pos="intern";

}

 

employee(intn,stringstr,stringy,Dateday,stringp)

{

no=n;

name=str;

sex=y;

birth=day;

pos=p;

}

 

friendstringGetname(employeex)

{

stringname1;

name1=x.name;

returnname1;

}

friendintGetno(employeex)

{

returnx.no;

}

friendstringGetsex(employeex)

{

stringsex1;

sex1=x.sex;

returnsex1;

}

friendDateGetbirth(employeex)

{

returnx.birth;

}

 

friendstringGetpos(employeex)

{

stringpos1;

pos1=x.pos;

returnpos1;

}

friendvoiddisplay()

{

cout<<"\n\t\t======================================="<

cout<<"\t\t1:

录入2:

显示3:

修改4:

查找0:

退出"<

cout<<"\t\t======================================="<

cout<<"\n输入您要进行的操作:

";

}

};

intmain()

{

employee*persons[N];

employeetemp;

Dateday;

intnum=0;

inti,choose=1,choose1=0;

intn,y,m,d;

stringstr;

strings;

stringp;

display();

cin>>choose;

while(choose!

=0)

{

switch(choose)

{

case1:

if(num==100)

{

cout<<"full!

\n";

continue;

}

cout<<"输入员工编号:

";

cin>>n;

cout<<"输入员工生日:

";

cin>>y>>m>>d;

cout<<"输入员工姓名:

";

cin>>str;

cout<<"输入员工性别:

";

cin>>s;

cout<<"输入员工职位:

";

cin>>p;

persons[num++]=newemployee(n,str,s,Date(y,m,d),p);

break;

case2:

cout<<"编号\t"<<"姓名\t"<<"性别\t"<<"生日\t\t"<<"职务"<

for(i=0;i

{

cout<

<<"年"<

}

break;

case3:

cout<<"输入员工编号:

";

cin>>n;

for(i=0;i<=num-1;i++)

{

if(n==Getno(*persons[i]))

{cout<<"\n\t\t=================================="<

cout<<"\t\t1:

名字修改2:

职位修改"<

cout<<"\t\t===================================="<

cout<<"选择您要进行的修改方式:

"<

cin>>choose1;

switch(choose1)

{

case1:

cout<<"输入员工姓名:

";

cin>>str;

n=Getno(*persons[i]);

s=Getsex(*persons[i]);

y=Getbirth(*persons[i]).GetYear();

m=Getbirth(*persons[i]).GetMonth();

d=Getbirth(*persons[i]).GetDay();

p=Getpos(*persons[i]);

deletepersons[i];

persons[i]=newemployee(n,str,s,Date(y,m,d),p);

break;

case2:

cout<<"输入员工职位:

";

cin>>p;

n=Getno(*pe

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 医学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1