C ++ function restrained, constructors, destructors, shallow copy

inline

// inline functions can declare and implement, but it must be in the same file
// inline functions can not be divided into header and implementation files


int the Add inline (X int, int Y)
{// Do not put Usually loop
  return X + Y;
}

// If it is a custom class type, must capitalize the first letter of
class Computer
{
public: // public members can access outside the class
// public members of the class known as the external interface, features, services

  // member function using the hump mode named
  void setBrand (const char * Brand)
  {

     strcpy(_brand, brand);
  }

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

// protected: // protected members can not be accessed outside the class
  void Print ()
  {
    COUT << "Brand:" << endl << _brand
      << ". price:" << endl << _price;
  }

// private member as much as possible into the bottom of the class
private: // private member can not be accessed outside the class
  char _brand [20 is]; // brand_ / m_brand
  Double _price;
};


Declarations and definitions of class

 

// class declaration
class Computer
{  

// class of default access is private

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

// keyword performance package through private properties
private: // class object space occupied by only the data related to members,
the program code and does not occupy space object // member function storage


  char _brand[20];
  double _price;
};

// class implementation
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;
}


Constructors destructor

 

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

  // destroyed in the process object will automatically call the destructor
  @
  @: Implementation of the destructor object is destroyed

  #if 0
  void Release ()
  {
    Delete [] _brand;
  }
  #endif

  Computer ~ ()
  {   // destructor function: the object is to reclaim the resource request
    IF (_brand) {
      Delete [] _brand;
      _brand = nullptr a; // 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();

  Destruction // heap space object, you need to manually perform
  Computer * = new new PC3 Computer ( "Thinkpad", 7777);
  cout << ">> PC3:" << endl;
  PC3-> Print ();

  delete pc3; // Do not forget, during the execution delete an expression, it will call the destructor

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

  return 0;
}

test1 void ()
{
  Computer * Computer PC3 new new = ( "Thinkpad", 7777);
  COUT << ">> PC3:" << endl;
  PC3-> Print ();
  PC3-> ~ Computer (); // this after the statement is executed, the object is not destroyed in
  the Delete PC3;
}

test2 void ()
{
  Computer PC1 ( "MateBook", 6666);
  COUT << ">> PC1:" << endl;
  pc1.print ();
  PC1 ~ Computer (); // destructor active call. but this is not recommended
  // destructor should be allowed to automatically perform
  //pc1.release ();
}


 Copy constructor


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

// system provides not meet the needs
  #if 0
  Computer (const Computer & RHS)
     : _brand (rhs._brand) // shallow copy, only the transfer address
    , _price (rhs._price)
  {
    COUT << "Computer (const & Computer ) "<< endl;
  }
  #endif
   Computer (const Computer & RHS)
    : _brand (new new char [strlen (rhs._brand) +. 1] ())
    , _price (rhs._price)
  {
     strcpy (_brand, rhs._brand) ;
    COUT << "Computer (Computer const &)" << 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();

  // initialize the object with a new object already exists another
  Computer PC1 PC2 =;
  COUT << ">> PC2:" << endl;
  pc2.print ();
}

 

Guess you like

Origin www.cnblogs.com/Davirain/p/11770257.html