【C++】实验十

题目:

1、有以下程序:

#include<iostream.h>

class student

{

public:

student(int n, float s):num(n),score(s){}

void change(int n, float s){num=n;score=s;}

void display(){cout<<num<<”  ”<<score<<endl;}

private:

int num;

float score;

}  ;

void main()

{

student stud(101, 78.5);

stud.display();

stud.change(101, 80.5);

stud.display();

}

①阅读此程序,分析其执行过程,然后上机运行,对比输出结果。

②修改上面的程序,增加一个fun函数,改写main函数。在main函数中调用fun函数,在fun  函数中调用change和display函数。在fun函数中使用对象的引用(Studeng&)作为形参。

2、商店销售某一个商品,商店每天公布统一的折扣(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(输出结果)

3、有以下程序

#include<iostream>

using namespace std;

class Date;

class Time

{

public:

Time (int ,int ,int);

void display(Date&);

private:

int  hour;

int minute;

int sec;

};

class Date

{

public:

Date(int ,int ,int);

friend void Time::display(Date&);

private:

int month;

int day;

int year;

};

Time::Time(int  h,int m, int s)

{

hour=h;

minute=m;

sec=s;

}

void Time ::display(Date &d)

{

cout<<d.month<<"/"<<d.day<<"/"<<d.year<<endl;

cout<<hour<<"/"<<minute<<":"<<sec<<endl;

}

Date::Date(int m,int d,int y)

{month=m;

day=d;

year=y;

}

int main()

{

Time t1(10,13,56);

Date d1(12,25,2004);

t1.display(d1);

return 0;

}

请分析和运行此程序,注意友元函数Time::display的作用。将程序中的display函数不放在Time类中。而作为类外的普通函数,然后分别在Time和Date类中将display声明为友元函数。在主函数中调用display函数,display函数分别引用Time和Date两个类的对象的私有数据,输出年、月、日、和时、分、秒。

4、有以下使用类模板程序

#include<iostream>

using namespace std;

template<class  numtype>

class Compare

{

public:

Compare(numtype  a,numtype b)

{x=a;y=b;}

numtype max()

{return (x>y)?x:y;

}

numtype  min()

{return (x<y)?x:y;}

private:

numtype x,y;

};

int main()

{

Compare<int>cmp1(3,7);

cout<<cmp1.max()<<"is the maximum of two integer numbers."<<endl;

cout<<cmp1.min()<<"is the minimum of two integer numbers."<<endl<<endl;

Compare<float>cmp2(45.78,93.6);

cout<<cmp2.max()<<"is the maximum of two float numbers"<<endl;

cout<<cmp2.min()<<"is the minimum of two float numbers"<<endl;

Compare<char>cmp3('a','A');

cout<<cmp2.max()<<"is the maximum of two float numbers"<<endl;

cout<<cmp2.min()<<"is the minimum of two float numbers"<<endl;

return 0;

}

运行此程序,体会类模板的作用。

将它改写为在类模板外定义个成员函数。

参考代码:

1、

#include<iostream>
using namespace std;

class student{
public:
    student(int n,float s){
    num=n;
    score=s;
    }
    void chang(int n,float s){
      num=n;
      score=s;
    }
    void display(){
      cout<<num<<"   "<<score<<endl;
    }
    void fun(student&){
      chang(num,score);
      display();
    }
private:
    int num;
    float score;
};

void main(){
  student stud1(101,78.5);
  stud1.fun (stud1);
  student stud2(101,80.5);
  stud2.fun (stud2);
}

2、

#include <iostream>
using namespace std;
class Discount
{
    private:
        int num;
        double price;
        int quantity;
        static int n;
        static double sum;
        static double discount;
    public:
        void in();
        void count();
    
        static double average();
        static void display();
        
};
double Discount::sum=0;
double Discount::discount=0.95;
int Discount::n=0;

void Discount::in()
{
    cout<<"请输入售货员编码:";
    cin>>num;
    cout<<endl<<"请输入售货件数:";
    cin>>quantity;
    cout<<endl<<"请输入售货单价:";
    cin>>price;
    cout<<endl;
    
}
void Discount::count()
{
    if(quantity>10)
    {
        sum+=quantity*price*discount;
        n+=quantity;
    }
    else
    {
        sum+=quantity*price;
        n+=quantity;
    }
}
double Discount::average()
{

    return sum/n;
}
void Discount::display()
{
    cout<<"平均售价为"<<Discount::average()<<endl;
    cout<<"总销售款为"<<sum<<endl;

}
int main()
{
    int m;
    Discount *d=new Discount[m];
    cout<<"请输入数据组数:"<<endl;
    cin>>m;
    for(int j=0;j<m;j++)
    {
        cout<<"第"<<j+1<<"组:"<<endl;
        d[j].in();
    }
    for(int i=0;i<m;i++)
    {
        d[i].count();
    }
    Discount::display();
    return 0;
}

3、

#include<iostream>
using namespace std;

class Date;
class Time{
public:
    Time(int,int,int);
    friend void display(Date&,Time&);
private:
    int hour;
    int minute;
    int sec;
};

class Date{
public:
    Date(int,int,int);
    friend void display (Date&,Time&);
private:
    int month;
    int day;
    int year;
};

Time::Time(int h,int m,int s){
  hour=h;
  minute=m;
  sec=s;
}

void display (Date &d,Time&t){
  cout<<d.month <<'/'<<d.day <<'/'<<d.year <<endl;
  cout<<t.hour<<'/'<<t.minute<<':'<<t.sec<<endl;
}

Date::Date(int m,int d,int y){
  month=m;
  day=d;
  year=y;
}

int main(){
  Time t1(10,13,56);
  Date d1(12,25,2004);
  display (d1,t1);
  return 0;
}

4、

#include<iostream>
using namespace std;
template<class numtype>
class Compare{
public:
Compare(numtype  a,numtype b)
{x=a;y=b;}
numtype max();
numtype min();

private:
numtype x,y;
};

template<class numtype>
numtype Compare<numtype>::max ()
{return (x>y)?x:y;}
template<class numtype>
numtype Compare<numtype>::min ()
{return (x<y)?x:y;}

int main()
{
Compare<int>cmp1(3,7);
cout<<cmp1.max()<<"is the maximum of two integer numbers."<<endl;
cout<<cmp1.min()<<"is the minimum of two integer numbers."<<endl<<endl;
Compare<float>cmp2(45.78,93.6);
cout<<cmp2.max()<<"is the maximum of two float numbers"<<endl;
cout<<cmp2.min()<<"is the minimum of two float numbers"<<endl;
Compare<char>cmp3('a','A');
cout<<cmp2.max()<<"is the maximum of two float numbers"<<endl;
cout<<cmp2.min()<<"is the minimum of two float numbers"<<endl;
return 0;
}

  • 16
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值