GeekBand​boolanC++ 笔记第一周

来源:互联网 发布:超级优化李玄 编辑:程序博客网 时间:2024/05/21 10:31
2.头文件与类的声明
创建类的写法比如 string 1("hello");
先使用没有指针的类
先写一个防卫式的声明

#ifndef  _complex_
#define _complex_
code..
#endif
3.构造函数
定义在类内的函数自动成为inline函数,类似于宏比较快比较好 最好都写成inline function ,
有些函数即使写成inline 编译器也没有办法编程inline
可以都写成inline 函数

成员函数中 
double real() const{return 0;}
函数如果不改变内部的值那末请使用const ;

访问级别 public  private两个级别

构造函数的创建 比如complex(double r=0,double I =0):re(r) 注意 initiliztaion lilst  C++特殊的用法在大括号里用也是同样可以的但是initilzation和assignments是不同的操作
{}                                                上面给了 default argument / 
上面的构造函数已经有(default argument)所以不再可以有
 complex():re(r){}
构造函数可以有很多个 (overloading)
4.参数传递及返回值
构造函数可以放在private内部
只打算用一份
成员常量函数 函数的小括号后面加 const
不会改变数据的函数加上const
const  complex(1,2)
这样定义的comlex就会报错

pass by value pass by reference
尽量使用pass by reference
comlex& operator+=(const complex& b)
尽量 return by reference


友元friend
 friend complex& __doap1(complex*,const comlex&)
友元的使用方法
在友元函数内部可以直接使用private

相同的 class 的各个objects 互为 friends;
比如 该函数在comlex内 而er mi 均为private内的数据
int func(const comlex& parm)
{
return param.re+param.im;
}

正确的引用返回:
inline complex& __doapl(complex* ths ,const complex& r){
return *ths
}
5.操作符重载与临时对象
传递者无需知道接受端是如何接收的。
非成员函数的重载
inline complex
operator +(const complex&x , double y){
}
temp object 临时对象的使用 不要传出引用
一个参数必定在运算符的右端
两个参数就在两端
<<的这种运算符比较特殊 查标准库吧
1 0
原创粉丝点击