实验七静态成员与友元

loading 分享 2026-8-7 下载文档

max = C.balance; strcpy(str,\农业银行\ } } cout << \存款最多的银行是: \ }

int main() { CBank X; BBank Y; GBank Z; X.getbalance(); Y.getbalance(); Z.getbalance(); total(X,Y,Z); max(X,Y,Z); system(\ return 0; }

2. 完成实验项目

定义一个Student类,在该类中包括一个数据成员score(分数),两个静态数据成员total(总分)和学生人数count;成员函数score_total_count(float s)用于设置分数、求总分和累计学生人数;静态成员函数sum()用于返回总分;静态成员函数average()用于求平均值。在main()函数中,输入某班同学的成绩,并调用上述函数求全班学生的总分和平均分。 四、课后作业

1. 声明Book与Ruler两个类,二者都有weight属性,定义二者的友元函数total_weight(),计算二者的重量和。

2. 撰写实验报告。

#include #include using namespace std; class Book; class Ruler { private:

double weight; public:

Ruler(double wg) { this->weight = wg; }

Ruler(Ruler& tp) {

this->weight = tp.weight; }

double getWeight() { return weight; }

~Ruler() { }

void show() {

cout << \ << endl; cout << \ << weight << endl; } };

class Book { private:

double weight; public:

Book(double wg) { this->weight = wg; }

Book (Book& tp) {

this->weight = tp.weight; }

double getWeight() { return weight; } ~Book() { }

friend double totalWeight(Book& a, Ruler& b); void show() {

cout << \ << endl; cout << \ << weight << endl; } };

double totalWeight(Book& a, Ruler& b) { return a.getWeight() + b.getWeight(); }

int main() { Book a(512.0); Ruler b(666.0); a.show(); b.show();

cout << \ << endl; cout << totalWeight(a, b); return 0; }


实验七静态成员与友元.doc 将本文的Word文档下载到电脑
搜索更多关于: 实验七静态成员与友元 的文档
相关推荐
相关阅读