C++面向对象高级编程学习(1)

C++内联函数与构造函数详解
本文详细介绍了C++中的内联函数概念及其使用方法,并解释了构造函数的不同写法及其作用。同时,文章还涉及了常量成员函数、参数传递、友元等高级特性。

目录

一、学习网站

二、inline内联函数

    1、基本定义

2、基本使用

    3、内联函数关键知识点

三、构造函数初始化

四、常量成员函数

五、参数传递和返回值使用const引用

六、友元

七、完整代码如下


一、学习网站

        B站免费学习学习,网盘资料视频地址链接:https://pan.baidu.com/s/1azfa1N5DN6fimspqU1mpsw  密码:kut5

二、inline内联函数

    1、基本定义

                inline是C++语言中比较重要的一个关键字,可以用于程序中定义内联函数,inline的引进使内联函数的定义更加简单,说到内联

        函数,这里给出比较常见的定义,内联函数是C++中的一种特殊函数,它可以像普通函数一样被调用,但是在调用时并不通过函数

        调用的机制,而是通过将函数体直接插入调用处来实现的,这样可以大大减少由函数调用带来的开销,从而提高程序的运行效率。

        一般来说,inline用于定义类的成员函数。

2、基本使用

                (1)类内定义成员函数

                        这种情况下,不用在函数头部加inline关键字,因为编译器会自动将类内定义的函数申明为内联函数,代码如下:

class temp{
  public:
     int  amount;

  //构造函数
   temp(int amount){
      this->amount = amount;
   }

  //普通成员函数,在类内定义时前面可以不加inline
  void print_amount(){
    cout << this-> amount;
  }
}

            (2)类内申明函数,在类外定义函数

                    根据C++编译器的规则,这种情况下如果想把该函数设置为内联函数,则可以在类内申明时不加inline关键字,而在类外定义

                函数时加上inline关键字,代码如下:

class temp{
  public:
     int  amount;

  //构造函数
   temp(int amount){
      this->amount = amount;
   }

  //普通成员函数,在类内声明时前面可以不加inline
  void print_amount()
}

//在类外定义函数体,必须在前面加上inline关键字
inline void temp:: print_amount(){
  cout << amount << endl;
}

    3、内联函数关键知识点

        (1)inline关键字用来定义一个类的内联函数,引入它的主要原因是用它代替C中表达式形式的宏定义,解决一些频繁调用的小函数

        大量消耗栈空间(栈内存)的问题。

        (2)inline的使用是有所限制的,inline只适合函数体内代码简单的函数使用,不能包含复杂的结构控制语句,例如while、switch,

        并且不能内联函数本身,不能是直接递归函数(即,自己内部还调用自己的函数)。

        (3)inline函数仅仅是一个对编译器的建议,所以最后能否真正的内联,取决于编译器,申明内联只是一个建议。

        (4)定义在类中的成员函数缺省都是内联的,在类外想内联该函数,就在类外加上inline,如2、基本使用示例。

三、构造函数初始化

    相对于C语言来说,C++有一个比较好的特性就是构造函数,即类通过一个或者几个特殊的成员函数来控制其对象的初始化过程。构造函数的任务,就是初始化对象的数据成员,无论何时只要类的对象被创建,就会执行构造函数。构造函数的名字必须和类名相同,与其他函数不一样的是,构造函数没有返回值,而且其必须是公有成员,因为私有成员不允许外部访问,且函数不能声明为const类型。

    构造函数有两种写法,代码如下:

class complex
{
public:
  //第一种写法
  complex (double r = 0, double i = 0): re (r), im (i) { }

  //第二种写法
  complex (double r = 0, double i = 0){
    re = r;
    im = i;
  }

private:
  double re, im;
};

    第一种写法叫做初始化列表构造函数。重点为初始化。建议使用这种方式。

    第二种写法叫做拷贝构造函数。重点为赋值。

    当构造函数重载时,黄色标注的构造函数定义将出现问题,如果该函数与上面构造函数同时出现,在无参初始化该类对象时将产生冲突,因为第一个构造函数已经有参数默认初始化列表了,定义该类对象时可以不加入参数,这就产生了冲突。

四、常量成员函数

    在一个类中,如果成员函数中没有改变成员函数操作(例如get操作),那么建议在该方法声明处加入const关键字,如果不加入const关键字,那么c++编译器将认为该函数可能会修改类的成员变量。这样做有个好处,可以看到上图的"?!"一图,使用者利用了const关键字定义并通过构造函数初始化了一个complex类,这个类将不能被修改,只能读取属性。当使用者调用complex的real()或者imag()方法时,如果这两个方法在定义处没有加入const关键字,那么将报错。

class complex
{
public:
  complex (double r = 0, double i = 0): re (r), im (i) { }

  double real () const { return re; }
  double imag () const { return im; }
private:
  double re, im;

};

五、参数传递和返回值使用const引用

    函数参数传递,如果不需要改变参数值,建议使用const reference减小开销。

六、友元

    相同类的各个实例对象互为友元,可以通过彼此的内部方法调用传入参数的内部私有成员变量。

七、完整代码如下

#ifndef __MYCOMPLEX__
#define __MYCOMPLEX__

class complex; 
complex&
  __doapl (complex* ths, const complex& r);
complex&
  __doami (complex* ths, const complex& r);
complex&
  __doaml (complex* ths, const complex& r);


class complex
{
public:
  complex (double r = 0, double i = 0): re (r), im (i) { }
  complex& operator += (const complex&);
  complex& operator -= (const complex&);
  complex& operator *= (const complex&);
  complex& operator /= (const complex&);
  double real () const { return re; }
  double imag () const { return im; }
private:
  double re, im;

  friend complex& __doapl (complex *, const complex&);
  friend complex& __doami (complex *, const complex&);
  friend complex& __doaml (complex *, const complex&);
};


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);
}

inline complex&
__doami (complex* ths, const complex& r)
{
  ths->re -= r.re;
  ths->im -= r.im;
  return *ths;
}
 
inline complex&
complex::operator -= (const complex& r)
{
  return __doami (this, r);
}
 
inline complex&
__doaml (complex* ths, const complex& r)
{
  double f = ths->re * r.re - ths->im * r.im;
  ths->im = ths->re * r.im + ths->im * r.re;
  ths->re = f;
  return *ths;
}

inline complex&
complex::operator *= (const complex& r)
{
  return __doaml (this, r);
}
 
inline double
imag (const complex& x)
{
  return x.imag ();
}

inline double
real (const complex& x)
{
  return x.real ();
}

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));
}

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));
}

inline complex
operator * (const complex& x, const complex& y)
{
  return complex (real (x) * real (y) - imag (x) * imag (y),
			   real (x) * imag (y) + imag (x) * real (y));
}

inline complex
operator * (const complex& x, double y)
{
  return complex (real (x) * y, imag (x) * y);
}

inline complex
operator * (double x, const complex& y)
{
  return complex (x * real (y), x * imag (y));
}

complex
operator / (const complex& x, double y)
{
  return complex (real (x) / y, imag (x) / y);
}

inline complex
operator + (const complex& x)
{
  return x;
}

inline complex
operator - (const complex& x)
{
  return complex (-real (x), -imag (x));
}

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;
}

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;
}

#include <cmath>

inline complex
polar (double r, double t)
{
  return complex (r * cos (t), r * sin (t));
}

inline complex
conj (const complex& x) 
{
  return complex (real (x), -imag (x));
}

inline double
norm (const complex& x)
{
  return real (x) * real (x) + imag (x) * imag (x);
}

#endif   //__MYCOMPLEX__




 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值