假期2.6

一、填空题

1、一个类的头文件如下所示,num初始化值为5,程序产生对象T,且修改num为10,并使用show()函数输出num的值10。

#include <iostream.h>

class Test

{ private:

static int num;

public:

Test(int);

void show();

};

int Test::num = 5;_________

Test::Test(int n)

{ num=n; }

void Test::show()

{ cout<<num<<endl; }

void main()

{ Test t(10);

t.show;_____

}

2 、在下面程序的底画线处填上适当的字句,使该程序执行结果为40。

#include <iostream>

class Test

{ public:

___static int x;

Test (int i=0)

{x=i+x;}

int Getnum()

{return Test::x+7;}

};

int Test::x = 33;_____;

void main()

{Test test;

cout<<test.Getnum()<<endl;;

}

3 、下列程序运行的结果是___________

#include <iostream.h>

#include <string.h>

#include <iomanip.h>

class student

{char name[8];

int deg;

char level[7];

friend class process; // 说明友元类

public:

student(char na[],int d)

{ strcpy(name,na);

deg=d;

}

};

class process

{ public:

void trans(student &s)

{int i=s.deg/10;

switch(i)

{case 9:

strcpy(s.level, "优");break;

case 8:

strcpy(s.level,"良");break;

case 7:

strcpy(s.level,"中");break;

case 6:

strcpy(s.level,"及格");break;

default:

strcpy(s.level,"不及格");

}

}

void show(student &s)

{cout<<setw(10)<<s.name<<setw(4)<<s.deg<<setw(8)<<s.level<<endl;}

};

void main()

{ student st[]={student("张三",78),student("李四",92),student("王五

",62),student("孙六",88)};

process p;

cout<<"结 果:"<<"姓名"<<setw(6)<<"成绩"<<setw(8)<<"等级"<<endl;

for(int i=0;i<4;i++)

{ p.trans(st[i]);

p.show(st[i]);}

}

输出:

结果: 姓名 成绩 等级

张三 78 中

李四 92 优

王五 62 及格

孙六 88 良

二、编程题

(1) 编写一个类,声明一个数据成员和一个静态数据成员。让构造函数初始化数据成员,并把静态数据成员加1,让析构函数把静态数据成员减1。

(2) 根据(1)编写一个应用程序,创建三个对象,然后显示它们的数据成员和静态数据成员,再析构每个对象,并显示它们对静态数据成员的影响。

(3) 修改(2),让静态成员函数访问静态数据成员,并让静态数据成员是保户的。

(1) 下述代码有何错误,改正它。

#include < iostream>

using namespace std;

class Animal;

void SetValue(Animal&, int);

void SetValue(Animal&, int, int);

class Animal

{

public:

friend void SetValue(Animal&, int);

protected:

int itsWeight;

int itsAge;

};

void SetValue(Animal& ta, int tw)

{

a.itsWeight = tw;

}

void SetValue(Animal& ta, int tw, int tn)

{

ta.itsWeight = tw;

ta.itsAge = tn;

}

int main()

{

Animal peppy;

SetValue(peppy, 5);

SetValue(peppy, 7,9);

return 0;

}

将上面程序中的友员改成普通函数,为此增加访问类中保护数据的成员函数。

改后

#include <iostream>

class Animal {

public:

void SetValue(int);

void SetValue(int, int);

protected:

int itsWeight;

int itsAge;

};

void Animal::SetValue(int tw) {

itsWeight = tw;

}

void Animal::SetValue(int tw, int tn) {

itsWeight = tw;

itsAge = tn;

}

int main() {

Animal peppy;

peppy.SetValue(5);

peppy.SetValue(7, 9);

return 0;

}

  1. 重新编写下述程序,使函数Leisure()成为类Car和类Boat的函数。作为重新编程,在类Car和类Boat中,增加函数set()

改后

#include <iostream>

using namespace std;

class Boat; // 前向声明

class Car {

public:

Car(int j) { size = j; }

int Leisure(int time, Boat& bobj);

void set(int j) { size = j; }

int getSize() { return size; } // 公有访问函数

private:

int size;

};

class Boat {

public:

Boat(int j) { size = j; }

int Leisure(int time, Car& aobj);

void set(int j) { size = j; }

int getSize() { return size; } // 公有访问函数

private:

int size;

};

int Car::Leisure(int time, Boat& bobj) {

return time * getSize() * bobj.getSize(); // 使用公有访问函数

}

int Boat::Leisure(int time, Car& aobj) {

return time * getSize() * aobj.getSize(); // 使用公有访问函数

}

int main() {

Car c1(2);

Boat bl(3);

int time = 4;

cout << c1.Leisure(time, bl) << endl;

return 0;

}

相关推荐
yzx9910131 小时前
基于 Q-Learning 算法和 CNN 的强化学习实现方案
人工智能·算法·cnn
iCxhust1 小时前
Prj10--8088单板机C语言8259测试(1)
c语言·开发语言
亮亮爱刷题1 小时前
算法练习-回溯
算法
眼镜哥(with glasses)2 小时前
蓝桥杯 国赛2024python(b组)题目(1-3)
数据结构·算法·蓝桥杯
крон4 小时前
【Auto.js例程】华为备忘录导出到其他手机
开发语言·javascript·智能手机
zh_xuan4 小时前
c++ 单例模式
开发语言·c++·单例模式
老胖闲聊5 小时前
Python Copilot【代码辅助工具】 简介
开发语言·python·copilot
Blossom.1185 小时前
使用Python和Scikit-Learn实现机器学习模型调优
开发语言·人工智能·python·深度学习·目标检测·机器学习·scikit-learn
曹勖之5 小时前
基于ROS2,撰写python脚本,根据给定的舵-桨动力学模型实现动力学更新
开发语言·python·机器人·ros2
豆沙沙包?6 小时前
2025年- H77-Lc185--45.跳跃游戏II(贪心)--Java版
java·开发语言·游戏