20240206作业

第五章 静态成员与友元

一、填空题

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

#include <iostream>

using namespace std;

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;}

int main(){

Test t(10);

++++t++++ ++++.++++ ++++show++++ ++++();++++

}

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

#include <iostream>

using namespace std;

class Test{

public:

++++static++++ ++++int++++ ++++x++++ ++++;++++

Test (int i=0){x=i+x;}

int Getnum (){return Test::x+7;}

};

++++int++++ ++++Test++++ ++++::++++ ++++x++++ ++++=++++ ++++33++++ ++++;++++

int main(){

Test test;

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

}

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

#include <iostream>

#include <string.h>

#include <iomanip>

using namespace std;

class student{

char name8;

int deg;

char level7;

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;

}

};

int 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),让静态成员函数访问静态数据成员,并让静态数据成员是保户的。

#include <iostream>

using namespace std;

class Student{

private:

string name;

static int age;

public:

Student (string n,int a):name(n){

Student::age=a+1;

}

~Student(){

Student::age--;

cout << "~Student name=" << name << " age=" << age << endl;

}

void show(){

cout << "name=" << name << " age=" << age << endl;

}

};

int Student::age=0;

int main(){

Student s1("meili",18);

s1.show();

Student s2("fugui",19);

s2.show();

Student s3("zhaocai",20);

s3.show();

}

(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);

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

protected:

int itsWeight;

int itsAge;

};

void SetValue (Animal& ta, int tw){

ta.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;

}

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

#include <iostream>

using namespace std;

class Animal;

void SetValue (Animal&, int);

void SetValue (Animal&, int, int);

class Animal{

public:

void SetValue (Animal&, int);

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

protected:

int itsWeight;

int itsAge;

};

void Animal::SetValue (Animal& ta, int tw){

ta.itsWeight = tw;

}

void Animal::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;

}

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

#include <iostream>

using namespace std;

class Boat;

class Car{

public:

Car (int j){size = j;}

friend int Leisure (int time, Car& aobj, Boat& bobi);

protected:

int size;

};

class Boat{

public:

Boat (int j){size = j;}

friend int Leisure (int time, Car& aobj, Boat& bobj);

protected:

int size;

};

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

return time * aobj.size * bobi.size;

}

int main(){

Car c1(2);

Boat b1(3);

int time = 4;

cout << Leisure(time,c1 ,b1 ) <<endl;

return 0;

}

相关推荐
MC皮蛋侠客几秒前
Tauri 2.x 系列(五):调用系统 API——官方插件、Rust crate 与原生能力
开发语言·后端·rust
sel_912 分钟前
【多轮对话论文导读(三)】多轮对话与Agent论文阅读笔记:用户模拟、轨迹生成与长期记忆
论文阅读·人工智能·笔记·深度学习·算法·机器学习
Asize32 分钟前
438. 找到字符串中所有字母异位词
算法
Asize39 分钟前
283. 移动零
算法
布莱克6051 小时前
软中断和硬中断的区别
开发语言·操作系统
whcyhhh1 小时前
头歌实践教学平台:数据科学与大数据技术导论(十二)
大数据·开发语言·python·数据清洗
lzhdim2 小时前
13、JavaScript事件循环机制 - JavaScript学习系列文章
开发语言·前端·javascript·学习·ecmascript
zbyyd2 小时前
Linux 多线程:互斥锁 &amp; 信号量
linux·c语言·开发语言·网络
船厂电气自动化ai大模型3 小时前
AI大模型与数学·第57课 傅里叶全套工具链综合实战:串联级数/连续变换/DFT/FFT,图像、音频、扩散模型完整例题
数据结构·人工智能·深度学习·算法·机器学习
wuyk5553 小时前
从零吃透Modbus通信|第3章:STM32 Modbus RTU主机
服务器·开发语言·网络·数据结构·stm32·单片机·嵌入式硬件