- 1、本文档共14页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
实验10 思考题
实验10 类与对象(二)
三、实验思考
1.定义一个圆类,计算圆的面积和周长。要求分别用成员函数和友元函数来求圆的面积和周长。
#includeiostream
using namespace std;
class Circle
{
public:
Circle(float,float,float);
float area();
float perimeter();
friend float area(Circle );
friend float perimeter(Circle );
private:
float x;
float y;
float r;
};
Circle::Circle(float a,float b,float c)
{
x=a;
y=b;
r=c;
}
float Circle::area()
{
return 3.14159*r*r;
}
float Circle::perimeter()
{
return 2*3.14159*r;
}
float area(Circle c)
{
return 3.14159*c.r*c.r;
}
float perimeter(Circle c)
{
return 2*3.14159*c.r;
}
int main()
{
Circle cir(5.0,4.0,3.0);
coutcir.area()endl;
coutcir.perimeter()endl;
coutarea(cir)endl;
coutperimeter(cir)endl;
return 0;
}
2.定义Boat与Car两个类,两者都有weight属性,定义两者的一个友元函数totalWeight()为外部函数计算者的重量和。#include iostream
using namespace std;
class Car;
class Boat
{
public:
Boat(int a):weight(a){}
friend int totalWeight(Boat boat, Car car);
private:
int weight;
};
class Car
{
public:
Car(int a):weight(a){}
friend int totalWeight(Boat boat, Car car);
private:
int weight;
};
int totalWeight(Boat boat, Car car)
{
return boat.weight + car.weight;
}
int main()
{
Boat boat(20);
Car car(10);
coutThe weight is: ;
couttotalWeight(boat,car)endl;
return 0;
}
3.设计一个Book类,包含两个私有数据成员count和price,建立一个含有5个元素的数组对象,将count初始化为1,2,3,4,5,将price初始化为10,15,20,25,30,显示每个对象的count和price值。
#includeiostream
using namespace std;
class Book
{
public:
Book(int c,int p):count(c),price(p){}
void output();
private:
int count;
int price;
};
void Book::output()
{
coutcount priceendl;
}
int main()
{
Book b[5]={Book(1,10),Book(2,20),Book(3,30),Book(4,40),Book(5,50)};
for(int i=0;i5;i++)
b[i].output();
return 0;
}
4.设计一个Student类,包含学生的基本信息:学号、姓名、性别、出生日期、年级、班Student类有多个构造函数:缺省构造函数、带参数的构造函数、带默认参数的构造函数。类的基本功能有:
(1)使用对象数组保存学生对象。
(2)从键盘输入学生的基本信息。
(3)修改学生的基本信息。
(4)显示学生信息。
#includeiostream
#includestring
#includestring.h
#define N 100
using namespace std;
class Date
{
public:
Date(){}
Date(int y,int m,int d):year(y),mon
您可能关注的文档
最近下载
- 丽声北极星自然拼读绘本第二级 Eggs for Ben 课件2.pptx
- 第09课 美丽的颜色(公开课一等奖创新教学设计).docx
- 2024年危险化学品生产企业安全试题(安全管理人员)真题题库附解析答案.docx
- 2022-2023学年宁波市鄞州区八年级上学期期中数学试卷(含答案解析).docx
- 工程项目分包合同书样本.doc
- 轻骑士橡筋制作_航模系列.ppt
- 兽医检验复习试题.doc
- DB42_T 2163-2023 水利工程质量监督规程.doc
- 金井“茶乡小镇”城镇建设及旅游开发一期PPP项目-招标文件.doc
- 人教版初中美术九年级上册《异彩纷呈的中国古代雕塑、工艺和建筑》优课比赛课件.pptx VIP
文档评论(0)