1. 程式人生 > >建構函式、解構函式和拷貝建構函式

建構函式、解構函式和拷貝建構函式

一.建構函式

C++ 中類的建構函式與 java 很類似,其實對於面向物件程式設計來說,大傢伙都是一個思想,可能語法上有所不同而已。

class Student
{
public:
    // 建構函式
    Student(){// 空引數建構函式
        cout << "空引數建構函式"<< endl;
    }

    // Student(char* name):age(0){// 一個引數建構函式, 相當於 this->age = 0
    //  cout << "一個引數建構函式" << endl;
    //  this->name = name;
// this->age = 0; // } Student(char* name) :Student(name,0){// 呼叫兩個引數的建構函式,注意:先會呼叫兩個引數的建構函式,然後才會執行當前建構函式 cout << "一個引數建構函式" << endl; } Student(char* name, int age){// 兩個引數建構函式 cout << "兩個引數建構函式" << endl; this->name = (char*)malloc
(sizeof(char)*100); strcpy(this->name,name); // this->name = name; this->age = age; } private: int age; char* name; } void main(){ // Student stu;// 1. 預設呼叫空參的建構函式 // stu.setAge(24); // stu.setName("Darren"); // Student stu("Darren",24); // 2. 呼叫兩個引數的建構函式
// 3. 用 new 關鍵字,返回的是一個 Student 的一級指標 // Student *stu = new Student("Darren",24); // 4. 用 malloc 的方式,並沒有呼叫空參的建構函式 // Student *stu = (Student*)malloc(sizeof(Student)); // stu->setAge(24); // stu->setName("Darren"); // 建構函式相互呼叫 Student *stu = new Student("Darren"); cout << stu -> getName() << " , " << stu->getAge() << endl; getchar(); }

需要注意的是屬性初始化的時候要用 :屬性名(初始值),建構函式之間需要相互呼叫時用 :建構函式名(引數值),具體請看上面的事例程式碼。

二.解構函式

在 C++ 中當一個物件被回收的時候,會呼叫該物件的解構函式,一般來講我們都會在解構函式中做一些釋放記憶體的操作,當然如果不處理不好,也可能出現宕機的情況。這在 java 中似乎很少見,因為 java 都是由 JVM 自動管理記憶體的,但其實 java 類也有一個解構函式。在騰訊面試的時候就遇到了這麼一個筆試題:請說出 finalize,finally,final 之間的區別。

// 2. 解構函式,如果有在物件內部開闢堆記憶體,可以在解構函式中釋放記憶體
~Student(){
  cout << "解構函式" << endl;
  // 臨終遺言,物件被回收的時候會被呼叫
  // 釋放記憶體
  free(this->name);
  this->name = NULL;
}

void main(){
    Student *stu = new Student();
    delete(stu);
    getchar();
}
二.拷貝建構函式

在 java 中如果 Student stu2 = stu1,那麼我們一般會認為 stu2 物件和 stu1 物件是同一個物件,但是在 c++ 中這種認為就是錯誤的,我們可以分別列印 stu1 和 stu2 的地址發現並不相等,所以 stu1 和 stu2 並不是同一個物件,而是會呼叫拷貝建構函式。

// 4.拷貝建構函式,物件會有一個預設的拷貝建構函式,用來物件之間的賦值
Student(const Student& stu){// 常量的引用
  cout << "拷貝建構函式" << endl;
  // this->name = stu.name;// 淺拷貝
  // 如果動態開闢記憶體,一定要採用深拷貝
  this->name = (char*)malloc(sizeof(char)* 100);
  strcpy(this->name, stu.name);
  this->age = stu.age;
}

Student getStudent(char* name){
    Student stu(name);// 棧 ,方法執行完,這個物件會被回收,但是發現呼叫了拷貝建構函式
    cout << &stu << endl;
    return stu;// 會返回一個新的 Student 物件,而棧內開闢的 stu 是會被回收
}


void printStudent(Student stu){// stu 是該方法棧中一個新的物件,拷貝建構函式賦值,方法執行完會呼叫解構函式
    cout << stu.getName() << " , " << stu.getAge() << endl;
}

void main(){
    // 1. = 會呼叫拷貝建構函式
    // Student stu1("Darren", 24);
    // Student stu2 = stu1; // = 是賦值,把裡面所有定義的屬性賦值,c/c++ 編譯器幫我們做的,其實會呼叫物件的拷貝構造

    // Student stu2;// 宣告變數,開闢變數記憶體
    // stu2 = stu1; // 這個不會去呼叫拷貝建構函式,但是會賦值 c 的類似

    // 2. 第二種場景 作為引數返回的時候會呼叫拷貝建構函式
    // Student stu = getStudent("Jack");
    // cout << &stu << endl;

    // 3. 第三種場景 作為引數傳遞的時候會呼叫拷貝建構函式
    // Student stu("Darren", 24);
    // printStudent(stu);

    // 知識的補充
    Student stu = getStudent("Jack");

    // cout << stu.getName() << " , " << stu.getAge() << endl;

    printStudent(stu);

    getchar();
}

相關推薦

編寫類String的建構函式拷貝建構函式函式賦值函式

class String { public: String(const char *str = NULL); // 普通建構函式 String(const String &other); // 拷貝建構函式 ~String(void);

編寫類String 的建構函式拷貝建構函式函式賦值函式

