c++程序设计实验报告

发布时间 : 星期二 文章c++程序设计实验报告更新完毕开始阅读

Date(int year,int month,int day); ~Date(){};

int &GetYear(){return year;} int &GetMonth(){return month;} int &GetDay(){return day;} private: int year; int month; int day;

static bool IsLeapyear;//是否闰年 };

bool Date::IsLeapyear=true;

Date::Date(int year,int month,int day) {

(*this).year=year; (*this).month=month; (*this).day=day; }

void main() {

int year,month,day;

cin>>year>>month>>day; Date mydate(year,month,day); int &myyear=mydate.GetYear(); int &mymonth=mydate.GetMonth(); int &myday=mydate.GetDay();

cout<

cout<< mydate.GetYear(); }

5.代码测试 1.

2.

6.测试过程和运行结果分析

1、if(seconds>=60) { seconds-=60; minutes++; } if(minutes>=60) { minutes-=60; hours++; }

用if时当seconds和minutes>=60时,程序只减一次60,如果seconds和minutes是60的两倍或以上的话,明显减的不够。所以改用while的话就可以很好的解决这个问题了。

2、int &myday=mydate.GetDay();是对mydate.GetDay()的引用,相当于给它起了个别名叫做myday,所以当myyear=8888;时,cout<< mydate.GetYear();输出的也是8888.

7.思考题解答

main函数中int &myyear=mydate.GetYear(); 、int &mymonth=mydate.GetMonth(); 和int &myday=mydate.GetDay();语句表达的是什么思想?这样做的目的是什么?这种方法是否“好”呢?为什么?如果“不好”应该怎样修改?

答:int &myyear=mydate.GetYear(); 、int &mymonth=mydate.GetMonth(); 和int &myday=mydate.GetDay();是引用,相当于给右边的变量起了个别名。这样做,“myyear=8888;

cout<< mydate.GetYear();”输出的就是8888了。这样不好,破坏了类的封装性,导致类的私有成员数据在类外可以被随意修改。

实验报告八 继承与派生类

1.实验目的

(1) 掌握单继承程序设计的基本方法。 (2) 掌握多继承程序设计的基本方法。

2.实验设备

硬件环境:微型计算机 软件环境:

操作系统: Windows 语言环境: Visual C++

3.实验内容

(1) 下面程序定义一个vehicle类,并派生出car和truck两个派生类。 #include class vehicle {

};

class car: public vehicle {

private: int passenger_load; public: void initialize(int whls, double wght, int people =4); int passengers() { return passenger_load; } };

class truck: public vehicle {

private: int passenger_load; double payload; public: void init_truck(int number =2, double max_load =24000.0); double efficiency(); int passengers() { return passenger_load; } };

void vehicle::initialize(int whls, double wght) { wheels=whls; weight=wght; }

void car::initialize(int whls, double wght, int people) {

wheels=whls; weight=wght; passenger_load=people; }

void truck::init_truck(int number, double max_load)

protected:

int wheels; double weight; public:

void initialize(int whls, double wght); int get_wheels() { return wheels; } double get_weight() { return weight; }

double wheel_loading() { return weight/wheels; }

联系合同范文客服:xxxxx#qq.com(#替换为@)