对象和类复习题(c++ prime plus )

2023-03-07,,

第一题:

复习题5描述的类提供方法定义,并编写一个小程序来演示所有的特性:

bank.h

#ifndef BACK_H_
#define BACK_H_
#include <string> class Bankaccount
{
private:
std::string accountname;//用户名称
std::string username;//用户名字
double money;
public:
Bankaccount(const std::string username,const std::string accountname,double money);//默认析构函数
void show() const;//表示函数
void deposit(double cash);//存钱
void withdraw(double cash);//取钱
}; #endif

bank.cpp

#include <iostream>
#include "bank.h" Bankaccount::Bankaccount(const std::string username,const std::string accountname,double money)
{
this->username=username;
this->accountname=accountname;
this->money=money;
} void Bankaccount::deposit(double cash)
{
if(cash>0)
{
money+=cash;
}
else
{
std::cout<<"Input is error"<<std::endl;
}
} void Bankaccount::withdraw(double cash)
{
if(cash>0&&cash<money)
{
money-=cash;
}
else
{
std::cout<<"输入的数字小于0或者大于本金!"<<std::endl;
}
} void Bankaccount::show() const
{
using namespace std;
cout<<"username="<<username<<endl;
cout<<"accountname="<<accountname<<endl;
cout<<"money="<<money<<endl;
}

userbank.cpp

#include <iostream>
#include "bank.h" using namespace std; int main()
{ double money; Bankaccount myaccount("whp","whpazz",468.0);
myaccount.show();
cout<<"输入你的存款数额"<<endl;
cin>>money;
myaccount.deposit(money);
myaccount.show();
cin.ignore();
cout<<"输入你的取款数额"<<endl;
cin>>money;
myaccount.withdraw(money);
myaccount.show();
return 0; }

第二题

它使用了一个string对象和一个字符数组,让您能够比较它们的用法。请提供未定义的方法的代码,以完成这个类的实现。再编写一个使用这个类的程序,它使用了三种可能的构造函数调用(没有参数、一个参数和两个参数)以及两种显示方法。下面是一个使用这些构造函数和方法的例子

person.h:

#ifndef PERSON_H_
#define PERSON_H_ #include <string>
#include <cstring>
class Person
{
private:
static const int LIMIT=25;
std::string lname;
char fname[LIMIT];
public:
Person(){lname="",fname[0]='\0';};
Person(const std::string & ln,const char *fn="Hello");
void show()const;
void FormalShow()const;
}; #endif

person.cpp:

#include <iostream>
#include "person.h" Person::Person(const std::string & ln,const char * fn)
{
lname=ln;
strcpy(fname,fn);
} void Person::show() const
{
std::cout<<"The lname is"<<lname<<std::endl;
} void Person::FormalShow()const
{
std::cout<<"The fname is"<<fname<<std::endl;
}

userperson.cpp:

#include <iostream>
#include "person.h" using namespace std; int main()
{
Person myname;
Person myname1("whp");
myname.show();
myname.FormalShow();
cout<<"----------"<<endl;
myname1.show();
myname1.FormalShow();
Person myname2("whp","whpazz");
cout<<"----------"<<endl;
myname2.show();
myname2.FormalShow();
return 0; }

第三题:

完成第9章的编程练习1,但要用正确的golf类声明替换那里的代码。用带合适参数的构造函数替换setgolf(golf &, const char *, int), 以提供初始值。保留setgolf()的交互版本,但要用构造函数来实现它(例如,setgolf()的代码应该获得数据,将数据传递给构造函数来创建一个临时对象,并将其赋给调用对象,即*this)

golf.h

#ifndef GOLF_H_
#define GOLF_H_ const int Len=40; class golf
{
private:
char fullname[Len];
int handicap;
public: golf(const char * fn,int ha);
golf();
void handicaps(int hc);
void showgolf() const;
}; #endif

golf.cpp

