侯捷C++学习(一)
//c++学习
//标准库非常重要
//要规范自己的代码
complex c1(2,1);
complex c2;
complex* pc = new complex(0,1);
string s1("Hello");
string s2("world");
string* ps
#include<iostream>
using namespace std;
int main() {
	int i=7;
	cout<<"i="<<i<<endl;
	return 0;
}
#include<iostream>
#include"complex.h"//防卫式声明 
using namespace std;
int main() {
	complex c1(2.1);
	complex c2;
	cout<<c1<<endl;
	cout<<c2<<endl;
	c2=c1+5;
	c2=7+c1;
	c2=c1+c2;
	c2 +=3;
	c2= -c1;
	cout<<(c1=c2)<<endl;
	cout<<(c1!=c2)<<endl;
	cout<<conj(c1)<<endl;
	return 0;
}
class complex {
	public:
		complex (double r=0,double i=0)
			: re (r),im (i);//构造函数才有的初值列,
		//	{ }
		complex& operator +=(const complex&);
		double real () const {
			return re;
		}
		double imag () const {
			return im;
		}
	private:
		double re ,im;
		friend complex&_doapl (complex*,const complex&);
}
{
	complex c1(2,1);
	complex c2;
	complex* p = new complex(4);
	//构造函数是没有返回值的
}
//构造函数可以有很多个overloading重载同名可以存在重载
//常常发生在构造函数里
class complex {
	public:
		complex (double r=0,double i=0)//
			:re(r),im(i) {
		}
		complex () :re(0),im(0);
		{}//不允许重载
		complex& operator += (const complex&);
		double real () const {
			return re;
		}
		double imag () const {
			return im;
		}
	private:
		double re,im;
		friend complex&_doapl (complex*,const complex&);
};
void real(double r) const {
	re =r;
}
//构造函数在private区域
//表示这个函数是不可以被外界调用的
class complex {
	public:
		complex (double r = 0,double i = 0)
			:re(r),im(i) {
		}
		complex& operator +=(const complex&);
		double real () const {
			return re;
		}
		double imag () const {
			return im;
		}
	private:
		double re,im;
		friend complex&_doapl (complex*,const complex&);
};
{
	complex c1(2,1);
	complex c2;
	...
}
//Singleton单份
//可以把构造函数写在private里面
//**在函数的后面加const  class分类里对于不改变数据的,在函数的后面加const
//比如把函数拿出来的
//对于上面的代码
{
	complex c1(2,1);
	cout<<c1.real();
	cout<<c1.imag();
}
{
	const complex c1(2,1);
	cout<< c1.real();
	cout<< c1.imag();
}
//要有const的话要都有const.周全的代码正规
//参数传递
class complex {
	public:
		complex (double r= 0, double i = 0)
			:re (r) , im(i) {
		}
		complex operator += (const complex&);
		double real () const {
			return re;
		}
		double imag () const {
			return im;
		}
	private:
		double re,im;
friend complex&_dopal (complex*,const complex&);
};
ostrean&
operator<< (ostream& os, const complex& x) {
	return os<< '(' <<real (x) <<','
	       << imag (x) <<'(';
}
//尽量不要 pass by value  引用就是一个指针
//传递引用防止更改所以我们要pass by  reference (to const)
//返回值的传递尽量by reference(to const)
calss complex {
public:
	complex (double r = 0,double i = 0)
		:re (r), im (i);
	{ }
	complex& operator += (const complex&);
	double real () const {return re;}
	double imag () const {return im;}
private:
	double re,im;
	friend complex&_dopal (complex*, const complex);
};
ostream&
operator << (ostream& os, const complex& x) {
	return os << '(' <<real (x) << '.'
	       << imag (x) << ')';
}
//尽量使用reference
class complex {
	public:
		complex (double r = 0,double i = 0)
			:re(r),im(i);
		{ }
		complex& operator += (const complex&);
		double real () const {
			return re;
		}
		double imag () const {
			return im;
		}
	private:
		double re,im;
		friend complex&_doapl (complex*, const complex);
};
inline complex&
_doapl (complex*,const complex& r) {
	this->re += r.re;
	this->im += r.im;
	return *this;
}
//friend(友元)因为我们是朋友所以可以直接取得private里面的re  im
//private不让外界取得re  im
class complex {
	public:
		complex (double r = 0,double i = 0)
			:re(r),im(i) {
		}
		int func(const complex& param) {
			return param.re + param.im;
		}
	private:
		double re,im;
};
{
	complex c1(2,1);
	complex c2;
c2.func(c1);
}
//相同的class的各个objects互为friends(友元)
//class body 外的各种定义
//do assignment plus
inline complex&
_doapl(complex* ths, const complex& r) {
	ths->re += r.re;//第一参数将会被改动
	ths->im += r.im;//第二参数将不会被改动
	return *ths;
}
inline complex&
complex::operator += (const complex& r) {
	return _doapl (this, s);
}
///什么情况下不可以使用by reference
//c1+c2的时候+完的数值会被改变
//操作符重载—1,成员函数
//所有的成员函数都会带有一个隐藏的参数 this
//不可以在参数列写出来
inline complex&
_doapl(complex* ths,const complex& r) {
	ths->re += r.re;
	ths->im += r.im;
	return *ths;
}
inline complex&
complex::operator +=(const complex& r) {
	return _doapl (this,r);
}
{
	complex c1(2,1);
	complex c2(5);
	c2 +=c1;
	//??c3 += c2 += c1;
	//要考虑到操作符重载,在设计函数的时候要考虑到问号里连续使用的状况
	//连串使用的时候
}
//global函数
//class body之外的各种定义
inline double
imag(const complex& x) {
	return x.imag ();
}
inline double
real(const complex& x) {
	return x.real ();
}
{
	complex c1(2,1);
	cout << imag(c1);
	cout << real(c1);
}
//operator overloading(操作符重载-2,非成员函数
inline complex
operator + (const complex& x, const complex& y) {
	return complex (real (x) + real (y),
	                imag (x) + imag (y));
}
inline complex
operator + (const complex& x,double y) {
	return complex (real (x) + y,imag (x));
}
inline complex
operator + (double x,const complex& y) {
	return complex (x + real (y), imag (y));
}
--------->>>>> {
	complex c1(2,1);
	complex c2;
	c2 = c1 + c2;
	c2 = c1 + 5;
	c2 = 7 + c1;
}
//上面的这些函数绝对不可以使用return by reference,
//因为,它们返回的必定是个local object
//也就是说加过的东西没有放的地方,(x + real (y), imag (y))
//typename();
//生命短暂   temp object  临时对象   return complex ();
//构造函数是有默认值的0
------------->>>>>>
inline complex
operator + (const complex& x)
{
	return x;
}
inline complex
operator - (const complex& x)
{
	return complex (-real (x), -imag (x););
}
{
	complex c1 (2,1);
	complex c2;
	cout << -c1;
	cout << +c1; 
} 
//正号和负号,只有一个参数的前面有+-号
// return complex (-real (x), -imag (x););
//临时对象
//上面的那句是正号没有变化,标准库里的,可以把它reference
--------->>>>>
inline bool
operator == (const complex& x,
			 const complex& y)
{
	return real (x) == real (y)
        && imag (x) == imag (y);
}
inline bool
operator == ( const complex& x,double y)
{
	return real (x) == y && imag (x) ==0
}
inline bool
operator == (double x, const complex& y)
{
	return x == real (y) &&&imag (y) ==0;
}
{
	complex c1(2,1);
	complex c2;
	cout << (c1 ==c2);
	cout << (c1 == 2);
	cout << (0 == c2);
}
----------->>
inline complex
conj (const complex& x)
{
	return complex (real (x),-imag (x));
 } 
