静态成员与友元
1、实验目的
·学习友元函数的定义和原理。
·学习静态数据成员和静态成员函数的使用。
·学习静态成员代替全局变量实现对象间的共享。
2、实验内容
(1)有如下类的定义。类成员函数copy用于实现两个对象的相互拷贝,请完成该函数的实现。(有两种方法即用成员函数和友元函数实现)
#include <iostream>
#include <windows.h>
using namespace std;
class Myclass {
public:
Myclass (int a=1,int b=1) {
x=a;
y=b;
}
void copy(Myclass &my);
void set(int a=1,int b=1) {
x=a;
y=b;
}
void print( ) {
cout<<"x = "<<x<<endl;
cout<<"y = "<<y<<endl;
}
friend void mycopy(Myclass &a,Myclass &b);
private:
int x,y;
};
void Myclass::copy(Myclass &my) {
my.x=x;
my.y=y;
}
void mycopy(Myclass &a,Myclass &b) {
a.x=b.x;
a.y=b.y;
}
int main() {
Myclass a,b;
cout<<"A初始默认值:"<<endl;
a.print();
cout<<"用成员函数B拷贝A:"<<endl;
b.copy(a);
cout<<"B拷贝的值:"<<endl;
b.print();
int m,n;
cout<<"重新设置B的值:\n输入x,y"<<endl;
cin>>m>>n;
b.set(m,n);
cout<<"B的值:"<<endl;
b.print();
cout<<"用友元函数A拷贝B:"<<endl;
mycopy(a,b);
cout<<"A的值:"<<endl;
a.print();
return 0;
}
(2)商店经销一种货物,货物成箱购进,成箱卖出,购进和卖出时以重量为单位,各箱的重量不一样,单价不一样,因此商店需要记录下目前库存的货物的总重量和总价值。编写一个程序,通过定义类Carlo来模拟商店货物购进和卖出的情况。
(本题目主要练习静态数据成员的使用,定义私有变量存每件货物的价格和重量,用静态数据成员存货物的总重量和总价钱,定义构造函数和析构函数,当定义新的对象完成初始化的功能和删除对象时,从总重量和总价钱中减去对象的重量和价格)
#include <iostream>
#include <windows.h>
using namespace std;
class Carlo {
public:
Carlo(double Weight = 0,double Price = 0);
virtual ~Carlo();
void SetCarlo(double = 0,double = 0);
const double &GetCurrentTotalWeight() const;
const double &GetCurrentTotalPrice() const;
void BuyBox();
void SellBox();
void ShowBoxInfor() const;
friend class Menu;
protected:
static double TotalWeight;
static double TotalPrice;
private:
double BoxWeight;
double BoxPrice;
};
class Menu {
public:
int show(Carlo &carlo);
};
double Carlo::TotalPrice=0.0;
double Carlo::TotalWeight=0.0;
Carlo::Carlo(double Weight, double Price) {
BoxWeight=Weight;
BoxPrice=Price;
TotalPrice+=Weight * Price;
TotalWeight+=Weight;
}
const double &Carlo::GetCurrentTotalPrice() const {
return TotalPrice;
}
const double &Carlo::GetCurrentTotalWeight() const {
return TotalWeight;
}
void Carlo::SetCarlo(double Weight, double Price) {
BoxWeight=Weight;
BoxPrice=Price;
TotalWeight+=Weight;
TotalPrice+=Weight*Price;
}
void Carlo::BuyBox() {
Carlo carlo;
double Weight,Price;
cout<<"请输入进货的重量和价格:"<<endl;
cin>>Weight>>Price;
while(Weight<0||Price<0) {
cout<<"参数不对,请重新输入:"<<endl;
cin>>Weight>>Price;
}
carlo.SetCarlo(Weight,Price);
}
void Carlo::SellBox() {
double Weight,Price;
cout<<"输入出货的重量和单价:"<<endl;
cin>>Weight>>Price;
while(Weight<0||Price<0) {
cout<<"参数不对,请重新输入:"<<endl;
cin>>Weight>>Price;
}
if(((Weight*Price)>TotalPrice)||Weight>TotalWeight || TotalPrice<0) {
cout<<"存货不够!"<<endl;
getchar();
getchar();
} else {
TotalPrice-=Weight*Price;
TotalWeight-=Weight;
cout<<"出货完成!"<<endl;
getchar();
getchar();
}
}
void Carlo::ShowBoxInfor() const {
cout<<"现在的总重量:\t"<<TotalWeight<<endl;
cout<<"现在的总价格:\t"<<TotalPrice<<endl;
getchar();
getchar();
}
Carlo::~Carlo() {
cout<<"程序结束,释放内存成功!"<<endl;
getchar();
getchar();
}
int Menu::show(Carlo &carlo) {
cout<<"进出货演示:"<<endl;
cout<<"1.进货登记."<<endl;
cout<<"2.出货登记."<<endl;
cout<<"3.获取库存信息."<<endl;
cout<<"4.退出."<<endl;
cout<<"请选择操作:"<<endl;
int c=0;
while(c!=4) {
cin>>c;
switch(c) {
case 1:
carlo.BuyBox();
break;
case 2:
carlo.SellBox();
break;
case 3:
carlo.ShowBoxInfor();
break;
}
system("cls");
}
return 0;
}
int main() {
Menu menu;
Carlo carlo;
menu.show(carlo);
return 0;
}