/*******************************************************************************************************************
Author : Cui mingyang
Blog : cx_12586
Time : 2017/10/19
From : C++ Primer Plus第五版第10章编程练习 第1题
*******************************************************************************************************************/
//头文件
#ifndef ACCOUNT_H_
#define ACCOUNT_H_
using namespace std;
class Account
{
private:
string name;
string account;
double savings;
public:
Account();
Account(const string &n,const string &a ,const double money);
void show()const;
void save(const double money);
void withdraw(const double money);
~Account();
};
#endif
//头文件对应的cpp文件
#include<iostream>
#include <string>
#include "Account.h"
Account::Account()
{
name = " ";
account = " ";
savings =0;
}
Account::Account(const string &n,const string &a ,const double money)
{
name = n;
account = a;
savings = money;
}
Account::~Account()
{
cout << "Bye.\n";
}
void Account::show()const
{
cout << "The name is : " << name << endl;
cout << "The account is : "<< account <<endl;
cout << "The saving is : " << savings <<endl;
}
void Account::save(const double money)
{
if (money <0)
{
cerr << "The number of saving can't be negative.\n";
}
else
savings+=money;
}
void Account::withdraw(const double money)
{
if (money > savings)
{
cerr << "The number of withdrawing can't be bigger than savings.\n";
}
else
savings-=money;
}
//演示主程序
#include<iostream>
#include "Account.h"
int main()
{
Account account1("Bob Smith","452164146413541641",7800);
account1.show();
account1.save(1200);
account1.show();
account1.save(-1200);
account1.show();
account1.withdraw(2000);
account1.show();
account1.withdraw(20000);
account1.show();
system("pause");
return 0;
}
/*******************************************************************************************************************
Author : Cui mingyang
Blog : cx_12586
Time : 2017/11/17
From : C++ Primer Plus第五版第10章编程练习 第2题
*******************************************************************************************************************/
//头文件
#ifndef PERSON_H_
#define PERSON_H_
using namespace std;
#include<string>
class Person
{
private:
static const int LIMIT =25;
string lname;
char fname[LIMIT];
public:
Person(){lname="";fname[0]='\0';}
Person(const string & ln,const char *fn= "Heyyou");
void show() const;
void FormalShow()const;
};
#endif
//头文件对应的cpp文件
#include<iostream>
#include<cstring>
#include"Person.h"
using namespace std;
Person::Person(const string & ln,const char *fn)
{
lname = ln;
strncpy(fname,fn,LIMIT-1);
fname[LIMIT-1]='\0';
}
void Person::show() const
{
cout << "The name is : " << fname << " " << lname <<endl;
}
void Person::FormalShow()const
{
cout << "The name is : " << lname << " " << fname <<