Loading

C++内敛函数,构造函数,析构函数,浅拷贝

inline

//inline函数可以有声明和实现,但是必须在同一文件
//inline函数不能分成头文件和实现文件


inline int add(int x, int y)
{   //一般不要放循环语句
  return x + y;
}

//如果是自定义类类型,都要大写首字母
class Computer
{
public://public成员可以在类之外访问
//public成员称为类对外的接口、功能、服务

  //对成员函数采用驼峰方式进行命名
  void setBrand(const char * brand)
  {

     strcpy(_brand, brand);
  }

  void setPrice(double price)
  {
    _price = price;
  }

//protected://保护成员不能在类之外访问
  void print()
  {
    cout << "brand:" << _brand << endl
      << "price:" << _price << endl;
  }

//私有成员尽量放到类的底部
private://私有的成员不能在类之外访问
  char _brand[20];//brand_ / m_brand
  double _price;
};


类的声明和定义

 

//类的声明
class Computer
{  

//class的默认访问权限是private的

public:
  void setBrand(const char * brand);
  void setPrice(double price);
  void print();

//通过private关键字表现封装的特性
private://类对象占据的空间只与数据成员有关,
//成员函数存储在程序代码区, 不会占据对象的空间


  char _brand[20];
  double _price;
};

//类的实现
void Computer::setBrand(const char * brand)
{
  strcpy(_brand, brand);
}

void Computer::setPrice(double price)
{
  _price = price;
}

void Computer::print()
{
  cout << "brand:" << _brand << endl
    << "price:" << _price << endl;
}


构造函数析构函数

 

class Computer
{
public:
  Computer(const char * brand, double price)
  : _brand(new char[strlen(brand) + 1]()) //浅拷贝, 只传地址
  , _price(price)
  {
    strcpy(_brand, brand);
    cout << "Computer(const char *, double)" << endl;
  }

  void print()
  {
    cout << " brand:" << _brand << endl
      << " price:" << _price << endl;
  }

  //对象销毁的过程中会自动调用析构函数
  //
  //问题: 执行了析构函数就是销毁了对象

  #if 0
  void release()
  {
    delete [] _brand;
  }
  #endif

  ~Computer()
  {   //析构函数的作用:是用来回收对象申请的资源
    if(_brand) {
      delete [] _brand;
      _brand = nullptr;//NULL
    }
    cout << "~Computer()" << endl;
  }

private:
  char * _brand;
  double _price;
};

//Computer pc2("Xiaomi", 7500);

int test0(void)
{
  {
    Computer pc1("MateBook", 6666);
    cout << ">> pc1: " << endl;
    pc1.print();
  }

  cout << ">> pc2: " << endl;
  // pc2.print();

  //堆空间的对象的销毁,需要手动执行
  Computer * pc3 = new Computer("Thinkpad", 7777);
  cout << ">> pc3: " << endl;
  pc3->print();

  delete pc3;//不要忘了, 执行delete表达式的过程中,就会调用析构函数

  static Computer pc4("Macbook pro", 20000);
  cout << ">> pc4: " << endl;
  pc4.print();

  return 0;
}

void test1()
{
  Computer * pc3 = new Computer("Thinkpad", 7777);
  cout << ">> pc3: " << endl;
  pc3->print();
  pc3->~Computer();//该语句被执行之后,对象是没有被销毁的
  delete pc3;
}

void test2()
{
  Computer pc1("MateBook", 6666);
  cout << ">> pc1: " << endl;
  pc1.print();
  pc1.~Computer();//析构函数可以主动调用,但不推荐这样做
  //析构函数应该让其自动执行
  //pc1.release();
}


 拷贝构造函数


class Computer
{
public:
  Computer(const char * brand, double price)
    : _brand(new char[strlen(brand) + 1]())
    , _price(price)
  {
    strcpy(_brand, brand);
    cout << "Computer(const char *, double)" << endl;
  }

//系统提供的 已经不能满足需求
  #if 0
  Computer(const Computer & rhs)
    : _brand(rhs._brand)//浅拷贝, 只传地址
    , _price(rhs._price)
  {
    cout << "Computer(const Computer&)" << endl;
  }
  #endif
  Computer(const Computer & rhs)
    : _brand(new char[strlen(rhs._brand) + 1]())
    , _price(rhs._price)
  {
    strcpy(_brand, rhs._brand);
    cout << "Computer(const Computer&)" << endl;
  }

  void print()
  {
    printf(" brand = %p\n", _brand);
    cout << " brand:" << _brand << endl
      << " price:" << _price << endl;
  }

  ~Computer()
  {
    delete [] _brand;
    cout << "~Computer()" << endl;
  }

private:
  char * _brand;
  double _price;
};

void test0()
{
  Computer pc1("MateBook", 6666);
  cout << ">> pc1: " << endl;
  pc1.print();

  //用一个已经存在的对象初始化另一个新对象
  Computer pc2 = pc1;
  cout << ">> pc2: " << endl;
  pc2.print();
}

 

posted @ 2019-10-31 12:37  RainDavi  阅读(227)  评论(0编辑  收藏  举报