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 、下列程序运行的结果是_____

姓名 成绩 等级

张三 78 中

李四 92 优

王五 62 及格

孙六 88 良


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

}

二、编程题

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

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

  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 bl(3); int time = 4; cout << Leisure(time, cl,bl); return 0; } |

相关推荐
kyle~5 分钟前
C/C++字面量
java·c语言·c++
纨妙10 分钟前
python打卡day59
开发语言·python
wuxuanok16 分钟前
Web后端开发-请求响应
java·开发语言·笔记·学习
Sally璐璐36 分钟前
IPSAN 共享存储详解:架构、优化与落地实践指南
开发语言·php
Mr.Winter`41 分钟前
轨迹优化 | 基于激光雷达的欧氏距离场ESDF地图构建(附ROS C++仿真)
c++·人工智能·机器人·自动驾驶·ros·ros2·具身智能
csdn_aspnet44 分钟前
C++ n条水平平行线与m条垂直平行线相交的平行四边形的数量
c++
像风一样的男人@1 小时前
python --货车装厢问题
开发语言·python
Humbunklung1 小时前
Rust枚举:让数据类型告别单调乏味
开发语言·后端·rust
Y1nhl1 小时前
力扣_链表_python版本
开发语言·python·算法·leetcode·链表·职场和发展
OEC小胖胖1 小时前
深入理解 Vue.js 响应式原理及其在 Web 前端开发中的应用
开发语言·前端·javascript·vue.js·web