#include <iostream>
#include "golf.h"
#include <cstring> golf::golf(const char * fn,int ha)
{
strcpy(fullname,fn);
handicap=ha; } golf::golf()
{
char temp[40];
int hc;
std::cout<<"请输入你的名字"<<std::endl;
std::cin.getline(temp,40);
std::cin.ignore();
std::cout<<"请输入你的handicap:"<<std::endl;
std::cin>>hc;
*this=golf(temp,hc); } void golf::handicaps(int ha)
{
handicap=ha;
} void golf::showgolf()const
{
std::cout<<"名字:"<<fullname<<std::endl;
std::cout<<"handicap"<<handicap<<std::endl;
}

usergolf.cpp

#include <iostream>
#include "golf.h" int main()
{
golf mygolf("whp",468);
mygolf.showgolf();
std::cout<<"------------------"<<std::endl;
golf mygolf1;
mygolf1.showgolf();
std::cout<<"------------------"<<std::endl;
mygolf1.handicaps(200);
mygolf1.showgolf();
return 0;
}

第四题

完成第9章的编程练习4,但将Sales结构及相关的函数转换为一个类及其方法。用构造函数替换setSales(sales &, double [], int)函数。用构造函数实现setSales(Slaes &)方法的交互版本。将类保留在名称空间SALES 中。

setsale.h

#ifndef SETSALE_H_
#define SETSLE_H_ namespace SALES
{ class sale
{
private:
double sales[4];
double average;
double max;
double min;
public:
sale();
sale(const double ar[],int n);
void shawSales()const ;
}; } #endif

setsale.cpp

#include <iostream>
#include "setsale.h" namespace SALES
{
sale::sale(const double ar[],int n)
{
if(n<4)
{
for(int i=0;i<n;i++)
{
sales[i]=ar[i];
}
for(int j=n;j<4;j++)
{
sales[j]=0;
}
}
else
{
for(int i=0;i<4;i++)
{
sales[i]=ar[i];
}
}
average=(sales[0]+sales[1]+sales[2]+sales[3])/4;
max=0.0;
min=10000.0;
for(int i=0;i<4;i++)
{
if(sales[i]>max)
{
max=sales[i];
}
if(sales[i]<min)
{
min=sales[i];
}
}
} sale::sale()
{
using namespace std;
cout<<"请输入你的sale!"<<endl;
for(int i=0;i<4;i++)
{
cin>>sales[i];
}
average=(sales[0]+sales[1]+sales[2]+sales[3])/4;
max=0.0;
min=10000.0;
for(int i=0;i<4;i++)
{
if(sales[i]>max)
{
max=sales[i];
}
if(sales[i]<min)
{
min=sales[i];
}
}
}
void sale::shawSales() const
{
using namespace std;
cout<<sales[0]<<"--"<<sales[1]<<"--"<<sales[2]<<"--"<<sales[3]<<"--"<<endl;
cout<<"average="<<average<<endl;
cout<<"max="<<max<<" "<<"min="<<min<<endl;
}
}

usersetsales.cpp

#include <iostream>
#include "setsale.h" using namespace std;
using namespace SALES; int main()
{
double arc[10]={9.0,10.0,10.0,10.0,10.0,10.0};
sale mysale(arc,10);
mysale.shawSales();
cout<<"------------------"<<endl;
sale mysale1;
mysale1.shawSales();
return 0; }

第五题

编写一个程序,它从栈中添加和删除customer结构(栈用Stack类声明表示)。每次customer结构被删除时,其payment的值都被加入到总数中,并报告总数。注意:应该可以直接使用Stack类而不作修改;只需修改typedef声明,使Item的类型为customer,而不是unsigned long即可。

stack.h

#ifndef STACK_H_
#define STACK_H_ struct customer
{
char fname[40];
double payment;
}; typedef customer Item; class Stack
{
private:
Item items[3];
int top;
public:
Stack();
bool push(const Item & item);
bool pop(Item & item);
bool isfull()const;
bool isempty() const;
}; #endif

stack.cpp

