学完C再学C++(4)使用类


/*
使用类
不要在一开始学习时就打算记住C++的所有特性。

1.运算符重载
	使运算符多一些用法,例如数组名加数组名可以让,里面每个元素逐一相加。
	 operator +(a,b)重载加号;ab两操作数,返回值是结果

	不能重载C++没有的运算符。
	重载后运算的至少有一个操作数是用户自定的。
	重载运算符原来几个操作数重载也是几个操作数。

2.友元
	非类中成员访问类中私有部分的途径。
	编写原型(重载操作符函数)
	friend ClassName operator*();
	定义不要写friend
	ClassName operator*(){};
	重载<<符号见书393页,非常细节。

	重载是作为类的成员还是不要作为类的成员?
	假设a是对象,b只是一个常数。
	一、作为类的成员可以直接访问类中私有,但是重载符号第一个操作数必须是类的成员,因为重载也相当于类的方法调用。比如 a+b -> a.(b)√ b+a b.(a)×
	二、不是类的成员可以随处使用重载后的操作符,但是访问类中私有要用友元函数,因为b不是类的成员,要b打头就必须友元函数方式重载 b+a√ a+b√

	使用类全tm是代码,得看吐,vector讲的还是重载运算符,没新的,略过。
	stonewt 看半天

3.其他类型转化成类
	ClassName c1;
	c1=114.514;
	利用构造函数进行类型转化。
	C++会找到只有一个double或者float参数的构造函数给填进去例如ClassName(double f){f=xxx;}
	这样就完成了其他类型转化成类。

	若有多个自动转换的构造函数,不存在二义性,才能转。
	如果想进行转化的构造函数有两个,那么第二个要给默认值。就可以自动转化第一个。
	如果想显式转化,也就是你ClassName c =(ClassName)var;时候才有效,需要在转化的构造函数前加explicit。

4.类转换其他类型
	需要用到C++自带的类型转换函数,也就是类似double();
	原型的写法是 operator double();//不能带参数,不能指定返回类型,必须是类方法

	转换函数使用explicit在C++98中不行,C++11可以。

书中:
不要依赖隐式转换,用显式的!
我的想法:
C++的类的类型转换特性其实是语法糖,个人感觉不实用,方便了bug也多了,严谨一点多写几个字用函数赋值,其实也没啥。
这章看了很久,主要是边看边玩手机,上午写DAY4,下午看DAY5,开写
*/

#include <iostream>
#include <fstream>
#include<string.h>
#include "study.h"

#ifdef DAY5
//课后题7,复数运算符重载
#ifndef Hfile
//头文件部分
using namespace std;
class complex
{

private:
	double real;
	double imag;
public:
	complex(double a = 0, double b = 0);
	~complex();
	void show();
	ostream  friend & operator<<(ostream &os, const complex &c);
	istream  friend & operator>>( istream &os, complex &c);
	complex  friend operator+(const complex& c1, const complex& c2);
	complex  friend operator-(const complex& c1, const complex& c2);
	complex  friend operator*(const complex& c1, const complex& c2);
	complex  friend operator*(const double & re, const complex& c2);
	complex  friend operator~(const complex& c);
	void reset(double a, double b);
};
#endif
complex::complex(double a, double b)
{
	this->real = a;
	this->imag = b;
	//cout << this << " new " << a << "+" << b <<"i"<< endl;
}
complex::~complex()
{
	//cout << this << " memary alerly free" << endl;
}
void complex::show()
{
	cout << "real:" << real << "imaginary:" << imag<<endl;
}
ostream & operator<<(ostream &os , const complex &c)
{
	if (c.imag < 0)
		os << c.real << c.imag << "i";
	else
		os << c.real << "+" << c.imag << "i";
	return os;
}
istream & operator>>(istream &os, complex &c)
{
	cout << "real:";
	os >> c.real; 
	cout << "imaginary:";
	os >> c.imag;
	return os;
}
complex operator+(const complex& c1, const complex& c2)
{
	complex c3;
	c3.real = c1.real + c2.real;
	c3.imag = c1.imag + c2.imag;
	return c3;
}
complex operator-(const complex& c1, const complex& c2)
{
	complex c3;
	c3.real = c1.real - c2.real;
	c3.imag = c1.imag - c2.imag;
	return c3;
}
complex operator*(const complex& c1, const complex& c2)
{
	complex c3;
	c3.real = c1.real * c2.real - c1.imag * c2.imag;
	c3.imag = c1.real * c2.imag + c1.imag * c2.real;;
	return c3;
}
complex operator*(const double& re, const complex& c2)
{
	complex c3;
	c3.real = re * c2.real;
	c3.imag = re * c2.imag;
	return c3;
}
complex operator~(const complex& c)
{
	complex c3;
	c3.real = c.real;
	c3.imag = -c.imag;
	return c3;

}
//重置
void complex::reset(double a, double b)
{
	this->real = a;
	this->imag = b;
}
//测试
void little_test()
{
	cout << "creat c1,c2,c3\n";
	complex c1(1.14, 5.14);
	complex c2(3.14, 6.66);
	complex c3;
	cout << "m1:\n";
	c1.reset(5.14, 1.14);
	cin >> c2;
	cout << c2;
}
int main()
{
	complex a(3.0, 4.0);
	complex c;
	cout << "Enter a complex number(q to quit):" << endl;
	while (cin >> c)
	{
		cout << "c is " << c << endl;
		cout << "~c is " << ~c << endl;
		cout << "a+c is " << a + c << endl;
		cout << "a-c is " << a - c << endl;
		cout << "a*c is " << a * c << endl;
		cout << "2*c is " << 2 * c << endl;
		cout << "Enter q to quit" << endl;
	}
	return 0;
}

#endif`

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值