#include<iostream.h>
ostream&
operator << (ostream& os,const complex& x)
{
	return os << '(' << real (x) << ','
			  << imag (x) << ')'; 
}
{
	complex c1(2,1);
	cout << conj(c1);
	cout << c1 << conj(c1);
}
//共轭复数
侯捷C++学习(一)的更多相关文章
- 侯捷STL学习(12)--STL相关内容hash+tuple
		layout: post title: 侯捷STL学习(12) date: 2017-08-01 tag: 侯捷STL --- 第四讲 STL相关的内容 Hash Function 将hash函数封装 ... 
- 侯捷STL学习(11)--算仿+仿函数+适配器
		layout: post title: 侯捷STL学习(十一) date: 2017-07-24 tag: 侯捷STL --- 第三讲 标准库内核分析-算法 标准库算法形式 iterator分类 不同 ... 
- 侯捷STL学习(十)--容器hashtable探索(unordered set/map)
		layout: post title: 侯捷STL学习(十) date: 2017-07-23 tag: 侯捷STL --- 第二十三节 容器hashtable探索 hashtable冲突(碰撞)处理 ... 
- 侯捷STL学习(九)--关联式容器(Rb_tree,set,map)
		layout: post title: 侯捷STL学习(九) date: 2017-07-21 tag: 侯捷STL --- 第十九节 容器rb_tree Red-Black tree是自平衡二叉搜索 ... 
