静态成员与友元

有如下类的定义。类成员函数copy用于实现两个对象的相互拷贝,请完成该函数的实现。(有两种方法即用成员函数和友元函数实现)
#include <iostream.h>
class Myclass
{public:
Myclass (int a,int b) { x=a;y=b;}
void copy(Myclass & my);
void print( )
{ cout<<“x=”<<x<<endl;
cout<<”y=”<<y<<endl;
}
private:
int x,y;
};

#include <iostream.h>
 class Myclass 
{public:
  Myclass (int a,int b) { x=a;y=b;}
  void copy(Myclass & my);
  void print();
 private:
 int x,y;
};
 void Myclass::copy(Myclass & my)
 {
	 x=my.x,y=my.y;
 }
 void Myclass::print()
 {   cout<<"x="<<x<<endl;
     cout<<"y="<<y<<endl;
 } 
 void main()
 {
	 int x,y,a,b;
	 cout<<"输入x,y"<<endl;
	 cin>>x>>y;
	 Myclass my(x,y);
	 my.print();
	 cout<<"输入a,b"<<endl;
	 cin>>a>>b;
	 Myclass t(a,b);
	 my.copy(t);
	 my.print();
 }

商店经销一种货物,货物成箱购进,成箱卖出,购进和卖出时以重量为单位,各箱的重量不一样,单价不一样,因此商店需要记录下目前库存的货物的总重量和总价值。编写一个程序,通过定义类Carlo来模拟商店货物购进和卖出的情况。
(本题目主要练习静态数据成员的使用,定义私有变量存每件货物的价格和重量,用静态数据成员存货物的总重量和总价钱,定义构造函数和析构函数,当定义新的对象完成初始化的功能和删除对象时,从总重量和总价钱中减去对象的重量和价格)`

/*main.cpp*/
#include"iostream.h"
#include"Menu.h"
#include"Carlo.h"
void main()
{
	Menu menu;
	Carlo carlo;
	int chioce;
	chioce=menu.show();
	while(chioce)
	{
		switch(chioce)
		{
		case 1:carlo.BuyBox();
			break;
		case 2:carlo.SellBox();
			break;
		case 3:carlo.ShowBoxInfor();
			break;
		}
	}
}
/*.h*/
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;
protected:
	static double TotalWeight;
	static double TotalPrice;
private:
	double BoxWeight;
	double BoxPrice;
};

/*Carlo.cpp*/
#include"iostream.h"
#include"Carlo.h"
double Carlo::TotalPrice=0.0;
double Carlo::TotalWeight=0.0;
Carlo::Carlo(double Weight,double Price)
{
	BoxWeight=(Weight>=0)?Weight:0;
	BoxPrice=Price>0?Price:0;
	TotalPrice+=BoxPrice;
	TotalWeight+=BoxWeight;
}
void Carlo::BuyBox()
{
	TotalPrice+=BoxPrice,TotalWeight+=BoxWeight;
	Carlo carlo;
	double Weight,Price;
	cout<<"输入买进时的价钱和重量"<<endl;
	cin>>Weight>>Price;
	TotalPrice+=BoxPrice,TotalWeight+=BoxWeight;
	if(Weight<0||Price<0)
	{cout<<"输入错误"<<endl;}
	else
	{
		if(Weight*Price>TotalPrice)
		{cout<<"价钱不够"<<endl;}
		else
		{carlo.SetCarlo(Weight,Price);}
	}
}
const double &Carlo::GetCurrentTotalPrice() const
{
	return TotalPrice;
}
const double &Carlo::GetCurrentTotalWeight() const
{
	return TotalWeight;
}
void Carlo::SetCarlo(double Weight,double Price)
{
	BoxPrice=(Price>0)?Price:0;
	BoxWeight=(Weight>0)?Weight:0;
	TotalPrice=BoxWeight*BoxPrice;
	TotalWeight+=BoxWeight;

}
void Carlo::SellBox()
{
	double Weight,Price;
	cout<<"请输入卖出的价钱和重量"<<endl;
	cin>>Weight>>Price;
	if(Weight<0 || Price<0)
	{
		cout<<"输入错误,请重新输入"<<endl;
	}
	else
	{
		if(Weight>TotalWeight)
		{cout<<"数量不够"<<endl;}
		else
		{
		 TotalWeight-=BoxWeight;
		 TotalPrice+=(Weight*Price);
		}
	}
}
void Carlo::ShowBoxInfor() const
{
	cout<<"总重量:"<<TotalWeight<<endl;
	cout<<"总价钱:"<<TotalPrice<<endl;
}
Carlo::~Carlo()
{
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值