实验10思考题.docx

上传人:b****3 文档编号:24735740 上传时间:2023-06-01 格式:DOCX 页数:19 大小:17.89KB
下载 相关 举报
实验10思考题.docx_第1页
第1页 / 共19页
实验10思考题.docx_第2页
第2页 / 共19页
实验10思考题.docx_第3页
第3页 / 共19页
实验10思考题.docx_第4页
第4页 / 共19页
实验10思考题.docx_第5页
第5页 / 共19页
点击查看更多>>
下载资源
资源描述

实验10思考题.docx

《实验10思考题.docx》由会员分享,可在线阅读,更多相关《实验10思考题.docx(19页珍藏版)》请在冰豆网上搜索。

实验10思考题.docx

实验10思考题

实验10类与对象

(二)

三、实验思考

1.定义一个圆类,计算圆的面积和周长。

要求分别用成员函数和友元函数来求圆的面积和周长。

#include

usingnamespacestd;

classCircle

{

public:

Circle(float,float,float);

floatarea();

floatperimeter();

friendfloatarea(Circle&);

friendfloatperimeter(Circle&);

private:

floatx;

floaty;

floatr;

};

Circle:

:

Circle(floata,floatb,floatc)

{

x=a;

y=b;

r=c;

}

floatCircle:

:

area()

{

return3.14159*r*r;

}

floatCircle:

:

perimeter()

{

return2*3.14159*r;

}

floatarea(Circle&c)

{

return3.14159*c.r*c.r;

}

floatperimeter(Circle&c)

{

return2*3.14159*c.r;

}

intmain()

{

Circlecir(5.0,4.0,3.0);

cout<

cout<

cout<

cout<

return0;

}

 

2.定义Boat与Car两个类,两者都有weight属性,定义两者的一个友元函数totalWeight()为外部函数,计算两者的重量和。

#include

usingnamespacestd;

classCar;

classBoat

{

public:

Boat(inta):

weight(a){}

friendinttotalWeight(Boat&boat,Car&car);

private:

intweight;

};

classCar

{

public:

Car(inta):

weight(a){}

friendinttotalWeight(Boat&boat,Car&car);

private:

intweight;

};

inttotalWeight(Boat&boat,Car&car)

{

returnboat.weight+car.weight;

}

intmain()

{

Boatboat(20);

Carcar(10);

cout<<"Theweightis:

";

cout<

return0;

}

 

3.设计一个Book类,包含两个私有数据成员count和price,建立一个含有5个元素的数组对象,将count初始化为1,2,3,4,5,将price初始化为10,15,20,25,30,显示每个对象的count和price值。

#include

usingnamespacestd;

classBook

{

public:

Book(intc,intp):

count(c),price(p){}

voidoutput();

private:

intcount;

intprice;

};

voidBook:

:

output()

{

cout<

}

intmain()

{

Bookb[5]={Book(1,10),Book(2,20),Book(3,30),Book(4,40),Book(5,50)};

for(inti=0;i<5;i++)

b[i].output();

return0;

}

 

4.设计一个Student类,包含学生的基本信息:

学号、姓名、性别、出生日期、年级、班级、院系和专业等,Student类有多个构造函数:

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

类的基本功能有:

(1)使用对象数组保存学生对象。

(2)从键盘输入学生的基本信息。

(3)修改学生的基本信息。

(4)显示学生信息。

#include

#include

#include

#defineN100

usingnamespacestd;

classDate

{

public:

Date(){}

Date(inty,intm,intd):

year(y),month(m),day(d){};

Date(Date&);

voidSetYear(intx){year=x;}

voidSetMonth(intx){month=x;}

voidSetDay(intx){day=x;}

intGetYear(){returnyear;}//获取年

intGetMonth(){returnmonth;}//获取月

intGetDay(){returnday;}//获取日

private:

intyear;

intmonth;

intday;

};

Date:

:

Date(Date&d)

{

year=d.year;

month=d.month;

day=d.day;

}

classStudent

{

public:

Student(intn,char*nam,chars,Dated,char*grd,char*cn,char*xy,char*mj);

voidSetName(char*n){strcpy(name,n);}

voidSetXueyuan(char*xy){strcpy(xueyuan,xy);}

voidSetMajor(char*mj){strcpy(major,mj);}

intGetNo(){returnno;}

stringGetName(){returnname;}

charGetSex(){returnsex;}

stringGetGrade(){returngrade;}

stringGetCno(){returncno;}

stringGetXueyuan(){returnxueyuan;}

stringGetMajor(){returnmajor;}

voidGetBirth()

{

cout<

}

private:

intno;

charname[16];

charsex;

Datebirth;

chargrade[16];

charcno[16];

charxueyuan[32];

charmajor[32];

};

Student:

:

Student(intn,char*nam,chars,Dated,char*grd,char*cn,char*xy,char*mj)

{

no=n;

strcpy(name,nam);

sex=s;

birth=d;

strcpy(grade,grd);

strcpy(cno,cn);

strcpy(xueyuan,xy);

strcpy(major,mj);

}