 編寫類String 的建構函式、解構函式和賦值函式,已知類String 的原型為: class String { public:String(const char *str = NULL); // 普通建構函式String(const String &other

建構函式函式拷貝建構函式

一.建構函式 C++ 中類的建構函式與 java 很類似,其實對於面向物件程式設計來說,大傢伙都是一個思想,可能語法上有所不同而已。 class Student { public: // 建構函式 Student(){// 空引數建構函式

類String的建構函式拷貝建構函式函式賦值運算子過載函式的實現

#include <iostream> using namespace std; class String { public: String(const char* str= NULL); String(const String& other); ~

找工作筆試面試那些事兒(5)---建構函式函式賦值函式

作者:寒小陽 時間:2013年9月。 出處:http://blog.csdn.net/han_xiaoyang/article/details/10833931。 宣告:版權所有,轉載請註明出處,謝謝。 類的建構函式、解構函式與賦值函式     &

請為CMyString型別編寫建構函式copy建構函式函式賦值運算子函式

如下為型別CMyString的宣告,請為該型別編寫建構函式、copy建構函式、解構函式和賦值運算子函式。 1 class CMyString 2 { 3 public: 4 CMyString(const char* pData = nullptr); 5 CMyS

物件——建構函式函式複製建構函式

類: 要宣告類,用class,注意區分大小寫。類含有成員屬性和方法。在宣告類的末尾要加分號; 物件: 宣告完類之後,它並不能對程式產生任何影響。只有依據類,例項化物件,才可以訪問類成員屬性和方法。 下面是類例項化物件、動態分配記憶體與變數的對比: double pi=3.14; int* p

【C++筆記】編寫類string的建構函式函式賦值函式

#include<iostream> using namespace std; class String { public: String(const char *str=NULL); //普通建構函式 String(const Stri

類的建構函式函式拷貝建構函式賦值函式

 類的四種基本預設函式:建構函式、解構函式、拷貝建構函式、賦值函式。 建構函式:建立類時,系統自動呼叫建構函式,用以初始化,分配空間,沒有自定義的建構函式時,系統預設一個無引數的建構函式。 class book { private:     int isBook;

建構函式函式變數的生存期

//referred from Wei Guo, Peking University #include<iostream> using namespace std; class Demo{ int id; public: Demo(int i)

C++類中的一些細節(過載重寫覆蓋隱藏,建構函式函式拷貝建構函式賦值函式在繼承時的一些問題)

1 函式的過載、重寫(重定義)、函式覆蓋及隱藏 其實函式過載與函式重寫、函式覆蓋和函式隱藏不是一個層面上的概念。前者是同一個類內,或者同一個函式作用域內,同名不同引數列表的函式之間的關係。而後三者是基類和派生類函式不同情況下的關係。 1.1 函式過載

C/C++面試題:編寫類String的建構函式函式賦值函式

考點:建構函式、解構函式和賦值函式的編寫方法出現頻率:☆☆☆☆☆已知類String的原型為:        class String        {        public:                String(const char *str = NULL);

C++第十週【任務2】定義一個名為CPerson的類,有以下私有成員:姓名身份證號性別年齡,成員函式建構函式函式輸出資訊的函式

/* (程式頭部註釋開始) * 程式的版權和版本宣告部分 * Copyright (c) 2011, 煙臺大學計算機學院學生 * All rights reserved. * 檔名稱: C++第十週【任務2】 * 作

編寫類String的建構函式函式賦值函式

//普通建構函式  String::String(const char *str)  {          if(str==NULL)          {                  m_data = new char[1]; // 得分點:對空字串自動申請存放結束

讀書筆記:實現string類的建構函式拷貝建構函式函式過載運算子=

#include <iostream> #include <cassert> #include <cstring> using namespace std; class MyString{ public: MyString(co

C++ 建構函式預設建構函式函式物件初始化

#include <iostream> using namespace std; class Student{     private:         int m_age;         int m_grade;         string m_sex

編寫String類的普通建構函式拷貝建構函式函式賦值函式

題目:編寫類String的建構函式、解構函式和賦值函式,已知類String的原型為: class String {  public:   String(const char *str = NULL); // 普通建構函式   String(const String

關於c++預設的建構函式函式拷貝建構函式move函式

在c++中,當我們定義一個類的時候,如果我們什麼都不定義的時候,c++編譯器會預設的為我們生成一些函式。 例如定義一個Example類。 class Example{ }; 當我們定義一個Example類的時候,不定義任何操作的時候,c++編譯系統將為Example類生成如

c++編寫類String的建構函式函式賦值函式

C++程式碼  按 Ctrl+C 複製程式碼 class String { public: String(const char *str = NULL);// 普通建構函式 String(const String &other);

建構函式函式拷貝建構函式小結

        建構函式的定義與使用 建構函式是特殊的公有成員函式,其特徵如下: 1.函式名與類名相同。 2.建構函式無函式返回型別說明。注意是沒有而不是void,即什麼也不寫,也