Experiment 4 Static members and friends
Class: Student ID: Name: Grade:
one. Purpose
1. Understand the role of static members and friends ;
2. Master the definition and use of static data members and static member functions;
3. Master the declaration and use of friend functions and friend classes.
two. Equipment and instruments used
Computer +Windows XP+Visual C++6.0
three. Experiment content and requirements
1. A store wants to manage sales orders for products. Order information includes item number, name, unit price, and quantity. Among them, if customers purchase more than 10 items at a time, they can enjoy a 9.2% discount.
Write a program that calculates the total number of orders, the total number of items sold, and the total sales amount for the store that day. (implemented using static members)
2. Define a point class Point, and define an ordinary function Distance to calculate the distance between two points. (implemented with friends)
3. A company needs to manage employee wages and requires the definition of employee and manager classes.
Manager class Manager includes number, name, department information.
Employee class Employee includes number, name, fixed salary and bonus. The bonus of the employee must be 0 initially. It must then be set by the manager. Member functions mainly include constructors, functions for inputting and displaying employee information, and functions for calculating the actual salary of employees (actual salary of employees = fixed salary + bonus).
Program the above functions and call them in the main function. (implemented with friends)
For example, see the main function.
intmain()
{
Employee e1("E1001","Tom",3000); //Define the employee class object
e1.Show();
Manager m1("M2001","Jerry","IT"); //Define the manager class object
m1.Show();
m1.SetEmpBonus(e1); //Manager sets employee bonus
e1.Show();
return 0;
}
Four. Experimental procedure
1.#include <iostream>
#include<iomanip>
using namespace std;
const int MAX=100;
const int N=12;
class Order
{
private:
int num; // order number
string name; // Order name
float price; // Order price
int quantity; // order 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<<" Illegal input, re-enter: ";
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<<" Please enter order number: ";
cin>>num;
cout<<" Please enter order name: ";
cin>>name;
cout<<" Please enter order price: ";
cin>>price;
price=judge(price);
cout<<" Please enter the number of items: ";
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<<" Please enter the number of orders: ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<" Please enter "<<i+1<<" order information: "<<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;
}
intmain()
{
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<<" Input coordinates: ";
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;
}
intmain()
{
Point s1,s2;
s1.Input();
s2.Input();
cout<<"The distance is: ";
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;
}
intmain()
{
Employee e1("E1001","Tom",3000); // Define the employee class object
e1.Show();
Manager m1("M2001","Jerry","IT"); // Define the manager class object
m1.Show();
m1.SetEmpBonus(e1,800); // Manager sets employee bonus
e1.Show();
return 0;
}
Result analysis: Calculate the distance between two points by entering the spatial coordinates of the two points
Experimental summary
1 . Written program source code
2 . Problems encountered in the experiment and their solutions
3 . reward
1. The definition of static function, use .
For example, in order to output count and total , k and mon are specially defined to record these two numbers;
k=a[i].Add_count();
mon=a[i].Add_total();
After thinking all night, I felt that the definition of k and mon was redundant, so I suddenly thought of a static function, static double total_Show();
static int count_Show();
Definition variables conflict with keywords such as static int cout ;
cout<<setw(N)<<n<<setw(N)<<a[n-1].Add_count()<<setw(N)<<a[n-1].Add_total()<<endl;
Ignore that when the function is called, count is still executing count=count+quantity ;
2. When defining a function
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;
}
By looking up the book, I found that the definition of the friend function is not correct.
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;
}
And the declaration of friend double Distant(Point&,Point& ) should be made in the Point class ;
The use of the setting function is not understood, such as:
void Employee::Input() // Input function set for this bonus
{
cout<<" Please enter employee bonus: ";
cin >> award;
}
void Manager::SetEmpBonus(Employee &a)
{
a.Input(); // The setup function is not used, an input function is called
}
after modification
Class Employee
{
Public:
friend void Manager::SetEmpBonus(Employee &e,double a)
};
void Manager::SetEmpBonus(Employee &e,double a)
{
e.award=a;
}