面向对象程序设计实验一
面向对象程序设计实训报告书(实训一)

如有余力,可选择:
在以上要求的基础上,设计两个方法,分别根据身份证号确定性别和出生日期,设计一个计算n个学生中成绩最好者、最差者、总成绩和平均成绩。
相关课程
教师评语
教师姓名
赵清山
实训课时数
4
备注
基本算法(用自然语言、图、表的形式,描述实现的基本思路和确定的、有限的、可行的步骤):
实现界面及主要代码:
面向对象程序设计(
完成日期:
学号
姓名
成
实训目的
加深对面向对象方法基本理论的理解。了解类的继承、重载和多态的概念,掌握类的继承、重载和先绑定多态的实现方法。熟练掌握类、对象的定义、访问和访问控制的方法。并能在项目中设计、使用简单的类,初步获得用类解决实际问题的能力。
实训结论与总结:
实训内容
及要求
1、设计一个“人(身份证号,姓名)”类,姓名公开,身份证隐藏,包括3个构建函数,其一,为初始化,其二,只输入身份证号,其三,输入身份证号和姓名。
2、根据人类,派生学生类,学生类添加学号和成绩两个成员,分别添加构造函数获得相应成员。
3、在一般方法中,至少有一个实现重载。
4、设计一个控制台应用或窗体应用,测试两个类的实现并显示1、2两项的信息(身份证号、姓名、学号、成绩)。
面向对象设计原则实验报告实验01

面向对象设计原则实验报告1.1实验目的1. 通过实例深入理解和掌握所学的面向对象设计原则。
2.熟练使用面向对象设计原则对系统进行重构。
3.熟练绘制重构后的结构图(类图)。
1.2实验内容1.在某绘图软件中提供了多种大小不同的画笔(Pen),并且可以给画笔指定不同颜色,某设计人员针对画笔的结构设计了如图1-1所示的初始类图。
通过仔细分析,设计人员发现该类图存在非常严重的问题,即如果需要增加一种新的大小或颜色的笔,就需要增加很多子类,例如增加一种绿色的笔,则对应每一种大小的笔都需要增加一支绿色笔,系统中类的个数急剧增加。
试根据依赖倒转原则和合成复用原则对该设计方案进行重构,使得增加新的大小或颜色的笔都较为方便,请绘制重构之后的结构图(类图)。
2.在某公司财务系统的初始设计方案中存在如图1-2所示的Employee类, 该类包含员工编号(ID),姓名(name),年龄(age).性别(gender)、薪水(salary)、每月工作时数( workHoursPerMonth),每月请假天数(leaveDaysPerMonth)等属性。
该公司的员工包括全职和兼职两类,其中每月工作时数用于存储兼职员工每个月工作的小时数,每月请假天数用于存储全职员工每个月请假的天数。
系统中两类员工计算工资的万法也不一样,全职员工按照工作日数计算工资﹐兼职员工按照工.作时数计算上资﹐内此在 Employee 类中提供了两个方法calculateSalaryByDays()和calculateSalaryByHours(),分别用于按照大数和时数计算工资,此外,还提供了方法displaySalary()用于显示工资。
试采用所学面向对象设计原则分析图1-2中Employee类存在的问题并对其进行重构绘制重构之后的类图。
3.在某图形界面中存在如下代码片段,组件类之间有较为复杂的相互引用关系:如果在上述系统中增加一个新的组件类,则必须修改与之交互的其他组件类的源代码,将导致多个类的源代码需要修改。
面向对象程序设计(C++)实验一--类和对象

实验一类和对象一、实验目的:掌握类的设计和对象的创建及相关知识点如对象数组、类聚合、友元函数、静态成员。
二、实验内容:1、设计圆类,求两个圆的位置关系;2、设计圆类和直线类,求直线与圆的位置关系;3、设计学生类,求某班学生成绩的最高分、最低分及平均分;4、设计部门类和教师类,其中部门类成员为教师类的子对象;5、设计家庭银行账户类,其中账户资金为静态数据成员,为家庭成员共享;6、设计复数类,友元函数实现复数的加减。
三、实验过程:1、根据实验题目确定需要设计哪些类、各类中包含哪些数据成员和函数成员,设计各成员的访问权限,设计数据成员的数据类型,设计函数成员的函数原型;2、给出类设计的程序实现,特别要注意重载构造函数的实现;3、在主函数中完成题目中的逻辑,一般情况下需要创建相关类的对象,通过对象对成员函数的调用或者通过对象指针对成员函数的调用完成相应的功能。
四、参考程序代码:1、设计圆类,求两个圆的位置关系#include <iostream>#include <math.h>using namespace std;class Circle{private:float a,b,r;public:Circle(float c,float d,float e){a=c;b=d; r=e;}friend void position(Circle &,Circle &);};void position(Circle &c1,Circle &c2){float n,m,p;p=sqrt((c1.a-c2.a)*(c1.a-c2.a)+(c1.b-c2.b)*(c1.b-c2.b)); n=fabs(c1.r-c2.r);m=c1.r+c2.r;if(p>m)cout << "相离";if(p==m)cout << "相切";if(p<m)cout << "相交";}int main(){float a,b,c,d,e,f;cin >> a >> b >> c >> d >> e >> f ;Circle c1(a,b,c),c2(d,e,f);position(c1,c2);return 0;}运行结果:2、设计圆类和直线类,求直线与圆的位置关系#include<iostream>#include<math.h>using namespace std;class Circle//圆类{private:float x,y,r;public:Circle(float a,float b,float c) {x=a;y=b;r=c;}float getx(){return x;}float gety(){return y;}float getr(){return r;}};class Line//直线类{private:float a,b,c;public:Line(float x,float y,float z){a=x;b=y;c=z;}void position(Circle c1){float d=fabs(a*c1.getx()+b*c1.gety()+c)/sqrt(a*a+b*b) ;//圆心到直线的距离if(d<c1.getr())cout << "相交";if((d-c1.getr())<0.000001)cout << "相切";cout << "相离";}};int main(){Line l1(3,3,4);//创建直线对象Circle c1(-23,3,7);//创建圆对象l1.position(c1);return 0;}运行结果:3、设计学生类,求某班学生成绩的最高分、最低分及平均分#include <iostream>#include <string.h>using namespace std;class Student{private:char name[20],xuehao[20];float math;public:Student(){}Student(char *na,char *xu,float ma):math(ma) {strcpy(name,na);strcpy(xuehao,xu);}void set(char *na,char *xu,float ma){math=ma;strcpy(name,na);strcpy(xuehao,xu);}char *get_name(){return name;}char *get_xuehao(){return xuehao;float get_math(){return math;}};void max_min(Student stu[],int n){Student max,min;max=min=stu[0];for(int i;i<n;i++){if(stu[i].get_math()>max.get_math())max=stu[i];if(stu[i].get_math()<min.get_math())min=stu[i];}cout << "学生成绩最高:" << max.get_name() << "," << max.get_xuehao() << "," << max.get_math() << endl;cout << "学生成绩最低:" << min.get_name() << "," << min.get_xuehao() << "," << min.get_math() << endl;}void ave(Student stu[],int n){float sum=0;for(int i=0;i<n;i++){sum=sum+stu[i].get_math();cout << "ave=" << sum/n;}int main(){char na[20],xu[20];float ma;Student stu[20];int n;cout << "请输入学生总人数"; cin >> n;for(int i=0;i<n;i++){cin >> na >> xu >> ma ; stu[i].set(na,xu,ma); }max_min(stu,n);ave(stu,n);return 0;}运行结果:4、设计部门类和教师类,其中部门类成员为教师类的子对象#include <iostream>#include <string.h>using namespace std;class Department{private:int dep_num;char dep_master[20];char dep_name[20];public:Department(){}Department(int num,char *ma,char *na){dep_num=num;strcpy(dep_master,ma);strcpy(dep_name,na);}void show(){cout << dep_num << "," << dep_master << "," << dep_name ; }};class Teacher{private:char name[20];char haoma[20];Department dep;public:Teacher(char *n,char *nu,int num,char *ma,char *na):dep(num,ma,na) {strcpy(name,n);strcpy(haoma,nu);}void show(){dep.show();cout << "," << name << "," << haoma << endl;}};int main(){Teacher hp("wanghai","12345",6,"kiki","computer");hp.show();return 0;}运行结果:5、设计家庭银行账户类,其中账户为静态数据成员,为家庭成员共享#include <iostream>#include <string.h>using namespace std;class Familymember//家庭成员类{private:char membername[10];//成员名public:static float account;//共享账户资金 Familymember(char *name);static void save(float money);//存钱 static void pay(float money);//支出 static void show(); //显示账户余额};float Familymember::account=0;Familymember::Familymember(char *name) {strcpy(membername,name);}void Familymember:: save(float money) {account= account+money;}void Familymember:: pay(float money){account= account-money;}void Familymember:: show(){cout<<account<<endl;}int main()//主函数{ Familymember husband("wang"),wife("Li"); husband.save(10000);wife.pay(3000);wife.save(5000);wife.show();husband. show();}运行结果:6、设计复数类,友元函数实现复数的加减#include <iostream>using namespace std;class Fushu{private:float ss,xs;public:Fushu(){}Fushu(float a,float b){ss=a;xs=b;}friend Fushu jiafa(Fushu &f1,Fushu &f2){return Fushu((f1.ss+f2.ss),(f1.xs+f2.xs)); }friend Fushu jian(Fushu &f1,Fushu &f2){return Fushu((f1.ss-f2.ss),(f1.xs-f2.xs)); }void show(){cout << ss << " + " << xs << "i" << endl; }};int main(){Fushu f1(2,3),f2(4,5);Fushu f3,f4;f3=jiafa(f1,f2);f4=jian(f1,f2);f3.show();f4.show();return 0;}运行结果:。
面向对象程序设计实验报告1

实验一
实验名称:实验一Visual C++环境
实验目的:
⒈熟悉VC++的集成开发环境,学习运行一个C++程序的步骤。
y=fun(x);
if(y==0)
cout<<"they input num is sushu"<<endl;
else
cout<<"they input num is not sushu"<iostream.h>
#include<math.h>
float fun(float a,float b,float c,float d)
return 0;
3. #include<iostream.h>
void main()
{inta,b,c,d,e;
double score;
a=b=c=d=0;
cin>>score;
while(score>=0)
{
if(score>=90)
a++;
else
if(score>=80)
b++;
else
if(score>=70)
do{
*p=*(--p);
}while( p>s+n);
(*p)=c;
}
return s;
}
int main()
C++面向对象程序设计》实验报告

《C++面向对象程序设计》实验内容实验1 C++程序设计初步1.实验目的(1)了解在C++编译系统(Visual C++6.0)上如何编辑、编译、连接和运行一个C++程序。
(2)通过运行简单的C++程序, 初步了解C++源程序的结构和特点。
(3)掌握简单C++程序的编写和调试方法。
(4)掌握重载函数的定义方法。
(5)能正确使用引用型变量。
2.实验内容和步骤(1)在Visual C++环境下编译和运行C++程序①先进入Visual C++6.0环境。
②在自己指定的子目录中建立一个名为test.cpp的新文件。
③从键盘输入以下程序int main(){int a,b;c=a+b;cout>> “a+b=”>>a+b;}选择Build→Compile test.cpp命令, 对此源程序进行编译。
观察和分析编译信息。
⑤根据编译信息指出的错误, 修改程序。
再进行编译, 如果还有错, 再重复此过程, 直到编译不出错为止。
⑥选择Build→Build test.exe命令, 对程序进行连接, 如果不出错, 就会生成可执行程序test.exe。
⑦选择Build→Execute test.exe命令, 执行可执行程序test.exe。
观察屏幕的变化。
在输出窗口应显示程序运行结果。
⑧分析结果是否正确, 如果不正确或认为输出格式不理想, 可以修改程序, 然后重新执行以上④和⑧步骤。
改过后的程序:#include<iostream>using namespace std;int add(int x,int y){int z;z=x+y;return(z);}int main(){int a,b,c;cin>>a>>b;c=add(a,b);cout<<"a+b="<<c<<endl;return 0;}实验2 C++对C的扩充(2)编一个程序, 用来求2个或3个正整数中的最大数。
面向对象程序设计实验(完整版)

实验结果
实验五
利用接口继承完成对Biology(生物),Animal(动物),Man(人)三个接口的定义
其中Biology接口定义一个breath()抽象方法,Animal接口定义sex()
和eat()两个抽象方法,Man接口定义think()和study()两个抽象方法;
定义一个NormalMan类实现上述三个接口定义的抽象方法。
实验六
实验名称:异常处理机制
实验目的:熟悉和掌握java中的异常的概念;熟悉和掌握java中异常的处理机制;熟悉和掌握异常的抛出和捕获。
实验内容:
1.上机输入以下程序,观察程序的运行结果,体会Java中的异常处理机制。
class Test{
public static void main(String[]args){
try{
int a=3 fb=0;
System.out.println(a=a/b);
} ‘
catch(ArithmeticException e){
System.out.println(”捕捉到一个算术异常”);
}
catch(Exception e){
System.out.println(”捕捉到一个系统一般的异常”);
}
finally{
System.out.println(“程序结束”);
}
}
}
思考:
(1)系统中哪条语句会抛出异常?哪条语句捕捉了异常?为什么程序不会打印出“捕
捉到一个系统一般的异常”?
(2)finally语句块中的语句一定会被执行吗?将程序中的变量b改成非零值,程序也会打印出“程序结束"吗?
(3)将两个catch语句块交换位置,程序能够编译通过吗?系统将给出什么错误提示?
实验报告实例

上海理工大学光电信息与计算机工程学院《面向对象程序设计实验》实验报告专业姓名学号年级指导教师成绩:教师签字:目录实验一面向对象开发环境C++基础实验二C++语言进阶实验三面向对象高级特性实验成绩细化表实验一面向对象开发环境基础一、实验目的(1)了解C++的开发环境,学会独立使用该系统。
(2)了解在该系统上如何编辑、编译、连接和运行一个C++程序。
(3)通过运行简单的C++程序,初步了解C++源程序的基本组成。
.(4)掌握C++中基本数据类型。
(5)掌握C++中各种运算符的作用、优先级和结合性,能熟练运用各种表达式。
(6)掌握不同数据类型的输入输出。
(7)掌握简单的语法错误的查找和改正方法。
二、实验设备PC机,操作系统Windows 8,开发环境Visual Studio 2010三、实验原理配套教材实验一、实验二四、实验源程序//2-35#include <iostream>#include <iomanip>using namespace std;int main(){int x,y,ans;for(x=1;x<10;x++){for(y=1;y<10;y++){ans=x*y;cout<<x<<"*"<<y<<"="<<setw(2)<<ans<<"";}cout<<endl;}system("pause");return 0;}五、自评良好实验二C++语言进阶一、实验目的(1)熟练掌握if语句、switch语句、for语句、while及do-while语句(2)掌握转移控制语句(3)理解编译预处理(4)掌握函数的定义和调用(5)掌握数组的定义和使用(6)掌握结构和枚举类型的定义方法和使用(7)掌握指针的使用方法及应用(8)掌握指针与数组(9)掌握指针传递和引用传递二、实验设备PC机,操作系统Windows 8,开发环境Visual Studio 2010三、实验原理配套教材实验三、实验五、实验六四、实验源程序//3-2#include<iostream>#include<stdlib.h>using namespace std;int Max1(int a,int b){if(a>b)return(a);elsereturn(b);}int Max1(int a, int b,int c){if(a>b){if(a>c)return(a);}else{if(b>c)return(b);elsereturn(c);}}double Max1(double x,double y){if(x>y)return(x);elsereturn(y);}double Max1(double x,double y,double z) {if(x>y){if(x>z)return(x);}else{if(y>z)return(y);elsereturn(z);}}int main(){int a,b,c;double x,y,z;cout<<"请输入两个整数:";cin>>a>>b;cout<<"最大值为:"<<Max1(a,b)<<endl; cout<<"请输入三个整数:";cin>>a>>b>>c;cout<<"最大值为:"<<Max1(a,b,c)<<endl; cout<<"请输入两个双精度数:";cin>>x>>y;cout<<"最大值为:"<<Max1(x,y)<<endl; cout<<"请输入三个双精度数:";cin>>x>>y>>z;cout<<"最大值为:"<<Max1(x,y,z)<<endl;system("pause");return 0;}//5-14#include<iostream>usingnamespace std;class Boat;class Car{private:int weight;public:Car(int j){ weight=j;}friendint getTotalWeight(Car &aCar,Boat &aBoat);};class Boat{private:int weight;public:Boat(int j){ weight=j;}friendint getTotalWeight(Car &aCar,Boat &aBoat);};int getTotalWeight(Car &aCar,Boat &aBoat){ return aCar.weight+aBoat.weight;}int main(){Car c(10);Boat b(7);cout<<"The total weight is : "<<getTotalWeight(c,b)<<endl; system("pause");return 0;}//6-6#include<iostream>#include<string>usingnamespace std;class Employee{private:char name[30];char stree[30];char city[30];char zip[6];public:Employee();void change_name();void display();};Employee::Employee(){strcpy(name,"lianmeng");strcpy(stree,"piamganjie3hao");strcpy(city,"shanghai");strcpy(zip,"900000");}void Employee::change_name(){cout<<"Enter a name:";cin>>name;}void Employee::display(){cout<<"\n"<<name<<"\t"<<stree<<"\t"<<city<<"\t"<<zip<<"\t"; }int main(){int i;Employee *p=new Employee[5];for(i=0;i<5;i++){p[i].display();}for(i=0;i<5;i++){cout<<"\nnumber"<<i+1<<" : "<<endl;p[i].change_name();}for(i=0;i<5;i++){ p[i].display(); }delete p;system("pause");return 0;}五、自评良好实验三面向对象高级特性一、实验目的(1)掌握类与对象的定义(2)掌握对象成员的访问、构造函数和析构函数(3)理解基类和派生类(4)掌握单一继承和多重继承的定义和使用方法(5)掌握运算符重载程序的编写(6)掌握虚函数的编写和使用(7)掌握流类库与输入/输出;异常处理二、实验设备PC机,操作系统Windows 8,开发环境Visual Studio 2010 三、实验原理配套教材实验四、七、八、十一四、实验源程序//4-8#include<iostream>usingnamespace std;class Dog{public:Dog(int nage=0,int nweight=0){age=nage;weight=nweight;}Dog(Dog &d);inlinevoid showstatus();void setstatus();private:int age,weight;};Dog::Dog(Dog &d){age=d.age;weight=d.weight;}inlinevoid Dog::showstatus(){cout<<"His status is:"<<endl<<"age:"<<age<<"\tweight:"<<weight<<"kg"<<endl; }void Dog::setstatus(){int na,nw;cout<<"Please set age and weight:";cin>>na>>nw;age=na;weight=nw;}int main(){Dog Zangming(19,60);cout<<"Here is a dong Zangming!!"<<endl;Zangming.showstatus();cout<<"Please reset his status!"<<endl;Zangming.setstatus();cout<<"\n\nNow:"<<endl;Zangming.showstatus();system("pause");}//7-6#include<iostream>usingnamespace std;class Mammal{public:Mammal(int a,int w):age(a),weight(w){cout<<"Mammal Constructor called..."<<endl;}~Mammal(){cout<<"Mammal Destructor called..."<<endl;}private:int age;int weight;};class Dog:public Mammal{public:Dog(int a,int w):Mammal(a,w){cout<<"Dog Constructor called..."<<endl;}~Dog(){cout<<"Dog Destructor called..."<<endl;}};int main(){{cout<<"Constructing a Mammal..."<<endl;Mammal mammal(8,12);cout<<endl<<"Constructing a Dog..."<<endl;Dog dog(3,9);cout<<endl<<"Destructing......"<<endl;}system("pause");return 0;}//8-5#include<iostream>usingnamespace std;class Mammal{public:Mammal(){cout<<"Mammal Constructor..."<<endl;} ~Mammal(){cout<<"Mammal Destructor..."<<endl;} virtualvoid speak()=0;};class Dog:public Mammal{public:Dog(){cout<<"Dog Constructor..."<<endl;}~Dog(){cout<<"Dog Destructor..."<<endl;}virtualvoid speak();};void Dog::speak(){cout<<"Dog's sound...."<<endl;}int main(){Dog *pdog=new Dog;pdog->speak();delete pdog;system("pause");return 0;}//11-5#include<fstream>#include<iostream>usingnamespace std;int main(){ofstream file1("test.txt",ios::app);file1<<"已成功添加字符!";file1.close();char ch;ifstream file2("test.txt");while(file2.get(ch))cout<<ch;file2.close();system("pause");return 0;}五、自评良好实验成绩细化表。
面向对象程序设计实验一及实验报告样式

《面向对象程序设计》实验一实验报告1 实验目的:(1)了解在VC++、C-free中如何编辑、编译、连接和运行一个C++程序;(2)掌握声明类的方法,类和类的成员的概念以及定义对象的方法(3)了解多文件的C++程序结构2 实验任务(1)输入两个整数,将它们按由大到小的顺序输出。
要求使用变量的引用;(2)编写一个程序,将两个字符串连接起来,结果取代第一个字符串;(3)声明Animal 类,数据成员有age,成员函数有SetAge()等。
首先把数据成员声明为公有的,在main()函数中访问,再把它改为私有的,指出在main()函数中访问会出现怎样的情况,如何避免?该程序需要在同一个文件中分别给出类的声明、类的实现、类的演示每部分的代码。
(4)声明日期类,数据成员有year、month、day,成员函数有输出日期、加一天等。
练习使用多文件结构来组织程序。
3 程序清单实验一:#include"iostream"using namespace std;int main(){int i,j;cout<<"input two number"<<endl;cin>>i>>j;swap(i,j);cout<<i<<","<<j<<endl;return 0;}void swap(int &a,int &b){int temp;temp=a;a=b;b=temp;}实验二:#include"iostream"#include"string"using namespace std;int main(){string string1,string2;cout<<"please input two string:";cin>>string1>>string2;string1=string1+string2;cout<<string1<<endl;return 0;}实验三:4 运行结果图1.1 任务一运行结果图2.1任务二运行结果5 总结或感悟(对运行结果所作的分析以及本次调试程序所取得的经验。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
贵州大学实验报告
学院:计算机科学与信息专业:通信工程班级:通信091
实验数据1 •设计并测试一个名为Rectangle的矩形类,其属性为矩形的左下角和右上角
两个点的坐标,能计算矩形的面积。
参考程序:
#include<iostream>
using namespace std;
class Rectangle{
public:
Rectangle(int xx1=0,int yy 1= OJnt xx2=3,int yy2=2)
{x1= XX1;
y»= yyi;
x2=xx2;
y2=yy2;
};
int area();
private:
int x1,y1,x2,y2;
int Rectangle::area()
{
return (x2_x1)*(y2_y1);
}
int main(){
int x1 ,y1 ,x2,y2;
cout «H please enter the value of the two points";
cin»x1;
cin»y1;
cin»x2;
cin»y2;
Rectangle myRectangle(x1 ,y1 ,x2,y2);
cout«H the area of myRectangle is:n«myRectangle.area()vvendl;
return 0;
}
2声明一个Circle类,有数据成员Radius (半径),成员函数GetArea (),计算圆的面积,构造一个Circle的对象进行测试。
参考程序:
#in clude<iostream>
using namespace std;
const float Pl=3.14151926;
class Circle{
public:
Circle(float r1)
};
float Circle::area()
{
return 2*PI*r;
}
int main(){
int radium;
cout «"please enter the value of radium";
cin> >radium;
Circle myCircle(radium);
cout«H the area of myCircle is:,,«myCircle.area()«endl; return 0;。