当前位置:文档之家› 贵州大学C++实验报告2

贵州大学C++实验报告2

贵州大学实验报告
学院:大数据信息与工程专业:通信班级: 122
2. 声明Boat和cat两个类,两者都有weight属性,定义二者的一个友元函数totalweight(),计算二者的重量和。

#include<iostream>
using namespace std;
class Cat
{
public:
Cat(int weight);
this->weight=weight;
}
Cat::~Cat()
{
}
int weight1(Cat&c)
{
int sss;
sss=c.weight;
return sss;
}
class Boat
{
public:
Boat( int weight);
~Boat();
friend int weight1(Boat&);
private:
int weight;
};
Boat::Boat(int weight)
{
cout<<"这个Boat的weight为:"<<weight<<endl;
this->weight=weight;
}
Boat::~Boat()
{
}
int weight1(Boat&c)
{
int sss;
sss=c.weight;
return sss;
}
int main()
{
Boat b(4);
Cat c(5);
int totalweight=weight1(b)+weight1(c);
cout<<"两个的weight和为:"<<totalweight<<endl;
return 0;
}
2.在函数fn1()中声明一个静态变量n,fn1()中对n的值加1,在主函数中调
注:各学院可根据教学需要对以上栏木进行增减。

表格内容可根据内容扩充。

相关主题