实验四 静态与友员

实验四  静态成员和友元

班级:         学号:            姓名:          成绩:    

一. 实验目的

1理解静态成员与友元的作用;

2、掌握静态数据成员与静态成员函数的定义与使用;

3、掌握友元函数与友元类的声明与使用。

二. 使用的设备和仪器

计算机+Windows XP +Visual C++6.0

三. 实验内容及要求

1.某商店要对商品的销售订单进行管理。订单信息包括商品编号,名称,单价,数量。其中,若客户一次购买10件以上商品,可享受9.8折优惠。

编写程序,计算当日该商店的订单总数,销售商品总数和总销售款。(使用静态成员来实现)

2.定义一个点类Point,并定义一个普通函数Distance来计算两点之间的距离。(用友元实现)

3.某公司需对员工工资进行管理,要求定义员工类和经理类。

经理类Manager包括编号、姓名、部门信息。

员工类Employee包括编号、姓名、固定工资和奖金。其中员工的奖金初始时必须0。之后必须由经理进行设置。成员函数主要有构造函数、输入、显示员工信息函数,计算员工实际工资的函数(员工的实际工资=固定工资+奖金)。

编程实现以上功能,并在主函数中调用它们。(用友元实现)

例如,参考主函数。

int main()

{   

Employee e1("E1001","Tom",3000);          //定义员工类对象

e1.Show();

Manager m1("M2001","Jerry","IT");         //定义经理类对象

m1.Show();

m1.SetEmpBonus(e1);                       //经理设置员工的奖金

e1.Show();                         

return 0;

}

四. 实验步骤

1.#include <iostream> 

#include<iomanip>

using namespace std;

const  int MAX=100; 

const  int N=12;

class Order

{

private:

int num; //订单编号 

string name; //订单名称 

float price; //订单价格 

int quantity; //订单数量 

static int count;

static double total;

public:

void Input();

void Output();

int Add_count();

double Add_total();

static double total_Show();

static int count_Show();

template<class T>

  T judge(T a)

{

while(a<0)

{

cout<<"输入不合法,重新输入:";

cin>>a;

}

return a;

}

};

int Order::count_Show()

{

return count;

}

double Order::total_Show()

{

return total;

}

int Order::count=0;

double Order::total=0;

int Order::Add_count()

{

count=count+quantity;

return count;

}

double Order::Add_total()

{

if(quantity>10)

total=0.98*price*quantity+total;

else

total=price*quantity+total;

return total;

}

void Order::Input()

{

cout<<"请输入订单编号:";

cin>>num;

cout<<"请输入订单名称:";

cin>>name;

cout<<"请输入订单价格:";

cin>>price;

price=judge(price); 

 cout<<"请输入商品数量:";

 cin>>quantity;

 quantity=judge(quantity);

}

void Order::Output()

{

cout<<setw(N)<<"编号"<<setw(N)<<"名称"<<setw(N)<<"价格"<<setw(N)<<"数量"<<endl;

cout<<setw(N)<<num<<setw(N)<<name<<setw(N)<<price<<setw(N)<<quantity<<endl; 

}

void Menu(Order a[] )

{

int n;

cout<<"请输入订单数:";

cin>>n;

for(int i=0;i<n;i++)

{

cout<<"请输入第"<<i+1<<"个订单订单信息:"<<endl;

a[i].Input();

a[i].Add_count();

a[i].Add_total(); 

}

cout<<setw(N)<<"订单数"<<setw(N)<<"商品总数"<<setw(N)<<"总额"<<endl;

cout<<setw(N)<<n<<setw(N)<<Order::count_Show()<<setw(N)<<Order::total_Show()<<endl; 

}

int main()

{

Order c[MAX];

Menu(c);

return 0;

}

2.#include<iostream>

#include<math.h>

using namespace std;

class Point

{

private:

double a;

double b;

double c;

public:

friend double Distant(Point &,Point&);

Point(double a=0,double b=0,double c=0)

{

this->a=a;

this->b=b;

this->c=c;

}

void Input()

{

cout<<"输入坐标:";

cin>>a>>b>>c;

}

};

