C++primer plus 第十一章编程练习

2023-11-13

银行账户类,头文件 

#ifndef HEAD_H_
#define HEAD_H_

#include<iostream>
#include<string>

using std::cout;
using std::endl;
using std::string;

namespace BANKID
{
	class BankID
	{
	public:
		BankID(const string&, const double = 0);

		void show(void)const;

		void add(const double);

		void get(const double);

		~BankID();

	private:
		static int sm_makeID;
		string m_name;
		int  m_ID;
		double m_depoist;
	};
}

#endif // !HEAD_H_

源文件 

namespace BANKID
{
	BankID::BankID(const string& name, const double deposit):m_name(name),m_depoist(deposit)
	{
		this->m_ID=BankID::sm_makeID;
		++BankID::sm_makeID;
	}

	BANKID::BankID::~BankID()
	{
		--BankID::sm_makeID;
	}

	void BANKID::BankID::show(void)const
	{
		cout << this->m_name << "  " << this->m_ID << "   " << this->m_depoist << endl;
	}

	void BANKID::BankID::add(const double deposit)
	{
		this->m_depoist += deposit;
	}

	void BANKID::BankID::get(const double deposit)
	{
		this->m_depoist -= deposit;
	}

	int BankID::sm_makeID = 1000;
}

 时间类,头文件

#ifndef HEAD_H_
#define HEAD_H_

#include<iostream>

using std::cout;
using std::endl;
using std::ostream;

namespace TIME
{
	class Time
	{

		friend ostream& operator<<(ostream&, const Time&);


	public:
		Time(void);
		Time(int, int minutes=0);

		const Time operator+(const Time&)const;
		const Time operator-(const Time&)const;
		const Time operator*(const int)const;
		const Time operator/(const int)const;

		Time& operator=(const Time&);
		Time& operator+=(const Time&);
		Time& operator-=(const Time&);
		Time& operator*=(const int);
		Time& operator/=(const int);

		const bool operator<(const Time&)const;
		const bool operator<=(const Time&)const;
		const bool operator>(const Time&)const;
		const bool operator>=(const Time&)const;

		int change_minutes(void)const;

		void show(void)const;

	private:
		//运算辅助函数,把一个分制时间改成时分制时间
		inline const Time count(const int minutes)const 
		{
			Time temp;
			temp.m_hours = minutes / 60;
			temp.m_minutes = minutes % 60;
			return temp;
		}
		//时分制时间转分制时间
		inline const int change(void)const
		{
			int minutes = this->m_hours * 60 + this->m_minutes;
			return minutes;
		}

		int m_hours;
		int m_minutes;
	};
}
#endif // !HEAD_H_

源文件

namespace TIME
{
	//构造
	TIME::Time::Time()
	{
		this->m_hours = this->m_minutes = 0;
	}
	TIME::Time::Time(int hours, int minutes)
	{
		if (minutes > 60)
			exit(EXIT_FAILURE);
		this->m_hours = hours;
		this->m_minutes = minutes;
	}
	//重载算数运算
	const Time TIME::Time::operator+(const Time& t) const
	{
		unsigned  minutes = this->change() + t.change();
		return count(minutes);
	}
	const Time TIME::Time::operator-(const Time& t) const
	{
		int minutes = this->change() - t.change();
		if (minutes < 0)
			exit(EXIT_FAILURE);
		return count(minutes);
	}
	const Time TIME::Time::operator*(const int x) const
	{
		if (x < 0)
			exit(EXIT_FAILURE);
		int minutes = this->change() * x;
		return count(minutes);
	}
	const Time TIME::Time::operator/(const int x) const
	{
		if (x <= 0)
			exit(EXIT_FAILURE);
		int minutes = this->change() / x;
		return count(minutes);
	}
	//重载(复合)赋值运算
	Time& TIME::Time::operator=(const Time& t)
	{
		this->m_hours = t.m_hours;
		this->m_minutes = t.m_hours;
		return *this;
	}
	Time& TIME::Time::operator+=(const Time& t)
	{
		return *this = *this + t;
	}
	Time& TIME::Time::operator-=(const Time& t)
	{
		return *this = *this - t;
	}
	Time& TIME::Time::operator*=(const int x)
	{
		return *this = (*this) * x;
	}
	Time& TIME::Time::operator/=(const int x)
	{
		return *this = (*this) / x;
	}
	//重载条件运算
	const bool TIME::Time::operator<(const Time& t) const
	{
		return this->change() < t.change();
	}
	const bool TIME::Time::operator>(const Time& t) const
	{
		return this->change()> t.change();
	}
	const bool TIME::Time::operator<=(const Time& t) const
	{
		return!(*this > t);
	}
	const bool TIME::Time::operator>=(const Time& t) const
	{
		return !(*this < t);
	}

