第一题:

为复习题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 )的更多相关文章

  1. Java常见对象Object类中的个别方法

    Java常见对象Object类 public int hashCode() : 返回该对象的哈希码值. 注意:哈希值是根据哈希算法计算出来的一个值,这个值和地址值有关,但是不是实际地址值.你可以理解成 ...

  2. .NET 基础 一步步 一幕幕[面向对象之对象和类]

    对象和类 本篇正式进入面向对象的知识点简述: 何为对象,佛曰:一花一世界,一木一浮生,一草一天堂,一叶一如来,一砂一极乐,一方一净土,一笑一尘缘,一念一清静.可见"万物皆对象". ...

  3. Python-面向对象(类)二

    一.成员修饰符 • 共有成员 • 私有成员 __+字段 __:成员修饰符 无法直接访问,只能通过该成员所属类的方法简介访问 class Foo: def __init__(self, name, ag ...

  4. Python-面向对象(类)一

    一.如何创建类 class cls: pass 二.创建方法 构造方法: __init__(self, arg) obj = 类('a1') 普通方法: obj = 类('xxx') obj.普通方法 ...

  5. CoreJava学习笔记1-基本概念、对象和类

    一.    java的基本程序设计结构 (一)   java共有8种基本类型:4种整型,2种浮点类型,1种char,1种boolean. 1)       4种整型:byte(1).short(2). ...

  6. XML转换为对象操作类详解

    //XML转换为对象操作类 //一,XML与Object转换类 using System.IO; using System.Runtime.Serialization.Formatters.Binar ...

  7. Python全栈--9.1--面向对象进阶-super 类对象成员--类属性- 私有属性 查找源码类对象步骤 类特殊成员 isinstance issubclass 异常处理

    上一篇文章介绍了面向对象基本知识: 面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 是一个模板,模板中包装了多个“函数”供使用(可以讲多函数中公用的变量封装到对象中) 对象 ...

  8. js之数组,对象,类数组对象

    许久不写了,实在是不知道写点什么,正好最近有个同事问了个问题,关于数组,对象和类数组的,仔细说起来都是基础,其实都没什么好讲的,不过看到还是有很多朋友有些迷糊,这里就简单对于定义以及一下相同点,不同点 ...

  9. winform中利用反射实现泛型数据访问对象基类(1)

    考虑到软件使用在客户端,同时想简化代码的实现,就写了一个泛型的数据访问对象基类,并不是特别健全,按道理应该参数化的方式实现insert和update,暂未使用参数化,抽时间改进. /// <su ...

  10. 对于python,一切事物都是对象,对象基于类创建

    新建列表.新建string字符串 li1 = [1, 2, 3, 4] li2 = list([1, 2, 3]) s1 = "abc" s2 = str("abc&qu ...

随机推荐

  1. 使用ts二次封装storage(sessionStorage/localStorage)

    export class LocalCache { setCache<T = any>(key: string, value: T): boolean; setCache<T = a ...

  2. 关键aspNetCore processPath 这一行,耗费了一天

    <?xml version="1.0" encoding="UTF-8"?> <configuration>   <locatio ...

  3. 第二周day7

    第二周day7,星期天 所用时间:0 代码量:0 博客量:0 所学知识:提高社交能力,多锻炼.

  4. 【git】3.4 git分支-分支开发工作流

    资料来源 (1) https://git-scm.com/book/zh/v2/Git-%E5%88%86%E6%94%AF-%E5%88%86%E6%94%AF%E5%BC%80%E5%8F%91% ...

  5. fetchAllAssoc 小分析

    这个函数出现在了两个地方 includes\database\database.inc line 2245 includes\database\prefetch.inc line 481 foreac ...

  6. debian(deepin)/ubuntu 安装 mysql5.7

    debian(deepin)/ubuntu 安装mysql5.7 Mysql安装 一.下载安装包 参考博客 https://blog.csdn.net/qq_44231964/article/deta ...

  7. 解决-装了WPS后Windows无法预览word、Excel、PPT等的问题

    https://www.bilibili.com/read/cv10469054/ https://www.cnblogs.com/qq3285862072/p/15097970.html Windo ...

  8. android 获取手机的唯一id,获取当前时间,获取两位随机数

    //获取机器的唯一id public static String getDeviceID() { String deviceID= ""; try{ //一共13位 如果位数不够可 ...

  9. Vue v-once指令 和 v-pre指令

    v-once指令: 1.v-once 所在节点在初始化动态渲染后,就视为静态内容了 2.以后数据的改变不会引起v-once所在结构的更新,可用于优化性能 v-pre指令: 1.跳过其所在节点的编译过程 ...

  10. 5.docker安装redis

    下载redis镜像 不讲那么细了,可以参考docker安装mysql的介绍 docker pull redis 不加冒号和版本表示下载最新版本的 镜像下载完后可以数据 docker images 命令 ...