C++多文件编程是什么
分类:
题目:
#include <iostream>
using namespace std;
class shang_stock; // 提前声明
class shen_stock {
private :
int general; // 普通股票个数
int st; // ST股票个数
int pt; // PT股票个数
public :
shen_stock(int a, int b, int c):general(a),st(b),pt(c) { };
friend void shen_count(shen_stock x);
friend void count(shang_stock y, shen_stock x);
};
class shang_stock {
private :
int general; // 普通股票个数
int st; // ST股票个数
int pt; // PT股票个数
public :
shang_stock(int a, int b, int c):general(a),st(b),pt(c) { };
friend void shang_count(shang_stock x);
friend void count(shang_stock y, shen_stock x);
};
void shen_count(shen_stock x)
{
cout<<"深圳股票总数:"<<x.general+x.st+x.pt<<endl;
}
void shang_count(shang_stock x)
{
cout<<"上海股票总数:"<<x.general+x.st+x.pt<<endl;
}
void count(shang_stock y, shen_stock x)
{
cout<<"深圳和上海总股票数:"<<x.general+x.st+x.pt+y.general+y.st+y.pt<<endl;
}
// 两类共用一个友元函数
int main()
{
shang_stock shanghai(1600,20,10); // 建立对象
// 表示上海有1600支普通股票,20支ST股票, 10支PT股票
shen_stock shenzhen(1500,15,8); // 建立对象
// 表示深圳有1500支普通股票,15支ST股票,8支PT股票
shang_count(shanghai); // 计算上海的股票总数
shen_count(shenzhen); // 计算深圳的股票总数
count(shanghai, shenzhen); // 计算上海和深圳的股票总数
return 0;
}
shang_stock.h:
class shen_stock;
class shang_stock {
private :
int general; // 普通股票个数
int st; // ST股票个数
int pt; // PT股票个数
public :
shang_stock(int a, int b, int c);
friend void shang_count(shang_stock x);
friend void count(shang_stock y, shen_stock x);
};
shang_stock.cpp:
#include <iostream>
#include "shang_stock.h"
shang_stock::shang_stock(int a, int b, int c):general(a),st(b),pt(c) {
}
shen_stock.h:
class shang_stock;
class shen_stock {
private :
int general; // 普通股票个数
int st; // ST股票个数
int pt; // PT股票个数
public :
shen_stock(int a, int b, int c);
friend void shen_count(shen_stock x);
friend void count(shang_stock y, shen_stock x);
};
shen_stock.cpp:
#include <iostream>
#include "shen_stock.h"
shen_stock::shen_stock(int a, int b, int c):general(a),st(b),pt(c) {
}
count.h:
void shen_count(shen_stock x);
void shang_count(shang_stock x);
void count(shang_stock y, shen_stock x);
count.cpp:
#include <iostream>
#include "shang_stock.h"
#include "shen_stock.h"
using namespace std;
void shen_count(shen_stock x)
{
cout<<"深圳股票总数:"<<x.general+x.st+x.pt<<endl;
}
void shang_count(shang_stock x)
{
cout<<"上海股票总数:"<<x.general+x.st+x.pt<<endl;
}
void count(shang_stock y, shen_stock x)
{
cout<<"深圳和上海总股票数:"<<x.general+x.st+x.pt+y.general+y.st+y.pt<<endl;
}
main.cpp:
#include <iostream>
#include "shang_stock.h"
#include "shen_stock.h"
using namespace std;
int main()
{
shang_stock shanghai(1600,20,10); // 建立对象
// 表示上海有1600支普通股票,20支ST股票, 10支PT股票
shen_stock shenzhen(1500,15,8); // 建立对象
// 表示深圳有1500支普通股票,15支ST股票,8支PT股票
shang_count(shanghai); // 计算上海的股票总数
shen_count(shenzhen); // 计算深圳的股票总数
count(shanghai, shenzhen); // 计算上海和深圳的股票总数
return 0;
}