- 侯捷STL学习(八)-- 深度探索deque
		layout: post title: 侯捷STL学习(八) date: 2017-07-19 tag: 侯捷STL --- 第十八节 深度探索deque上 duque内存结构 分段连续,用户看起来是 ... 
- 侯捷STL学习(七)--深度探索vector&&array
		layout: post title: 侯捷STL学习(七) date: 2017-06-13 tag: 侯捷STL --- 第十六节 深度探索vector vector源码剖析 vector内存2倍 ... 
- 侯捷STL学习(一)
		开始跟着<STL源码剖析>的作者侯捷真人视频,学习STL,了解STL背后的真实故事! 视频链接:侯捷STL 还有很大其他视频需要的留言 第一节:STL版本和重要资源 STL和标准库的区别 ... 
- 侯捷STL学习(一)--顺序容器测试
		开始跟着<STL源码剖析>的作者侯捷真人视频,学习STL,了解STL背后的真实故事! 视频链接:侯捷STL 还有很大其他视频需要的留言 第一节:STL版本和重要资源 STL和标准库的区别 ... 
- 侯捷STL学习(四)--allocator和容器时间的实现关系
		第十一节 分配器 分配器的好坏影响到容器的性能 operator new()里面调用malloc D:\Program Files (x86)\Microsoft Visual Studio 12.0 ... 
- 侯捷STL学习(五)--allocator和容器之间的实现关系
		第十一节 分配器 STL源码学习----内存管理 分配器的好坏影响到容器的性能 operator new()里面调用malloc D:\Program Files (x86)\Microsoft Vi ... 
随机推荐
- 计算机基础- 序列化(Serialization)和持久化(Persistence)的区别
			参考 https://en.wikipedia.org/wiki/Serialization https://en.wikipedia.org/wiki/Persistence_(computer_s ... 
- Ubuntu中安装pycharm
			1.首先在官网上下载pycharm 2.下载完成后解压,进入到解压文件夹里的bin,找到pycharm.sh 3.使用终端启动:sh pycharm.sh 4.关于破解: 通过命令打开hosts:cd ... 
- linear-gradient()的用法
			linear-gradient() 函数用于创建一个线性渐变的 "图像" 它的语法是 background: linear-gradient(direction, color-st ... 
- 【原】简单shell练习(四)
			1.查看已开启端口信息 #ss -ln 2.列出谁在使用某个端口(如:80) #lsof -i:80 3.显示文件夹下文件信息 #find /home/root -type f#find -type ... 
- Duilib程序添加托盘图标显示
			转载:https://www.zhaokeli.com/article/8266.html 温馨提示:技术类文章有它的时效性,请留意文章更新时间以及软件的版本 功能描述 实现点击关闭后,程序最小化到托 ... 
- Linux命令:vmstat命令
			vmstat:虚拟内存状态查看命令 命令选项 vmstat 1 #每秒钟刷新1次 vmstat 1 3 #每秒刷新1次,只刷3次 vmstat -s #显示内存 ... 
- IDEA中打war 包
			把war 放入tomcat 的 webapps 目录下,重启tomcat即可自动解压war包跑起来 (最好是,解压完成后,关闭tomcat,把war包拷贝出去,再启动,不然每次启动tomcat都会自动 ... 
- 「Luogu4556」Vani有约会-雨天的尾巴
			「Luogu4556」Vani有约会-雨天的尾巴 传送门 很显然可以考虑树上差分+桶,每次更新一条链就是把这条链上的点在桶对应位置打上 \(1\) 的标记, 最后对每个点取桶中非零值的位置作为答案即可 ... 
- EMR 配置纪录(不断更新)
			日志路径 -> /var/log(软连接到 /mnt/var/log) 在 spark master 机器中,配置的路径为 /usr/lib/spark/conf /usr/lib/hadoop ... 
- Python 爬取 北京市政府首都之窗信件列表-[信息展示]
			日期:2020.01.25 博客期:133 星期六 [代码说明,如果要使用此页代码,必须在本博客页面评论区给予说明] //博客总体说明 1.准备工作 2.爬取工作 3.数据处理 4.信息展示(本期博客 ... 
