C++视频课笔记(一)

在百度网盘上找到的c++视频资源,感觉讲的非常棒,做一下笔记

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
template<typename T>         
class complex // class head
{ // class body
public:
complex (T r=0, T i=0) //
: re(r), im(i) // initialization list 初值列
{ } // 上述三行为构造函数
//complex () :re(0),im(0) {} // 第二个构造函数,但是这个构造函数和上面的构造函数冲突了
complex& operator += (const complex&); // 声明
T real () const { return re; } // 内部定义的inline函数
T imag () const { return im; } // 内部定义的inline函数
private:
T re, im;
friend complex& __doapl (complex*,const complex&)
}
  1. inline(内联)函数
    直接在类内(class body内)进行定义的函数,一般来说执行效率更高,但是具体能否编译为inline函数,由编译器决定。(简单的区分是如果函数过于复杂,编译器也无法将其编译为inline)

    1
    2
    3
    4
    5
    inline double
    imag(const complex& x)
    {
    return x.imag();
    }

inline关键字可以建议编译器将类外的函数编译成inline函数。

  1. access level(访问级别)
    public、private限制了是否可以从外部直接访问,一般来说数据都要是private,通过public中的函数来进行获取。private中定义的函数是外部无法访问的
1
2
3
4
5
6
constructor (ctor 构造函数)
{
complex c1(2,1);
complex c2;
complex* p = new complex(4);
}

构造函数名称一定要和类的名称相同
构造函数可以有参数
构造函数的参数可以拥有默认值
构造函数没有返回值类型
构造函数可以有多个(overloading 重载)
ps. 函数重载在编译器编译后其实名称也是不同的,所以说从编译器层面来看,确实没有完全同名的函数。
构造函数在特殊情况下也可以放在private区中,比如设计模式中的单例模式,会把构造函数放在private中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Singleton
class A{
public:
static A& getInstance();
setup() {```}
private:
A();
A(const A& rhs);
...
};

A& A::getInstance()
{
static A a;
return a;
}

// 调用时
A::getInstance().setup();

==注意构造函数专业的写法不会在大括号中进行执行赋值操作,而是使用初值列/初始列进行初始化设值。这是构造函数独有的初始化方法。==

1
2
3
4
5
6
7
// 不专业的写法
complex(double r=0,double i=0)
{ re = r; im=i; }
// 专业的写法
complex(double r=0,double i=0)
:re(r),im(i)
{}
  1. const 常量成员函数
    类中的函数可以分为会改变数据内容的和不会改变数据内容的,对于不会改变数据内容的函数,可以加上const进行限制。
    ==参数传递时,拷贝值传递造成堆栈浪费,所以应该尽量避免使用值传递,而是使用引用传递,引用传递速度快也美观。但是为了防止原数据被修改,应当对不想要被更改的变量加const关键字进行限制。==
    ==函数返回时,也要尽量返回引用。==

    1
    2
    3
    4
    5
    6
    ostream&
    operator << (ostream& os,const complex& x)
    {
    return os << '(' << real (x) << ','
    << imag (x) << ')';
    }
  1. friend(友元)

    1
    2
    3
    4
    5
    6
    7
    inline complex&
    __doapl (complex* ths, const complex& r)
    {
    ths->re += r.re;
    ths->im += r.im;
    return *ths;
    }

==相同class的各个objects互为friends(友元)==

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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);
}
  1. class body 外的各种定义(definitions)
    区分清楚哪些情况引用传递,哪些情况返回引用
    ==被改动的参数不能使用引用传递,不会被改动的参数可以使用引用传递。==
    ==函数运算结果需要创建空间存放,此时申请的空间时local的,函数结束会释放,所以不能使用引用返回。==
    ==函数运算结果可以直接放在已有的空间,就看可以返回引用==

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    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);
    }

文章目录