C++程序求解

来源:百度知道 编辑:UC知道 时间:2024/06/22 19:37:41
实验目的:
1. 掌握静态数据成员、成员函数的初始化及使用
2. 掌握对象数据成员的缺省参数设置
3. 掌握构造函数的重载
4. 了解友元的概念及使用
5. 理解对象的生命周期
实验内容:
1. 商店销售某一商品,商店每天公布统一的折扣(discount)。同时允许销售人员在销售时灵活掌握售价(price),在此基础上,对一次购10件以上者还可以享受9.8折优惠。现已知当天3个售货员的销售情况为:
销货员号(num) 销货件数(quantity) 销货单价(price)
101 5 23.5
102 12 24.56
103 100 21.5
请编程计算出当日此商品的总销售款sum,以及每件商品的平均售价。要求用静态数据成员和静态成员函数。
(提示:将折扣discount,总销售款sum和商品销售总件数n声明为静态数据成员,再定义静态成员函数average(求平均售价)和display(输出结果))
2. 指出下列程序中的错误
#include <iostream.h>

class Student
{
public:
Student()
{
++x;
cout<<"\nplease input student No.";
cin>>Sno;
}
static int get_x()
{
return x;
}
int get_Sno()
{
return Sno;
}
private:
static int x;
int Sno;
};

int Student::x=0;

1.
#include <iostream>

void getRecord(int N);
class Sales
{
public:
static int n;
static float discount;
static float sum;

static float average();
static void display();
friend void getRecord(int N);

private:
static void input(int quantity, float price);
};

int Sales::n = 0;
float Sales::discount = 1.0;
float Sales::sum = 0;

float Sales::average()
{
return (sum/n);
}

void Sales::display()
{
std::cout<< "sum = " << sum << std::endl;
std::cout<< "avg = " << average() << std::endl;
}

void Sales::input(int quantity, float price)
{
n += quantity;;
sum += quantity*price;
}

void getRecord(int N)
{
int no;
int quantity;
float price;
for(int i = 0; i < N; ++i)
{
std::co