(面向对象程序设计)实验三 静态成员与友元

一、实验目的

·学习友元函数的定义和原理。
·学习静态数据成员和静态成员函数的使用。
·学习静态成员代替全局变量实现对象间的共享。

二、实验内容

(1)有如下类的定义。类成员函数copy用于实现两个对象的相互拷贝,请完成该函数的实现。(有两种方法即不用this 指针和用this指针),并利用友员函数实现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;
};

void Myclass::copy(Myclass &my)
{
	this->x=my.x;
	this->y=my.y;
}

void main()
{
	Myclass my(10,20),t(30,40);
	my.print();
	my.copy(t);
	my.print();
}


结果:
在这里插入图片描述
代码实现2:

#include <iostream.h>
class Myclass{
public:
	Myclass (int a,int b) 
	{x=a;y=b;}
	friend void copy(Myclass &m,Myclass n);
	void print()
	{
		cout<<"x="<<x<<endl;
		cout<<"y="<<y<<endl;
	}
private:
	int x,y;
};

void copy(Myclass &m,Myclass n)
{
	m.x=n.x;
	m.y=n.y;
}

void main()
{
	Myclass my(10,20),t(30,40);
	my.print();
	copy(my,t);
	my.print();
}

结果;
在这里插入图片描述

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

代码实现:

// Menu.h
#ifndef MENU_H
#define MENU_H
class Menu
{	public:
		int show();
};
#endif;



// Carlo.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;
     BoxPrice  = Price;
     TotalPrice += Weight * Price;
     TotalWeight += Weight;
}

void Carlo::BuyBox()
{
	cout<<"Please input the weight and Price of buy:"<<endl;
	SetCarlo(5.1,6);
	TotalWeight+=BoxWeight;
	TotalPrice+=BoxPrice*BoxWeight;
}

const double &Carlo::GetCurrentTotalPrice() const
{       
	return TotalPrice;          
}

const double &Carlo::GetCurrentTotalWeight() const
{        
	return TotalWeight;
}

void Carlo::SetCarlo(double Weight, double Price)
{
	double Weight1,Price1;
	cout<<"Weight,Price:"<<endl;
	cin>>Weight1>>Price1;
	BoxWeight = Weight1;
	BoxPrice= Price1;
}

void Carlo::SellBox()
{
	cout<<"Please input the weight and Price of sell:"<<endl;
	SetCarlo(5.1,6);
	TotalWeight-=BoxWeight;
	TotalPrice-=BoxPrice*BoxWeight;
}

void Carlo::ShowBoxInfor() const
{
	cout<<"库存总重量:"<<TotalWeight;
	cout<<"      ";
	cout<<"库存总价:"<<TotalPrice;
}


Carlo::~Carlo()
{

}



//Menu.cpp
#include <iostream.h>
#include <windows.h>
#include "Menu.h"
int Menu::show()
{       system("cls");
    	int chioce;
	    cout<<"\n\n\n\n\n\t\t\t"<<"\>\>\>";
        cout<<"进出货演示"<<"\<\<\<"<<endl<<endl;
        cout<<"\t\t\t"<<"1.进货登记."<<endl<<endl;
        cout<<"\t\t\t"<<"2.出货登记."<<endl<<endl;
        cout<<"\t\t\t"<<"3.获取库存信息."<<endl<<endl;
    	cout<<"\t\t\t"<<"4.退出."<<endl<<endl;
        cout<<"\t\t\t"<<"请选择操作:";
        cin>>chioce;
	    return (chioce);
}



//CarloDemo.cpp
#include <iostream.h>
#include "Menu.h"
#include "Carlo.h"
void main()
{   Menu menu;
	Carlo carlo;
    int chioce;
    while( (chioce = menu.show()) - 4)
	{   switch(chioce)
		{    case 1: carlo.BuyBox();
				     break;
			 case 2: carlo.SellBox();
				     break;
			 case 3: carlo.ShowBoxInfor();
				     break;
			 case 4:
					 exit;
		}
	}
}



结果:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值