我是靠谱客的博主 阔达墨镜,最近开发中收集的这篇文章主要介绍C++ Primer Plus 第11章整理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 messagen " << ~c << endl;
cout << "a messagen " << a << endl;
cout << "a + c messagen " << a + c << endl;
cout << "a - c messagen " << a - c << endl;
cout << "a * c messagen " << a * c << endl;
cout << "2 * c messagen " << 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,增加安全性,养成好习惯。

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

最后

以上就是阔达墨镜为你收集整理的C++ Primer Plus 第11章整理C++ primer plus 第11章整理的全部内容,希望文章能够帮你解决C++ Primer Plus 第11章整理C++ primer plus 第11章整理所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(62)

评论列表共有 0 条评论

立即
投稿
返回
顶部