第二章

2,改写本章例2.1程序,要求:
1)将数据成员改为私有;

(2)将输入和输出的功能改为由成员函数实现;

(3)在类体内定义成员函数;

#include <iostream>

using namespace std;

class Time

{

private:

int hour;

int minute;

int sec;

public:

void sl()

{

cin>>hour;

cin>>minute;

cin>>sec;

};

void sc()

{

cout<<hour<<":"<<minute<<":"<<sec<<endl;

};

};

int main()

{

Time t1;

cout<<"请按小时:分钟:秒输入\n";

t1.sl();

t1.sc();

return 0;

}

3,在第2题的基础上进行如下操作:在类体内声明成员函数,而在类外定义成员函数。

#include <iostream>

using namespace std;

class Time

{

private:

int hour;

int minute;

int sec;

public:

void sl();

void sc();

};

void Time::sl()

{

cin>>hour;

cin>>minute;

cin>>sec;

};

void Time::sc()

{

cout<<hour<<":"<<minute<<":"<<sec<<endl;

};

int main()

{

Time t1;

cout<<"请按小时:分钟:秒输入\n";

t1.sl();

t1.sc();

return 0;

}

4,在本章第2.6.2(题目错误)节中给出了包含类定义的头文件student.h,包含成员函数定义的源文件student.cpp以及包含主函数的源文件main.cpp。请完善该程序,在类中增加一个对数据成员赋初值得成员函数set_value。上机调试并运行。

main.cpp

#include <iostream>

#include"student.h"

using namespace std;

int main()

{

Student stud;

cout<<"请按 学号 名字 性别 输入\n";

stud.sex_value();

stud.display();

return 0;

}

student.cpp

#include<iostream>

#include"student.h"

void Student::display()

{

cout<<"num:"<<num<<endl;

cout<<"name:"<<name<<endl;

cout<<"sex:"<<sex<<endl;

}

void Student::sex_value()

{

cin>>num>>name>>sex;

}

student.h

#include <string>

using namespace std;

class Student

{

public:

void display();

void sex_value();

private:

int num;

string name;

char sex;

};

5,将本章的例2.4改写为一个多文件的程序:

(1)将类定义放在头文件arraymax.h中;

(2)将成员函数定义放在源文件arraymax.cpp中;

(3)主函数放在源文件filel.cpp中。

请写出完整的程序,上机调试并运行。

arraymax.h

#include <iostream>

class Array_max

{

public:

void set_value();

void max_value();

void show_value();

private:

int array[10];

int max;

};

arraymax.cpp

#include<iostream>

#include"arraymax.h"

using namespace std;

void Array_max::max_value()

{

int i;

max=array[0];

for(i=1;i<10;i++)

{

if(array[i]>max)

max=array[i];

}

};

void Array_max::set_value()

{

int i;

for(i=0;i<10;i++)

cin>>array[i];

};

void Array_max::show_value()

{

cout<<"max="<<max;

};

filel.cpp

#include<iostream>

#include"arraymax.h"

using namespace std;

int main()

{

Array_max arrmax;

arrmax.set_value();

arrmax.max_value();

arrmax.show_value();

return 0;

}

6,需要求3个长方柱的体积,请编写一个基于对的象程序。数据成员包括length(长),width(宽),height(高)。要求用成员函数实现以下功能:

(1)有键盘分别输入3个长方柱的长,宽,高;

(2)计算长方柱的体积;

(3)输出3个长方柱的体积。

请编程序,上机调试并运行。

#include<iostream>

using namespace std;

class V

{

private:

int length;

int width;

int height;

public:

void sl()

{

cout<<"按格式输入 长 宽 高:\n";

cout<<"length:";

cin>>length;

cout<<endl<<"width";

cin>>width;

cout<<endl<<"height";

cin>>height;

cout<<endl;

};

void sc()

{

int v=0;

v=length*width*height;

cout<<"体积:"<<v<<endl;

};

};

int main()

{

V v1;

v1.sl();

v1.sc();

return 0;

}

  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值