面向对象类和对象实验二

上传人:平*** 文档编号:12421955 上传时间:2017-10-19 格式:DOC 页数:11 大小:213.79KB
返回 下载 相关 举报
面向对象类和对象实验二_第1页
第1页 / 共11页
面向对象类和对象实验二_第2页
第2页 / 共11页
面向对象类和对象实验二_第3页
第3页 / 共11页
面向对象类和对象实验二_第4页
第4页 / 共11页
面向对象类和对象实验二_第5页
第5页 / 共11页
点击查看更多>>
资源描述

《面向对象类和对象实验二》由会员分享,可在线阅读,更多相关《面向对象类和对象实验二(11页珍藏版)》请在金锄头文库上搜索。

1、福建农林大学计算机与信息学院实验报告系: 计算机 专业: 计算机科学与技术 年级: 2011 级 姓名: 学号: 实验室号 田 507、513、514 计算机号 36 实验时间: 2011 年 10 月 11、18 日 指导教师签字: 成绩: 实验 2 类和对象一、实验目的和要求(1)掌握声明类的方法,类和类的成员的概念以及定义对象的方法。(1)掌握类、类的数据成员、类的成员函数的定义方式,理解类成员的访问控制方式。(2)掌握对象的定义和操作对象的方法。(3)掌握构造函数和析构函数的定义与执行过程。(4)掌握对象数组、对象指针的概念及使用方法。(5)掌握静态成员的基本使用方法。(6)掌握友元的

2、使用方法,进一步理解其概念和作用。二、实验内容和原理1定义一个猫类( Cat ) ,具有名字(c_name) 、年龄 (age) 、体重( weight )和颜色( color )等属性,以及设置、获取和输出这些属性的功能。(类的基础)2定义一个人类(Person) ,具有名字(p_name ) 、年龄 (age)、住址(address)等属性,猫类对象 cat1 为其的子对象,用成员函数实现对数据的初始化、获取和输出这些属性的功能。(子对象的应用)3在人的住宅附近有个小型的运动场所,可供猫活动,但是器械有限,只有 3 件,思考怎样改进类结构,解决资源共享问题。 (静态成员应用)4思考如何实现

3、一人有任意只猫(1 只以上)的子对象结构,定义 Person 类对象的时候,给出猫的数量。 (堆对象的应用) 。5修改 2 中的类结构,使得 Person 成为 Cat 的友元类,在 Person 中实现 cat 类的属性的设置,获取和输出功能。 (友元的应用)三、实验环境联想计算机,Windows XP 操作系统,Visual C+ 6.0四、算法描述及实验步骤(1)编写源程序。(2)检查程序有无错误(包括语法错误和逻辑错误),有则改之。(3)编译和连接,仔细分析编译信息,如有错误应找出原因并改正之。(4)运行程序,上机调试并运行,分析结果。(5)将调试好的程序保存在自己的用户目录中,文件名

4、自定。五、类视图1234.5六、实验结果1试验结果12345.七、总结通过这次的实验学会了关于类、子对象、继承机制中的知识点,对该门课程有了进一步的了解。八、附录:1代码#include #include using namespace std;class Cat public:Cat(string,int,float,string) ;void display();private:string c_name;int age;float weight;string color;Cat:Cat(string a,int b,float c,string d) c_name=a;age=b;wei

5、ght=c;color=d;display();void Cat:display()cout#include using namespace std;class Cat private:string c_name;int age;float weight;string color;public:Cat(string,int,float,string);void display();class Person Cat cat1;string p_name,address;int age;public:Person(string p_n,string add,int p_a,string c_n,i

6、nt a,int w,string c) :cat1(c_n,a,w,c)p_name=p_n;address=add;age=p_a;cout#includeusing namespace std;class Catpublic:Cat(string,int,float,string);void display(); int getresource();void freeresource();private:string c_name;int age;float weight;string color;static int resource;int Cat:resource=0;Cat:Ca

7、t(string c_n,int a,float w,string c) c_name=c_n;age=a;weight=w;color=c;int Cat:getresource() if(resource=3) return 0;else resource+; return 1;void Cat:freeresource() resource-;cout#include using namespace std;class Cat private:string c_name;int age;float weight;string color;public:Cat(); Cat(string,

8、int,float,string);void setc_name(string);void setage(int);void setweight(float);void setcolor(string);void display();class Person Cat *cat1;string p_name;public:Person(string p_n,int n)p_name=p_n;coutc_nawc;cat1i.setc_name(c_n); cat1i.setage(a);cat1i.setweight(w);cat1i.setcolor(c); ;void Cat:setc_na

9、me(string c_n)c_name=c_n;void Cat:setage(int a)age=a;void Cat:setweight(float w)weight=w;void Cat:setcolor(string c)color=c;Cat:Cat(string c_n,int a,float w,string c)c_name=c_n;age=a;weight=w;color=c;display();void Cat:display()cout#include using namespace std;class Cat private:string c_name;int age

10、;float weight;string color;public:friend class Person;Cat();Cat(string,int,float,string);void display();class Person Cat cat1;string p_name;public:Person();void setcat(string,int,float,string);Person(string p_n,string c_n,int a,int w,string c):cat1(c_n,a,w,c) coutp_name have a catendl;void Person:se

11、tcat(string c_n,int a,float w,string c)cat1.c_name=c_n;cat1.age=a;cat1.weight=w;cat1.color=c;cat1.display();Cat:Cat(string c_n,int a,float w,string c) c_name=c_n;age=a;weight=w;color=c;display();void Cat:display()cout猫的名字:c_name 年龄:age 重量:weight 颜色:colorendl;void main()Person p1;p1.setcat(lala,3,4,yellow);

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号