C++ Primer Plus第十章编程练习

本文提供了一系列C++编程练习,涵盖银行账户管理、全名表示、高尔夫球记分、销售记录、收入单处理、坐标运算、使用mutable修饰符以及将函数指针作为参数等主题,旨在提升C++编程技能。
摘要由CSDN通过智能技术生成

1、银行账户

//.h
#ifndef BANK_H_
#define BANK_H_
#include <string>

namespace bank
{
   
	using std::string;
	class Account
	{
   
	private:
		enum{
   LEN = 10};
		string name_;
		char id_[LEN];
		mutable double balance_;
	public:
		Account(string name = "default", const char* id = "000000000", double deposit = 0.0);
		~Account();
		void Show()const;
		void deposit(double)const;
		void withdraw(double)const;

	};
}
#endif

//.cpp
#include "Bank.h"
#include <iostream>
namespace bank
{
   
	Account::Account(string name, const char * id, double cash)
	{
   
		name_ = name;
		strncpy_s(id_, id, LEN);
		id_[LEN - 1] = '\0';
		balance_ = cash;
	}
	Account::~Account()
	{
   

	}
	void Account::Show()const
	{
   
		using std::cout;
		using std::endl;
		cout << "id: " << id_ << '\t' << "name: " << name_ << '\t' << "balance: " << balance_ << endl;
	}
	void Account::deposit(double cash)const 
	{
   
		balance_ += cash;
	}
	void Account::withdraw(double cash)const
	{
   
		using std::cout;
		if (balance_ < cash)
		{
   
			cout << "Your balance (" << balance_ << ") is insufficient!\n";
		}
		else
		{
   
			balance_ -= cash;
		}
	}
}

//main().cpp
#include 'Bank.h'
int main()
{
   
	using bank::Account;
	const Account jan("mmmmi", "10010");
	jan.Show();
	jan.deposit(500);
	jan.Show();
	jan.withdraw(250.5);
	jan.Show();
	jan.withdraw(1000);
	const Account bob;
	bob.deposit(100);
	bob.Show();
	system("pause");
	return 0;
}

2、人的全名

//.h
#ifndef PERSON_H_H
#define PERSON_H_H
#include <string>

namespace per
{
   
	using std::string;
	class Person
	{
   
	private:
		static const int LIMIT = 25;
		string lname_;
		char fname_[LIMIT];
	public:
		Person();
		Person(const string & lname, const char * fname = "default");
		void Show()const;
		void FormalShow()const;
	};
}
#endif

//.cpp
#include "Person.h"
#include <iostream>

namespace per
{
   
	Person::Person()
	{
   
		lname_ = ""; 
		fname_[0] = '\0';
	}
	Person::Person(const string & lname, const char * fname)
	{
   
		lname_ = lname;
		strncpy_s(fname_, fname, LIMIT);
		fname_[LIMIT - 1] = '\0';
	}
	void Person::Show() const
	{
   
		using std::cout;
		using std::endl;
		cout << fname_ << lname_ << endl;
	}
	void Person::FormalShow() const
	{
   
		using std::cout;
		using std::endl;
		cout << lname_ << "," << fname_ 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值