intmain()

{

Student*stu[N];

intnum=0;

inty,m,d,no;

charsex;

charname[16],grade[16],cno[16],xy[32],mj[32];

inti,choose,choose1=1;

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

cout<<"\t\t1:

录入2:

显示3:

修改0:

退出"<

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

";

cin>>choose;

while(choose!

=0)

{

switch(choose)

{

case1:

cout<<"请按学号、姓名、性别、生日、年级、班级、学院、专业顺序输入学生信息"<

cin>>no>>name>>sex;

cin>>y>>m>>d;

cin>>grade>>cno>>xy>>mj;

stu[num++]=newStudent(no,name,sex,Date(y,m,d),grade,cno,xy,mj);

break;

case2:

cout<<"学号、姓名、性别、生日、年级、班级、学院、专业等信息"<

for(i=0;i

cout<<(*stu[i]).GetNo()<<""<<(*stu[i]).GetName()<<""<<(*stu[i]).GetSex()<

(*stu[i]).GetBirth();

cout<<(*stu[i]).GetGrade()<<""<<(*stu[i]).GetCno()<<""<<(*stu[i]).GetXueyuan()<<""<<(*stu[i]).GetMajor()<

break;

case3:

cout<<"输入学号:

";

cin>>no;

for(i=0;i

{

if(no==(*stu[i]).GetNo())

{

cout<<"\n\n1:

名字修改2:

学院修改3:

专业修改"<

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

"<

cin>>choose1;

switch(choose1)

{

case1:

cout<<"输入姓名:

";

cin>>name;

(*stu[i]).SetName(name);

break;

case2:

cout<<"输入学院:

";

cin>>xy;

(*stu[i]).SetXueyuan(xy);

break;

case3:

cout<<"输入专业"<

cin>>mj;

(*stu[i]).SetMajor(mj);

break;

}

break;

}

elsecout<<"该学生不存在"<

}

break;

}

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

cout<<"\t\t1:

录入2:

显示3:

修改0:

退出"<

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

";

cin>>choose;

}

return0;

}

 

5.实现公司员工的管理。

设计employee类,包含员工基本信息:

编号、姓名、性别、出生日期和职务等。

出生日期使用自定义的Date类。

employee类有可以从外部访问类成员的友元函数。

程序基本功能有:

(1)职工信息的录入。

(2)职工信息的显示。

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

(4)可以修改人员的基本信息。

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

#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;

charname[16];

charsex[8];

Datebirth;

charpos[32];

public:

voidSetname(char*n)

{

strcpy(name,n);

}

voidSetpos(char*n)

{

strcpy(pos,n);

}

employee():

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

{

strcpy(name,"Andy");

strcpy(sex,"male");

strcpy(pos,"intern");

}

employee&operator=(employee&x)

{

no=x.no;

birth.SetYear(x.birth.GetYear());

birth.SetMonth(x.birth.GetMonth());

birth.SetDay(x.birth.GetDay());

strcpy(name,x.name);

strcpy(sex,x.sex);

strcpy(pos,x.pos);

}

employee(intn,char*str,char*y,Dateday,char*p)

{

no=n;

strcpy(name,str);

strcpy(sex,y);

birth=day;

strcpy(pos,p);

}

friendstringGetname(employeex)

{

stringname;

inti;

for(i=0;x.name[i]!

='\0';i++)

name+=x.name[i];

returnname;

}

friendintGetno(employeex)

{

returnx.no;

}

friendstringGetsex(employeex)

{

stringsex;

inti;

for(i=0;x.sex[i]!

='\0';i++)

sex+=x.sex[i];

returnsex;

}

friendDateGetbirth(employeex)

{

returnx.birth;

}

friendstringGetpos(employeex)

{

stringpos;

inti;

for(i=0;x.pos[i]!

='\0';i++)

pos+=x.pos[i];

returnpos;

}

};

intmain()

{

employee*persons[N];

employeetemp;

Dateday;

intnum=0;

inti,choose=1,choose1=0;

intn,y,m,d;

charstr[16];

chars[8];

charp[32];

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

cout<<"\t\t1:

录入2:

显示3:

修改4:

查找0:

退出"<

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

";

cin>>choose;

while(choose!

=0)

{

switch(choose)

{

case1:

if(num==15)

{

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;i++)

{

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

{

cout<<"\n\n1:

名字修改2:

职位修改"<

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

"<

cin>>choose1;

switch(choose1)

{

case1:

cout<<"输入员工姓名:

";

cin>>str;

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

break;

case2:

cout<<"输入员工职位:

";

cin>>p;

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

break;

}

break;

}

elsecout<<"该员工不存在"<

}

break;

case4:

cout<<"输入员工编号:

";

cin>>n;

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

{

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

{

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

cout<

<<""<

break;

}

}

break;

}

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

cout<<"\t\t1:

录入2:

显示3:

修改4:

查找0:

退出"<

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

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

";

cin>>choose;

}

return0;

}

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

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

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

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