C++primerplus第六版第⼗章编程练习答案第⼗章编程练习答案
1.
#ifndef ACCOUNT_H_
#define ACCOUNT_H_
//头⽂件
class BankAccount
{
private:
std::string fullname;
std::string account;
double money;
public:
BankAccount(const std::string & client, const std::string & acc, double cash = 0.0);
void save(double cash);
void withdraw(double cash);
void show() const;
};
#endif
#include <iostream>
#include <string>//streng类
#include "account.h"
//类的具体实现
BankAccount::BankAccount(const std::string & client, const std::string & ps, double cash)
{
fullname = client;//string直接赋值
account = ps;
money = cash;
}
void BankAccount::save(double cash)
{
if(cash < 0)
std::cout << "Number of cash can't be negative.\n";
else
money += cash;
}
void BankAccount::withdraw(double cash)
{
if(cash < 0)
std::cout << "Number of cash can't be negative.\n";
else if (cash > money)
std::cout << "You can't get more than you have!\n";
else
money -= cash;
}
void BankAccount::show() const
{
using std::cout;
cout << "Fullname: " << fullname << " Account: " << account << '\n';
cout << " money: $" << money << '\n';
}
2.#include <string>
#include "account.h"
//使⽤类,主程序变得简单
int main()
{
{
using std::cout;
cout << "Bank Account information:\n";
BankAccount b1("Nano Smart", "ASD", 12334.34);
b1.show();
BankAccount b2("Boffo Objects", "ZXC", 5834.983); b2.show();
cout << "\nAfter a month:\n";
b1.save(1231.23);
b1.show();
b1.withdraw(5353.34);
b1.show();
b2.withdraw(555.2322);
b2.show();
b2.save(2354.4);
b2.show();
}
return 0;
}
#ifndef PERSON_H_
#define PERSON_H_
//头⽂件
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 = "Heyyou"); void Show() const;
void FormalShow() const;
};
#endif
#include <string>//string类
#include <cstring>//字符串使⽤strcpy
#include "person.h"
//类的实现
Person::Person(const std::string & ln, const char * fn)
{
lname = ln;
strcpy(fname, fn);
}
void Person::Show() const
{
std::cout << "Full Name:" << std::endl;
std::cout << fname << " " << lname << std::endl;
}
void Person::FormalShow() const
{
std::cout << "Formal Name:" << std::endl;
std::cout << lname << " " << fname << std::endl;
}
#include <iostream>
#include <string>//streng类
#include <cstring>//字符串使⽤strcpy
#include "person.h"
/
/主程序
int main()
{
{
using std::cout;
using std::endl;
Person one;
one.Show();
cout << endl;
one.FormalShow();
cout << endl;
Person two("Smythecraft");
two.Show();
cout << endl;
two.FormalShow();
cout << endl;
Person three("Dimwdiddy", "Sam");
three.Show();
cout << endl;
three.FormalShow();
cout << endl;
}
return0;
}
3.
#ifndef GOLF10_H_
#define GOLF10_H_
#include <string>
//头⽂件
class Golf
{
private:
std::string fullname;
int handicap;
public:
Golf (const std::string name, int hc = 0);
int setgolf();
void sethandicap(const int hc);
void showgolf()const;
};
#endif
#include <iostream>
#include <string>
#include "U10p3golf.h"
/
/类的实现
Golf::Golf(const std::string name, int hc)
{
fullname = name;
handicap = hc;
}
int Golf::setgolf()
{
using namespace std;
int handicap = 0;
int temp = 1;
cout << "The fullname is: ";
getline(cin, fullname);//获取字符串
cout << "The handicap is: ";
cin >> handicap;
Golf g(fullname, handicap);
*this = g;
return fullname == "" ? 0 : 1;
//判断输⼊⾏是否为空
}
void Golf::sethandicap(const int hc)
{
handicap = hc;
}
void Golf::showgolf() const
{
using std::cout;
using std::endl;
cout << "The fullname is: " << fullname << endl;
cout << "The handicap is: " << handicap << endl; }
4.#include <iostream>
#include "U10p3golf.h"
//主程序
int main()
{
isalpha 函数Golf g("Alex Tony", 10);
g.showgolf();
std::cout << std::endl;
g.setgolf();
std::cout << std::endl;
g.showgolf();
std::cout << std::endl;
g.sethandicap(99);
g.showgolf();
return0;
}
#ifndef SALES_H_
#define SALES_H_
//头⽂件
namespace SALES//名称空间
{
class Sales
{
private:
static const int QUARTERS = 4;
double sales[QUARTERS];
double average;
double max;
double min;
public:
Sales();
Sales(const double *ar, const int n);
void setsales();
void showSales() const;
};
}
#endif
#include <iostream>
#include "U10p4sales.h"
//类的实现
SALES::Sales::Sales()//注意这是在名称空间⾥的类的函数,后⾯也是{
for(int i = 0; i < QUARTERS; i++)
sales[i] = 0;
average = 0;
max = 0;
min = 0;
}
SALES::Sales::Sales(const double * ar,const int n)
{
//计算和存储数组
double a, b, c;
a =
b = ar[0];
c = 0.0;
for(int i = 0; i < n; i++)
{
sales[i] = ar[i];
c += ar[i];
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论