double Distant(Point &s1,Point &s2)

{

double m,n;

m=pow(s1.a-s2.a,2)+pow(s1.b-s2.b,2)+pow(s1.c-s2.c,2);

n=sqrt(m);

return n;

}

int main()

{

Point s1,s2;

s1.Input();

s2.Input();

cout<<"距离为:";

cout<<Distant(s1,s2)<<endl;

return 0;

3.

#include<iostream>

#include<string>

#include<iomanip> 

const int N=12;

using namespace std;

class Employee;

class Manager

{

private:

  string num;

string name;

string depart;

public:

Manager(string num,string name,string depart)

{

this->num=num;

this->name=name;

this->depart=depart; 

}

void SetEmpBonus(Employee&,double);

void Show();

};

class Employee

{

private:

    string num;

string name;

double wage;

double award;

public:

Employee(string num,string name,double wage)

{

this->num=num;

this->name=name;

this->wage=wage;

this->award=0; 

}

void Input();

void Show();

friend void Manager::SetEmpBonus(Employee &e,double a);

};

void Manager::SetEmpBonus(Employee &e,double a)

{

e.award=a;

}

void Employee::Show()

{

cout<<setw(N)<<"编号"<<setw(N)<<"姓名"<<setw(N)<<"工资"<<setw(N)<<"奖金"<<endl;

cout<<setw(N)<<num<<setw(N)<<name<<setw(N)<<wage<<setw(N)<<award<<endl; 

}

void Manager::Show()

{

cout<<setw(N)<<"编号"<<setw(N)<<"姓名"<<setw(N)<<"部门"<<endl;

cout<<setw(N)<<num<<setw(N)<<name<<setw(N)<<depart<<endl; 

}

int main()

{

Employee e1("E1001","Tom",3000);          //定义员工类对象

e1.Show();

Manager m1("M2001","Jerry","IT");         //定义经理类对象

m1.Show();

m1.SetEmpBonus(e1,800);                       //经理设置员工的奖金

e1.Show();   

return 0;

}

结果分析:通过输入两个点的空间坐标计算两点距离

实验总结

1.编写的程序源代码

2.实验中遇到的问题及解决方法

3.收获

1.对静态函数的定义,使用

比如为了输出counttotal,有专门定义kmon来记录这两个数;

k=a[i].Add_count();

mon=a[i].Add_total(); 

经过彻夜思考后感觉定义kmon多余,于是突然想到了静态函数,static double total_Show();

static int count_Show();

 

 

定义变量与关键字发生冲突如static int cout

cout<<setw(N)<<n<<setw(N)<<a[n-1].Add_count()<<setw(N)<<a[n-1].Add_total()<<endl; 

忽略了在函数调用时,count还在执行count=count+quantity

2.对函数定义时

double friend Point::Distant(Point &s1,Point &s2)

{

double m,n;

m=pow(s1.a-s2.a,2)+pow(s1.b-s2.b,2)+pow(s1.c-s2.c,2);

n=sqrt(m);

return n;

}

通过查阅书才发现友元函数定义不对应该是

double Distant(Point &s1,Point &s2)

{

double m,n;

m=pow(s1.a-s2.a,2)+pow(s1.b-s2.b,2)+pow(s1.c-s2.c,2);

n=sqrt(m);

return n;

}

并且应该在Point类中进行friend double Distant(Point&,Point&)的声明;

设置函数的使用不明白比如:

void Employee::Input()       //为该奖金设置的输入函数

{

cout<<"请输入员工奖金:";

cin>>award;

}

void Manager::SetEmpBonus(Employee &a)

{

a.Input();     //不会用设置函数,调用了一个输入函数

}

修改后

Class Employee

{

Public

friend void Manager::SetEmpBonus(Employee &e,double a)

};

void Manager::SetEmpBonus(Employee &e,double a)

{

e.award=a;

}

 

猜你喜欢

转载自blog.csdn.net/benben0729/article/details/45618267