C语言程序设计

上传人:工**** 文档编号:474224630 上传时间:2022-12-28 格式:DOC 页数:32 大小:900.04KB
返回 下载 相关 举报
C语言程序设计_第1页
第1页 / 共32页
C语言程序设计_第2页
第2页 / 共32页
C语言程序设计_第3页
第3页 / 共32页
C语言程序设计_第4页
第4页 / 共32页
C语言程序设计_第5页
第5页 / 共32页
点击查看更多>>
资源描述

《C语言程序设计》由会员分享,可在线阅读,更多相关《C语言程序设计(32页珍藏版)》请在金锄头文库上搜索。

1、 计算机科学与技术系上机实验报告课 程:C+语言程序设计老 师:姓 名:(老挝留学生)班 级:计科 101 班学 号:学 院:计算机科学与信息学院 实验日期: 年 月 日实验一一、实验名称类和对象二、实验目的及要求设计一个类,并对其属性进行操作。三、实验环境Microsoft Visual Studio 2010四、实验内容1,定义一个dog类,包含age,weight等属性。以及对这些属性的操作方法。实现并测试这个类。2,设计一个rectangle类,其属性为矩形的左下角与右上角的坐标,根据坐标计算矩形的面积。五、算法描述及实验步骤Dog+Dog(n: string , ag: int ,w

2、e: int)+get()+show()+Dog()-name: string-weight: int-age: intRectangle+get(): void+show(): void-X1:int-X2: int-Y1: int-Y2: int六、调试过程及实验结果1, 保存源程序代码,并声称解决方案。2,调试并执行。3,输出为:the message of dog is:name:tutu age:2 weight:20input the name age and weight of dog花花 3 60the message of dog is:name:花花 age:3 weigh

3、t:60the message of dog is:name:hua age:4 weight:60calledcalled请按任意键继续. . .输入左下角的坐标:3 6输入右上角的坐标:4 7两点的坐标左下角的坐标:(3,6)右上角的坐标:(4,7)面积为:1输入左下角的坐标:1 0输入右上角的坐标:2 6两点的坐标左下角的坐标:(1,0)右上角的坐标:(2,6)面积为:6请按任意键继续. . .七、总结1,构造函数用于对对象的初始化,在定义类时,如果没有定义构造函数,系统将自动生成一个简单的构造函数。2,构造函数没有返回值,不允许显示调用,创建对象时,系统将自动调用相应的构造函数。3,析

4、构函数是对对象进行最后的清理工作,它不允许有参数,没有返回值。如果没有定义析构函数,系统将自动生成。4,对象的私有成员,可以通过成员函数访问,类外不能访问对象的私有成员。八、附录1, Dog.h#include#includeusing namespace std;class Dogpublic:Dog(string n,int ag,int we);void get();void show();Dog()coutcalled endl;private:string name;int weight;int age;Dog.cpp#include4-8.hDog:Dog(string n,int

5、 ag,int we):name(n),age(ag),weight(we)void Dog:get()coutinput the name age and weight of dognameageweight;void Dog:show()coutthe message of dog is:endl;coutname:name age:”age” weight:”weightendl;Dogmain.cpp#include4-8.hint main()Dog dog(tutu,2,20);dog.show();dog.get();dog.show();Dog dog1(hua,4,60);d

6、og1.show();return 0;2, rectangle.h#include#includeusing namespace std;class Rectanglepublic:void get();void show();private:int x1,x2,y1,y2;Rectangle.cpp#include4-9.hvoid Rectangle:get()cout输入左下角的坐标:x1y1;cout输入右上角的坐标:x2y2;void Rectangle:show()cout两点的坐标endl;cout左下角的坐标:(x1,y1)endl;cout右上角的坐标:(x2,y2)end

7、l;cout面积为:(x2-x1)*(y2-y1)endl;Rectangle main.cpp#include4-9.hint main()Rectangle r1;r1.get();r1.show();Rectangle r2;r2.get();r2.show();return 0;实验二一、实验名称类与对象二、实验目的及要求熟悉设计简单类及测试的方法。三、实验环境Microsoft Visual Studio 2010四、实验内容1,设计一个人事管理类,其属性有编号、性别、出生年月、身份证号等其中出生年月声明为一个日期类内嵌子对象。实现对成员信息的录入和输出。要求包括:构造函数析构函数、

8、复制构造函数、内联成员函数、带默认形参值的成员函数、类的组合。2,定义一个复数类complex,是下面的程序代码能够工作:Complex c1(3,5); /用复数5+3i初始化c1Complex c2=4.5; /用实数4.5初始化c2c1.add(c2); /将c1与c2相加,结果保留在c1中C1.show(); /将c1输出,结果应为7.5+5i五、算法描述及实验步骤1,Date+date(y:int=0,m:int=0,d:int=0)+get():void+show():void+date()-year:int-month:int -day:intHumain+humain()+hu

9、main(p: &humain)+get(p :&humain):void+show(p:&humain):void-berthday:date-name:string-CD:string-six:string-number:string2,Complex+complex(a:float=0,b:float=0)+add(p:&complex):void+show(p:&complex):void-real:float-imag:float六、调试过程及实验结果1, 保存源程序代码,并声称解决方案。2,调试并执行。3,输出为:the first one before setname:six:C

10、D:number:berthday:0/0/0the first one after setinput the name six CD and number:name:喻福松six:男CD:5210452015number:1008060028input berthday:input year month and day1990 5 6name:喻福松six:男CD:5210452015number:1008060028berthday:1990/5/6p2name:喻福松six:男CD:5210452015number:1008060028berthday:1990/5/6called da

11、tecalled date请按任意键继续. . . number:7.5+5i请按任意键继续.七、总结1,类的组合 当当前类所使用的类还没有定义时,在当前类中只能建立此类的引用。如果已经定义在前,就可以直接建立对象。2,访问子对象中的可访问成员用“当前类子对象成员”的形式。3,内联成员函数:用关键字“inline”声明,在程序调用内联成员函数时,并不真正执行函数的调用过程,如保留返回地址等处理,而是把函数代码嵌入程序的调用点,这样可以减少调用成员函数的时间。4,带默认参数的函数: 由于函数调用时,实参和形参的结合是从左到右顺序进行的,因此指定默认值的参数必须放在参数列表的最右端。八、附录1,h

12、umainh 文件#include#includeusing namespace std;class datepublic:date(int y=0,int m=0,int d=0);void get ();void show();date()cout called dateendl;private:int year;int month;int day;lass humainpublic:humain();humain(humain &p);void get(humain &p);void show(humain &p);private:date berthday;string name;st

13、ring CD;string six;string number;Humaincpp文件#include4-10.hdate:date(int y,int m,int d):year(y),month(m),day(d)void inline date:get() coutinput year month and dayyearmonthday;void date:show() coutyear/month/dayendl;humain:humain()name= ;CD= ;six= ;number=;humain:humain(humain &p)berthday=p.berthday;name=p.name;CD=p.CD;six=p.six;number=

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

当前位置:首页 > 资格认证/考试 > 自考

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