天津理工大学计算机科学与技术学院实验报告至学年第学期课程名称C++程序设计应用学号学生姓名年级专业物联网教学班号实验地点实验时间主讲教师辅导教师实验(一)实验名称类和简单对象Visual C++ 6.0软件环境无硬件环境实验目的(1)理解类和对象的概念;(2)掌握类与对象的定义方法;(3)理解类的成员的访问控制的含义,公有和私有成员的区别;(4)掌握构造函数和析构函数的含义与作用、定义方式和实现;(5)能够根据给定的要求定义类并实现类的成员函数;(6)掌握string类的使用方法(7)了解C++面向对象程序设计的基本思想、基本方法和基本步骤;(8)掌握MS Visual C++6.0调试C++程序的基本方法、基本步骤。
实验内容(应包括实验题目、实验要求、实验任务等)1.(1)记录程序的运行结果(2)将Coordinate类中带有两个参数的构造函数进行修改,在函数体内增加如下语句:cout<<”Constructor is called.”<<endl;重新记录程序的运行结果,并解释输出结果。
实验过程与实验结果(可包括实验实施的步骤、算法描述、流程、结论等)Coordinate::Coordinate(int a){}int main(){Coordinate p4;Coordinate p1(3,4);Coordinate p2(p1);Coordinate p5(2);Coordinate p3=p2;cout<<"p3=("<<p3.getx()<<","<<p3.gety()<<")"<<endl;return 0;}2.完整程序如下:#include <iostream.h>class Date{public:void printDate(); //显示日期void setDay(int); //设置日的值void setMonth(int); //设置月的值void setYear(int); //设置年的值private:int day, month, year;};void Date::setDay(int d){day=d;}void Date::setMonth(int m){month=m;}void Date::setYear(int y){year=y;}void Date::printDate(){cout<<"Today is "<<year<<'.'<<month<<"."<<day<<endl;}int main(){Date testDay;testDay.setDay(5);testDay.setMonth(10);testDay.setYear(2010);testDay.printDate();return 0;}3.完整程序如下:#include <iostream>using namespace std;class coutner{public:counter(int number); //构造函数void increment(); //给value原值加1void decrement(); //给value原值减1int getvalue(); //取得计数器值int print(); //显示计数private:int value; //数据成员};coutner::counter(int o){value=o;}void coutner::increment(){value++;}void coutner::decrement(){value--;}int coutner::getvalue(){return value;}int coutner::print(){cout<<"The value is "<<value<<endl;return value;}int main(){int output;coutner c;c.counter(0);c.increment();output=c.getvalue();cout<<"The output(get from value) is "<<output<<endl;c.decrement();c.print();c.increment();output=c.print();cout<<"The output(get from print()) is "<<output<<endl;return 0;}结果如下:4.#include <iostream>using namespace std;class Cls{private:int x;int y;public:Cls();Cls(int);Cls(int,int);int getx();int gety();void setx(int);void sety(int);void print();};int Cls::getx(){return x;}int Cls::gety(){return y;}void Cls::setx(int a){x=a;}void Cls::sety(int b){y=b;}void Cls::print(){cout<<"x="<<x<<" y="<<y<<endl;} Cls::Cls(){x=0;y=0;}Cls::Cls(int a,int b){x=a;y=b;}Cls::Cls(int c){x=c;y=0;}int main(){Cls a;Cls b(1,2);Cls c(1);a.print();b.print();c.print();return 0;}5.#include <iostream>using namespace std;const double PI=3.14;class cylinder{private:double radius;double height;public:cylinder(double,double);void setr(double);void seth(double);double area();double volume();void print();};cylinder::cylinder(double r,double h){radius=r;height=h;}void cylinder::seth(double h){height=h;}void cylinder::setr(double r){radius=r;}double cylinder::area(){return 2*PI*radius*height+2*PI*radius*radius; }double cylinder::volume(){return PI*radius*radius*height;}void cylinder::print(){cout<<"The area is "<<area()<<" and the volume is "<<volume()<<'.'<<endl; }int main(){cylinder c(1.0,2.0);c.print();c.seth(1.0);c.setr(2.0);c.print();return 0;}计算机科学与技术学院{b=strcpy(zhy,zhuan);}stu::ruxueshijian(int ru){rxshj=ru;}stu::shuchu(){cout<<"专业:"<<b<<"\t姓名:"<<a<<"\t学号:"<<xh<<"\t性别:";if(xb==1)cout<<"男";else cout<<"女";cout<<"\t年龄:"<<nl<<"\t入学时间:"<<rxshj<<endl<<endl<<endl; }stu::chengji(int jisuanji,int shuxue,int yingyu,int xuan){jsjchj=jisuanji;shxchj=shuxue;yychj=yingyu;xk=xuan;}stu::xuanke(char *xuanming){c=strcpy(xkm,xuanming);}stu::cha(){m=0;if(jsjchj>=96){o=4.5;cout<<"计算机4.5"<<endl;}if(jsjchj>=90&&jsjchj<=95){o=4.0;cout<<"计算机4.0"<<endl;}if(jsjchj>=85&&jsjchj<=89){o=3.5;cout<<"计算机3.5"<<endl;}if(jsjchj>=80&&jsjchj<=84){o=3.0;cout<<"计算机3.0"<<endl;}if(jsjchj>=75&&jsjchj<=79){o=2.5;cout<<"计算机2.5"<<endl;}if(jsjchj>=70&&jsjchj<=74){o=2.0;cout<<"计算机2.0"<<endl;}if(jsjchj>=65&&jsjchj<=69){o=1.5;cout<<"计算机1.5"<<endl;} if(jsjchj>=60&&jsjchj<=64){o=1.0;cout<<"计算机1.0"<<endl;}if(jsjchj<60){o=0;cout<<"计算机不及格需重考"<<endl;m++;}if(shxchj>=96){p=4.5;cout<<"数学4.5"<<endl;}if(shxchj>=90&&shxchj<=95){p=4.0;cout<<"数学4.0"<<endl;} if(shxchj>=85&&shxchj<=89){p=3.5;cout<<"数学3.5"<<endl;} if(shxchj>=80&&shxchj<=84){p=3.0;cout<<"数学3.0"<<endl;} if(shxchj>=75&&shxchj<=79){p=2.5;cout<<"数学2.5"<<endl;} if(shxchj>=70&&shxchj<=74){p=2.0;cout<<"数学2.0"<<endl;} if(shxchj>=65&&shxchj<=69){p=1.5;cout<<"数学1.5"<<endl;} if(shxchj>=60&&shxchj<=64){p=1.0;cout<<"数学1.0"<<endl;} if(shxchj<60){p=0;cout<<"数学不及格需重考"<<endl;m++;}if(yychj>=96)cout<<"英语4.5"<<endl;if(yychj>=85&&yychj<=89){q=3.5;cout<<"英语3.5"<<endl;}if(yychj>=80&&yychj<=84){q=3.0;cout<<"英语3.0"<<endl;}if(yychj>=75&&yychj<=79){q=2.5;cout<<"英语2.5"<<endl;}if(yychj>=70&&yychj<=74){q=2.0;cout<<"英语2.0"<<endl;}if(yychj>=65&&yychj<=69){q=1.5;cout<<"英语1.5"<<endl;}if(yychj>=60&&yychj<=64){q=1.0;cout<<"英语1.0"<<endl;}if(yychj<60){q=0;cout<<"英语不及格需重考"<<endl;m++;}if(xk>=96)cout<<c<<' '<<"4.5"<<endl;if(yychj>=90&&yychj<=95){q=4.0;cout<<"英语4.0"<<endl;} if(yychj>=85&&yychj<=89){q=3.5;cout<<"英语3.5"<<endl;}if(yychj>=80&&yychj<=84){q=3.0;cout<<"英语3.0"<<endl;}if(yychj>=75&&yychj<=79){q=2.5;cout<<"英语2.5"<<endl;}if(yychj>=70&&yychj<=74){q=2.0;cout<<"英语2.0"<<endl;}if(yychj>=65&&yychj<=69){q=1.5;cout<<"英语1.5"<<endl;}if(yychj>=60&&yychj<=64){q=1.0;cout<<"英语1.0"<<endl;}if(yychj<60){q=0;cout<<"英语不及格需重考"<<endl;m++;}if(xk>=96)cout<<c<<' '<<"4.5"<<endl;if(xk>=90&&xk<=95){r=4.0;cout<<c<<' '<<"4.0"<<endl;}if(xk>=85&&xk<=89){r=3.5;cout<<c<<' '<<"3.5"<<endl;}if(xk>=80&&xk<=84){r=3.0;cout<<c<<' '<<"3.0"<<endl;}if(xk>=75&&xk<=79){r=2.5;cout<<c<<' '<<"2.5"<<endl;}if(xk>=70&&xk<=74){r=2.0;cout<<c<<' '<<"2.0"<<endl;}if(xk>=65&&xk<=69){r=1.5;cout<<c<<' '<<"1.5"<<endl;}if(xk>=60&&xk<=64){r=1.0;cout<<c<<' '<<"1.0"<<endl;}if(xk<60){r=0;cout<<c<<' '<<"不及格需重考"<<endl;m++;}if(m!=0)cout<<"有"<<m<<"门课程需要重考"<<endl;}stu::pingjun(){float x;x=(o+p+q+r)/4;cout<<"平均绩点为: "<<x<<endl;}stu::chongxiu(){if(m!=0){int y;n=0;for(int i=0;i<m;i++){cout<<"输入重考分数"<<endl;cin>>y;if(y<60){cout<<"需重修"<<endl;n++;}elsecout<<"绩点1.0"<<endl;}if(n!=0)cout<<"有"<<n<<"门课程需要重修"<<endl;}}void main(){stu a;char name[15],zhuan[20],xuanming[20];int xing,hao,nian,ru,jisuanji,shuxue,yingyu,xuan;cout<<"请输入专业:"<<endl;cin.getline(zhuan,20,'\n');cout<<"请输入姓名:"<<endl;cin.getline(name,15,'\n');cout<<"请输入选课名:"<<endl;cin.getline(xuanming,20,'\n');cout<<"请输入学号:(例如20070001)"<<endl;cin>>hao;cout<<"请输入性别:(1.男2.女)"<<endl;cin>>xing;cout<<"请输入年龄:"<<endl;cin>>nian;cout<<"请输入入学时间:(例如200709)"<<endl;cin>>ru;a.xuehao(hao);a.ruxueshijian(ru);a.mingzi(name);a.xingbie(xing);a.nianling(nian);a.zhuanye(zhuan);a.shuchu();cout<<"请输入各门功课成绩:计算机、数学、英语、"<<xuanming<<endl;cin>>jisuanji>>shuxue>>yingyu>>xuan;;a.chengji(jisuanji,shuxue,yingyu,xuan);a.xuanke(xuanming);a.cha();a.pingjun();a.chongxiu();}附录(可包括源程序清单或其它说明)。