	int TIME::Time::change_minutes(void) const
	{
		return this->change();
	}
	ostream& TIME::operator<<(ostream& mycout, const Time& t)
	{
		mycout << t.m_hours <<"时" << t.m_minutes<<"分";
		return mycout;
	}
	void TIME::Time::show() const
	{
		cout << this->m_hours << "    " << this->m_minutes << endl;
	}
}

 复数类,头文件

#ifndef COMPLEX_H_
#define COMPLEX_H_

#include<iostream>

using std::ostream;
using std::istream;
using std::cout;
using std::cin;

namespace COMPLEX
{
	class Complex
	{
		friend ostream& operator<<(ostream&, const Complex&);
		friend istream& operator>>(istream&,  Complex&);

		friend const Complex operator+(const double, const Complex&);
		friend const Complex operator-(const double, const Complex&);
		friend const Complex operator*(const double, const Complex&);
	public:
		Complex();
		Complex(double,double);
		//隐式类型转换,重载>>无线递归??
		Complex(double);//转换!!!

		const Complex operator+(const Complex&)const;
		const Complex operator-(const Complex&)const;
		const Complex operator*(const Complex&)const;
		 Complex& operator~();
	private:
		double m_true;
		double m_imaginary;
	};
}
#endif // !COMPLEX_H_

源文件

namespace COMPLEX
{
	COMPLEX::Complex::Complex() :m_true(0.0), m_imaginary(0.0)
	{
	}
	COMPLEX::Complex::Complex(double a, double b) :m_true(a), m_imaginary(b)
	{
	}
	COMPLEX::Complex::Complex(double a) :m_true(a),m_imaginary(0.0){}
	//成员运算
	const Complex COMPLEX::Complex::operator+(const Complex& c) const
	{
		Complex temp(*this);
		temp.m_true += c.m_true;
		temp.m_imaginary += c.m_imaginary;
		return temp;
	}
	const Complex COMPLEX::Complex::operator-(const Complex& c) const
	{
		Complex temp(*this);
		temp.m_true -= c.m_true;
		temp.m_imaginary -= c.m_imaginary;
		return temp;
	}
	const Complex COMPLEX::Complex::operator*(const Complex& c) const
	{
		Complex temp;
		temp.m_true = this->m_true * c.m_true - this->m_imaginary * c.m_imaginary;
		temp.m_imaginary = this->m_imaginary * c.m_true + this->m_true * c.m_imaginary;
		return temp;
	}
	Complex& COMPLEX::Complex::operator~()
	{
		if (!this->m_imaginary)
			return *this;
		this->m_imaginary = -this->m_imaginary;
		return *this;
	}
	//友元运算
	const Complex COMPLEX::operator+(const double n, const Complex& c)
	{
		Complex temp(c);
		temp.m_true += n;
		return temp;
	}
	const Complex COMPLEX::operator-(const double n, const Complex& c)
	{
		Complex temp(c);
		temp.m_true -= n;
		return temp;
	}
	const Complex COMPLEX::operator*(const double n, const Complex& c)
	{
		Complex temp(c);
		temp.m_true *= n;
		return temp;
	}
	//输入输出
	ostream& COMPLEX::operator<<(ostream& mycout, const Complex& c)
	{
		mycout << '(' << c.m_true << ',' << c.m_imaginary << "!)";
		return mycout;
	}
	istream& COMPLEX::operator>>(istream& mycin,  Complex& c)
	{
		cout << "请输入实部: ";
		mycin >> c.m_true;
		cout << "请输入虚部: ";
		mycin >> c.m_imaginary;
		return mycin;
	}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C++primer plus 第十一章编程练习 的相关文章

  • 为 DocumentDb 设置自定义 json 转换器

    我正在使用类型化 DocumentQuery 从 Azure DocumentDb 集合中读取文档 from f in client CreateDocumentQuery
  • 无法在 QGLWidget 中设置所需的 OpenGL 版本

    我正在尝试在 Qt 4 8 2 中使用 QGLWidget 我注意到 QGLWidget 创建的默认上下文不显示 OpenGL 3 1 以上的任何输出 Qt wiki 有一个教程 http qt project org wiki How t
  • 并行运行多个任务

    我有一个代理列表 每个代理都会访问不同的站点并从站点中提取所需的数据 目前它一次只做一个 但我希望同时运行 10 20 个任务 这样它就可以一次性从 20 个站点下载 而不是只下载一个 这是我目前正在做的事情 private async T
  • 无法在 CUDA 中找到 1 到 100 数字的简单和?

    我正在研究使用 CUDA 的图像处理算法 在我的算法中 我想使用 CUDA 内核找到图像所有像素的总和 所以我在cuda中制作了内核方法 来测量16位灰度图像的所有像素的总和 但我得到了错误的答案 所以我在cuda中编写了一个简单的程序来查
  • 使用 POST 的 HttpWebRequest 的性能

