首页 > 编程语言 >C++PrimerPlus中文第六版第10章编程练习答案

C++PrimerPlus中文第六版第10章编程练习答案

时间:2023-02-11 10:33:05浏览次数:41  
标签:std 10 PrimerPlus const number 第六版 Person BankAccount include

1、

//bankaccount.h
#ifndef BANKACCOUNT_H_
#define BANKACCOUNT_H_

#include<string>

class BankAccount
{
private:
	std::string m_account_name;
	const char* m_account_number;
	double m_deposit;
public:
	BankAccount();
	BankAccount(std::string name, const char* number, double deposit);
	void show();
	void save(double fund);
	void draw(double fund);
};


#endif
//bankaccount.cpp
#include<iostream> #include<string> #include "bankaccount.h" BankAccount::BankAccount() { m_account_name = "Default Name"; m_account_number = "********"; m_deposit = 0; } BankAccount::BankAccount(std::string name, const char* number, double deposit) { m_account_name = name; m_account_number = number; if (deposit < 0) { std::cout << "Deposit can't be negative, the deposit set to 0.\n"; m_deposit = 0; } else m_deposit = deposit; } void BankAccount::show() { using std::cout; using std::endl; using std::ios_base; cout << "Account name: " << m_account_name << endl; cout << "Account number: " << m_account_number << endl; ios_base::fmtflags orig = cout.setf(ios_base::fixed, ios_base::floatfield); std::streamsize prec = cout.precision(2); cout << "Account deposit: $" << m_deposit << endl; cout.setf(orig, ios_base::floatfield); cout.precision(prec); } void BankAccount::save(double fund) { m_deposit += fund; } void BankAccount::draw(double fund) { if (fund > m_deposit) std::cout << "You can't draw the money more than you have. The drawing is failed.\n"; else m_deposit -= fund; }

2、

//person.h
#ifndef PRESON_H_ #define PERSON_H_ #include<string> class Person { private: static const int LIMIT = 25; std::string m_lname; char m_fname[LIMIT]; public: Person() { m_lname = ""; m_fname[0] = '\0'; }; Person(std::string lname, const char* fname = "NoFirstName"); void Show() const; void FormalShow() const; }; #endif
//person.cpp
#include<iostream> #include<cstdlib> #include<string> #include "person.h" Person::Person(std::string lname, const char* fname) { m_lname = lname; strcpy_s(m_fname, fname); } void Person::Show() const { using namespace std; cout << m_fname << ", " << m_lname << endl; } void Person::FormalShow() const { using namespace std; cout << m_lname << ", " << m_fname << endl; }
//useperson.cpp
#include<iostream> #include "person.h" int main() { Person one; Person two("Smythecraft"); Person three("Dimwiddy", "Sam"); one.Show(); one.FormalShow(); two.Show(); two.FormalShow(); three.Show(); three.FormalShow(); }

3、

 

标签:std,10,PrimerPlus,const,number,第六版,Person,BankAccount,include
From: https://www.cnblogs.com/xinmind/p/17110976.html

相关文章