C++ Primer Plus 第11章整理

C++ primer plus 第11章整理

主要内容是类的使用,运算符的重载,友元函数等。

需要注意点整理

  1. 对于运算符的重载 operator(typename) 这样的形式
  2. 有的运算符不能重载,有的在一定条件下可以重载
  3. 类也有自动转换和强制转换 使用explicit可以关闭这种特性

典型例子:使用复数并进行各种操作

//practice7.h 头文件
#ifndef PRACTICE7HEAD_H_
#define PRACTICE7HEAD_H_
#include<iostream>

class Complex_s
{
private:
	double real;	//实数部分
	double imaginary;	//虚数部分
	double module1;	//复数的模
public:
	Complex_s();	//默认构造函数
	Complex_s(const double real,const double imaginary);	//构造函数
	~Complex_s();	//析构函数
	//operator overloading
	Complex_s operator + (const Complex_s& T)	const;
	Complex_s operator - (const Complex_s& T)   const;
	Complex_s operator * (const Complex_s& T)	const;
	Complex_s operator * (const double x) const;
	Complex_s operator ~ ()	const;
	//friends
	friend std::istream& operator>>(std::istream & is,Complex_s& T);
	friend std::ostream& operator<<(std::ostream & os, const Complex_s& T);
	friend Complex_s operator*(const double x, Complex_s& T) { return T * x; };
};

#endif

//practice7 函数实现接口部分
#include<iostream>
#include<cmath>
#include"practice7head.h"

Complex_s::Complex_s()
{
	real = 0;
	imaginary = 0;
	module1 = 0;
}

Complex_s::Complex_s(const double real,const double imaginary)
{
	this->real = real;
	this->imaginary = imaginary;
	this->module1 = sqrt(real * real + imaginary * imaginary);
}

Complex_s::~Complex_s()
{

}

//operator overloading
Complex_s Complex_s::operator + (const Complex_s& T)	const
{
	Complex_s tem;
	tem.real = real + T.real;
	tem.imaginary = imaginary + T.imaginary;
	tem.module1 = sqrt(module1 * module1 + T.module1 * T.module1);
	return tem;
}

Complex_s Complex_s::operator - (const Complex_s& T)   const
{
	Complex_s tem;
	tem.real = real - T.real;
	tem.imaginary = imaginary - T.imaginary;
	tem.module1 = sqrt(module1 * module1 - T.module1 * T.module1);
	return tem;
}

Complex_s Complex_s::operator * (const Complex_s& T)   const
{
	Complex_s tem;
	tem.real = real * T.real - imaginary * T.imaginary;
	tem.imaginary = real * T.imaginary + imaginary * T.real;
	tem.module1 = sqrt(tem.real * tem.real + tem.imaginary * tem.imaginary);
	return tem;
}

Complex_s Complex_s::operator * (const double x) const
{
	Complex_s tem;
	tem.real = real * x;
	tem.imaginary = imaginary * x;
	tem.module1 = module1 * x;
	return tem;
}

Complex_s Complex_s::operator ~ ()	const
{
	Complex_s tem;
	tem.real = -real;
	tem.imaginary = -imaginary;
	tem.module1 = -module1;
	return tem;
}

//friend

std::istream& operator>>(std::istream & is, Complex_s& T)
{
	std::cout << "input the real part and the imaginary part!" << std::endl;
	std::cout << "First input real!" << std::endl;
	is >> T.real ;
	if (!is)
	{
		std::cout << "You choose quit, or meeting the unknowing error!" << std::endl;
		return is;
	}
	std::cout << "Now you need enter imaginary!" << std::endl;
	is >> T.imaginary;
	T.module1 = sqrt(T.real * T.real + T.imaginary * T.imaginary);
	return is;
}

std::ostream& operator<<(std::ostream & os, const Complex_s& T)
{
	os << "The real part is " << T.real << std::endl
		<< "The imaginary part is " << T.imaginary <<"i"<< std::endl
		<< "The module is " << T.module1 << std::endl;
	return os;
}

//practice7 main 主函数部分
#include<iostream>
#include<cmath>
#include"practice7head.h"
using namespace std;

int main(void)
{
	Complex_s a(3.0, 4.0);
	Complex_s c;
	cout << "Enter a complex number(q to quit):\n";
	while (cin >> c)
	{
		cout << "c message \n " << c << endl;
		cout << "complex conjugate message\n " << ~c << endl;
		cout << "a message\n " << a << endl;
		cout << "a + c message\n " << a + c << endl;
		cout << "a - c message\n " << a - c << endl;
		cout << "a * c message\n " << a * c << endl;
		cout << "2 * c message\n " << 2 * c << endl;
		cout << "Enter a complex number(q to quit):" << endl;
	}
	cout << "Done!" << endl;
	return 0;
}

实现中容易犯错的地方

  1. istream 和 ostream 类不能“赋值”,这就意味着友元函数在重载>> 和 <<时需要接受这个两个类具体对象的引用,就如代码中的is os。
  2. c++规定重载的~只能有一个参数(???为啥)但是类方法默认传入了this指针,也就是意味着重载的 ~不能接受任何参数,详细可以看上面对 ~ 重载的类成员函数。
  3. 类成员函数中的简单函数会被认为是内联函数,这时候不用显式的使用inline,但是必须在声明时就定义函数。
  4. 更多的去使用const,增加安全性,养成好习惯。

第一篇博文,诸多不足,可能有很多描述错误,欢迎交流改进!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值