    我有一个用于测试网络服务的小工具 它可以使用 POST 或 GET 调用 Web 服务 使用POST的代码是 public void PerformRequest WebRequest webRequest WebRequest Creat
  • C++中类成员函数相互调用有什么好处?

    我是 C 新手 我发现下面的编程风格对我来说很有趣 我在这里写了一个简化版本 include
  • 从结构调用 C++ 成员函数指针

    我找到了有关调用 C 成员函数指针和调用结构中的指针的信息 但我需要调用结构内部存在的成员函数指针 但我无法获得正确的语法 我在类 MyClass 的方法中有以下代码片段 void MyClass run struct int MyClas
  • Visual Studio 2013 调试器显示 std::string 的奇怪值

    我有一个大型的 cmake 生成的解决方案 其中包含许多项目 由于某种原因 我无法查看字符串的内容 因为根据调试器 Bx Buf含有一些垃圾 text c str 正确返回 Hello 该问题不仅仅发生在本地字符串上 返回的函数std st
  • 我担心我添加了太多接口

    我正在构建我的领域模型并继续重构它 正如我所做的那样 我发现我喜欢接口 因为它允许我根据接口为具体类型创建可重用的方法 控制器 视图 但是 我发现每次向域实体之一添加新属性时 我都会创建一个接口 例如 我有一个会员状态从抽象继承的对象Ent
  • 用于 C++ 中图像分析的 OpenCV 二进制图像掩模

    我正在尝试分析一些图像 这些图像的外部周围有很多噪声 但内部有一个清晰的圆形中心 中心是我感兴趣的部分 但外部噪声正在影响我对图像的二进制阈值处理 为了忽略噪音 我尝试设置一个已知中心位置和半径的圆形蒙版 从而使该圆之外的所有像素都更改为黑
  • 从图像创建半透明光标

    是否可以从图像创建光标并使其半透明 我目前正在拍摄自定义图像并覆盖鼠标光标图像 如果我可以将其设为半透明 那就太好了 但不是必需的 销售人员喜欢闪亮的 目前正在做这样的事情 Image cursorImage customImage Get
  • ASP.NET - Crystal Report Viewer 打印按钮在 ASP.NET 中不起作用

    我正在使用 Visual Studio 2008 但我遇到了水晶报告问题 当我单击打印按钮时 它会将我带到弹出窗口 但未找到页面 弹出的网址是 http localhost aspnet client System Web 2 0 5072
  • 当我尝试传递临时地址作为参数时,它是一个 UB 吗?

    对于以下 C 代码 include
  • C# 可以为控制台应用程序部分类“程序”类吗?

    我想知道是否可以将为任何控制台应用程序创建的默认 程序 类更改为部分类 我想这样做是因为我想要更好的组织 而不是将所有方法都放在按区域分类的 1 个文件中 对我来说 将某些方法类别放在单独的文件中会更有意义 我对分部类的理解是 它是多个文件
  • MINIX内部碎片2

    我正在用 C 语言编写一些软件 它递归地列出给定目录中的所有文件 现在我需要计算出内部碎片 我花了很长时间研究这个问题 发现 ext2 上的内部碎片只发生在最后一个块中 我知道理论上你应该能够从索引节点号获得第一个和最后一个块地址 但我不知
  • Clang 5.0 上的 vsprintf 和 vsnprintf [-Wformat-nonliteral] 警告

    我有这段代码 static void err doit int errnoflag int level const char fmt va list ap int errno save unsigned long n char buf MA
  • g++ / gcc 是否支持 C++20 新的atomic_flag 功能?

    根据参考参数 https en cppreference com w cpp atomic atomic flag c 20 有丰富的 对我来说有用的 支持atomic flag运营 然而 目前尚不清楚 gcc 是否支持这些功能 它们在任何
  • c++ - <未解析的重载函数类型>

    在我的班级里叫Mat 我想要一个将另一个函数作为参数的函数 现在我有下面 4 个函数 但是在调用 print 时出现错误 第二行给了我一个错误 但我不明白为什么 因为第一行有效 唯一的区别是功能f不是班级成员Mat but f2是 失败的是
  • C++ 中的析构函数

    我的 AB h 文件中有一个构造函数 class AB private int i public AB i 0 constructor AB i 0 destructor virtual void methodA unsigned int
  • 如何配置 qt Creator 以显示 C++ 代码而不是反汇编程序?

    昨天我做了很多事情 比如更新 GCC Clang 和重新安装 Qt Creator 今天 在逐步调试我的代码时 调试器显示的是反汇编代码 而不是我编写的 C 代码 紧迫F10 or F11 调试器正在进入汇编代码而不是 cpp nor h我

随机推荐