设计Sapmle类

来源:百度知道 编辑:UC知道 时间:2024/09/24 09:29:33
设计Sapmle类,有一个私有数据n,另有构造函数实现对数据n放大10倍,普通成员函数disp(),实现输出n植。主函数中建立该类的4个对象S1(n=10)、S2(n=20)、s3(n=30)、s4(n=40);设计一个友元函数实现这4个对象的n值的累加。主函数中输出结果

请务必把完整程序写下来 谢谢拉
一定要用C++写啊

Sample类如下:
#pragma once
#include <iostream>
using namespace std;

class Sample;
int Sum(Sample s1, Sample s2);

class Sample
{
public:
Sample(int n)
{
this->n = n*10;
}
~Sample(void)
{
;
}

void disp()
{
cout << n << endl;
}

friend int Sum(Sample s1, Sample s2)
{
return s1.n + s2.n;
}
private:
int n;
};
主函数如下:
#include <iostream>
#include "Sample.h"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
Sample S1(10);
Sample S2(20);
Sample S3(30);
Sample S4(40);

cout << Sum(S1, S2) + Sum(S3, S4) << endl;
}