复杂类与对象练习题

更新时间:2023-11-09 15:33:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

复杂类与对象练习题

1、商店销售某一商品,商店每天公布统一的折扣(discount)。同时允许销售人员在销售时灵活掌握售价(price),在此基础上,对一次购10件以上者,还可以享受9.8折优惠。现已知当天3名销货员的销售情况为: 销货员号(num) 101 102 103 销货件数(quantity) 5 12 100 销货单价(price) 23.5 24.56 21.5 请编程序,计算出当日此商品的总销售款sum,以及每件商品的平均售价。要求用静态数据成员和静态成员函数。

(提示: 将折扣discount、总销售款sum和商品销售总件数n声明为静态数据成员,再定义静态成员函数average(求平均售价)和display(输出结果)。

#include using namespace std; class Pr {

public: Pr(int m,int q,double p) { num=m; quantity=q; price=p; } void cal(); static double average(); static void display(); private: int num; int quantity; double price; static double discount; static double sum; static int n; };

void Pr::cal() { double rate=1; if(quantity>10) rate=0.98*rate;

sum=sum=quantity*price*rate*(1-discount); n=n+quantity; }

void Pr::display() { cout<double Pr::average() { return sum/n; }

double Pr::discount=0.05; double Pr::sum=0; int Pr::n=0; main() { Pr p[3]={Pr(101,5,23.5),Pr(102,12,24.56),Pr(103,100,21.5)}; for(int i=0;i<3;i++) p[i].cal(); Pr::display(); return 0; }

1、定义一个时钟类Clock,设计成员函数SetAlarm(int hour,int minute,int second)设置响铃时间;用run()成员函数模拟时钟运行,当运行到响铃时间时提示响铃。

#include using namespace std; class Clock {

public: Clock(int h,int m,int s) { hour=(h>23?0:h); minute=(m>59?0:m); second=(s>59?0:s); } void SetAlarm(int h,int m,int s) { Ahour=(h>23?0:h); Aminute=(m>59?0:m); Asecond=(s>59?0:s); } void ShowTime() { cout<<\

} void run() { second=second+1; if(second>59) { second=0; minute=minute+1; } if(minute>59) { minute=0; hour=hour+1; } if(hour>24) hour=0; if(hour==Ahour&&minute==Aminute&&second==Asecond) { cout<<\ } } private: int hour; int minute; int second; int Ahour; int Aminute; int Asecond; }; main() { Clock D1(7,59,57); D1.ShowTime(); D1.SetAlarm(8,0,0); for(int i=0;i<3600*24*3+100;i++) D1.run(); D1.ShowTime(); }

2、有一信息管理系统,要求检查每一个登录系统的用户(User)的用户名和口令,系统检查合格以后方可登录系统,用C++程序予以描述。

#include using namespace std; #include const int N=20; class User

{

public: User(char *name,char *pass) {

strcpy(username[num],name) ; strcpy(password[num],pass); for(int i=0;password[num][i]!='\\0';i++) password[num][i]+=i; num++; } AddUser(char *name,char *pass) { strcpy(username[num],name); strcpy(password[num],pass); for(int i=0;password[num][i]!='\\0';i++) password[num][i]+=i; num++; } int loggin(char *name,char *pass) { for(int i=0;i

int User::num=0; void main() { char name[10],pass[10]; User u1(\ u1.AddUser(\ u1.AddUser(\ cout<<\ cin>>name;

}

cout<<\cin>>pass;

if(u1.loggin(name,pass)>=0) cout<<\else cout<<\

3、定义一个FDAccount类,用以描述一个定期存折(fixed deposit),实现现金支取、余额合计、信息显示等。存折基本信息包括账号、账户名称、存款余额、存款期限(以月为单位)、存款利率(以百分点为单位)等。

#include using namespace std;

class Customer //客户类 { friend class BankQueue;//将BankQueue类声明为Customer类的友元类 private: int account;//账号 int amount;//金额,大于0表示存款,小于0表示取款 public: Customer(int account=-1,int amount=0); int GetAccount();//取账号 int GetAmount();//取金额 };

const int ARRAY_SIZE=10; class BankQueue //队列类 {

public: BankQueue(); void EnQueue(Customer newElem);//元素入队列 Customer DelQueue();//元素出队列 int GetLength()//取队列长度 { return length; } void Print() const;//输出队列 private: Customer elem[ARRAY_SIZE];//存放队列元素的数组 int first;//队列首元素位置 int length;//队列长度 };

Customer::Customer(int account,int amount) { this->account=account; this->amount=amount; }

int Customer::GetAccount()//取账号 { return account; }

int Customer::GetAmount()//取金额 { return amount; }

BankQueue::BankQueue() { first=0; length=0; }

void BankQueue::EnQueue(Customer newElem)//元素入队列 { int pos=(first+length)%ARRAY_SIZE;//计算新元素的存放位置 elem[pos]=newElem;//存储新元素 length++;//队列长度加1 }

Customer BankQueue::DelQueue()//元素出队列

{//暂存队列首元素,访问了Customer类的私有数据成员 Customer ret(elem[first].account,elem[first].amount); first=(first+1)%ARRAY_SIZE;//队列首元素位置进1 length--;//队列长度减1 return ret;//返回队列首元素 }

void BankQueue::Print() const//输出队列 { int pos=first; cout<<\队列中客户:\ for(int i=0;i

void main() { BankQueue q; int i; for(i=0;i<6;i++) q.EnQueue(Customer(i+1,100*(i+1))); q.Print(); Customer a;

}

for(i=0;i<3;i++) { a=q.DelQueue(); cout<<\}

cout<

本文来源:https://www.bwwdw.com/article/8wcv.html

Top