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:

_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) 下述代码有何错误,改正它。

|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| #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) { 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; } |

void SetValue(Animal& ta, int tw, int tn)无法访问类中被保护的成员,应在类中加上friend void SetValue(Animal& ta, int tw, int tn);

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

void AnimalSetValue(Animal& ta, int tw)

{

ta.itsWeight = tw;

}

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

|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| #include < iostream> using namespace std; class Boat; class Car { public: Car(int j){size = j;} void set(int j){size=j;} friend int Leisure(int time, Car& aobj, Boat& bobi); protected: int size; class Boat { public: Boat(int j){size = j;} void set(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 bl(3); int time = 4; cout << Leisure(time, cl,bl); return 0; } |

相关推荐
hh随便起个名5 小时前
力扣二叉树的三种遍历
javascript·数据结构·算法·leetcode
橘子真甜~5 小时前
C/C++ Linux网络编程15 - 网络层IP协议
linux·网络·c++·网络协议·tcp/ip·计算机网络·网络层
小浣熊熊熊熊熊熊熊丶5 小时前
《Effective Java》第25条:限制源文件为单个顶级类
java·开发语言·effective java
啃火龙果的兔子6 小时前
JDK 安装配置
java·开发语言
星哥说事6 小时前
应用程序监控:Java 与 Web 应用的实践
java·开发语言
等....6 小时前
Miniconda使用
开发语言·python
zfj3216 小时前
go为什么设计成源码依赖,而不是二进制依赖
开发语言·后端·golang
醇氧6 小时前
org.jetbrains.annotations的@Nullable 学习
java·开发语言·学习·intellij-idea
Java&Develop6 小时前
Aes加密 GCM java
java·开发语言·python
weixin_462446237 小时前
使用 Go 实现 SSE 流式推送 + 打字机效果(模拟 Coze Chat)
开发语言·后端·golang