#include <iostream>
#include "stack.h" Stack::Stack()
{
top=0;
} bool Stack::isempty()const
{
return top==0;
} bool Stack::isfull()const
{
return top==3;
} bool Stack::push(const Item & item)
{
if(top<3)
{
items[top++]=item;
return true;
}
else
{
return false;
} } bool Stack::pop(Item & item)
{
if(top<0)
{
return false;
}
else
{
item=items[--top];
return true;
}
}

userstack.cpp

#include <iostream>
#include "stack.h" using namespace std; int main()
{
Stack tk;
double totalpayment=0.0;
customer temp;
cout<<"1.....>入栈"<<endl;
cout<<"2'''''>出栈"<<endl;
char c;
int sign;
cout<<"是否开始"<<endl;
while(cin>>c&&c=='y')
{
cin>>sign;
cin.ignore();
switch(sign)
{
case 1:
cout<<"输入你的名字"<<endl;
cin.getline(temp.fname,40);
//cin.ignore();
cout<<"输入你的支付的数目"<<endl;
cin>>temp.payment;
if(tk.isfull())
{
cout<<"栈以满"<<endl;
}
else
{
tk.push(temp);
}
break;
case 0:
if(tk.isempty())
{
cout<<"栈为空栈"<<endl;
}
else
{
totalpayment+=temp.payment;
tk.pop(temp);
cout<<"开始退栈"<<temp.fname<<endl;
}
break;
}
cout<<"是否开始"<<endl;
}
cout<<"totalpayment="<<totalpayment<<endl;
return 0;
}

第六题

Move.h

#ifndef MOVE_H_
#define MOVE_H_ class Move
{
private:
double x;
double y;
public:
Move(double a=0.0,double b=0.0);
Move & add(const Move & m) const;
void showmove()const;
void reset(double a=0,double b=0);
}; #endif

Move.cpp

#include <iostream>
#include "Move.h"
using namespace std; Move::Move(double a,double b)
{
x=a;
y=b;
} Move & Move::add(const Move & m) const
{
static Move ns;
ns.x=m.x+this->x;
ns.y=m.y+this->y;
return ns;
} void Move::reset(double a,double b)
{
x=a;
y=b;
} void Move::showmove()const
{
cout<<"x="<<x<<std::endl;
cout<<"y="<<y<<std::endl;
}

UserMove.cpp

#include <iostream>
#include "Move.h" using namespace std;
int main()
{
Move m0(2.5,2.5);
Move m1(1.5,1.5);
double a,b;
cout<<"输入a,b"<<endl;
cin>>a;
cin>>b;
Move m2(a,b);
m2=m2.add(m0);
cout<<"M0为"<<endl;
m0.showmove();
cout<<"M1为"<<endl;
m1.showmove();
cout<<"更新为"<<endl;
m2.showmove();
return 0;
}

第七题

Betelgeusean plorg有这些特征。

数据:

plorg的名称不超过19个字符;
plorg有满意指数(CI),这是一个整数。
操作:

新的plorg将有名称,其CI值为50;
plorg的CI可以修改;
plorg可以报告其名称和CI;
plorg的默认名称为“Plorga”。

plorg.h

#ifndef PLORGH_H_
#define PLORGH_H_ class Plorg
{
private:
char fnam[20];
int CI;
public:
Plorg();
void rwrite(const char * str,int CIS);
void show_cls()const; }; #endif

ploorg.cpp

#include <iostream>
#include "plorg.h"
#include <cstring> Plorg::Plorg()
{
strcpy(fnam,"whp");
CI=50;
} void Plorg::rwrite(const char * str,int CIS)
{
strcpy(fnam,str);
CI=CIS;
} void Plorg::show_cls()const
{
using namespace std;
cout<<"Fname="<<fnam<<endl;
cout<<"CI="<<CI<<endl;
}

userplogry.cpp

#include <iostream>
#include "plorg.h" int main()
{
Plorg mtct;
mtct.show_cls();
mtct.rwrite("wer",52);
mtct.show_cls();
return 0;
}

对象和类复习题(c++ prime plus )的相关教程结束。

《对象和类复习题(c++ prime plus ).doc》

下载本文的Word格式文档,以方便收藏与打印。