簡體   English   中英

需要幫助理解我的程序中的構造函數(c++)

[英]Need help understanding the constructor in my program (c++)

好吧,我對 C++ 編程很陌生。 我已經為大學做了大約 2 個月了。 我給我的教授發了一份我的一個程序的副本,以幫助我理解它的不同部分。 他告訴我我提交的程序需要一個構造函數。 話雖如此,我對構造函數的了解是它們必須與程序中的 class 同名。 所以任何人都可以向我解釋我的構造函數在我的程序中是什么。 謝謝

    #include <iostream>  
    #include <string>    

    using namespace std;  
    class Vertebrae {     //here i am declaring my class as Vertebrae
    private:          //here i am setting a private access modifier
      string brain;     //creating the string brain as a private attribute
      string backbone;   //creating the string backbone as a private attribute
      string skeleton;  //creating the string skeleton as a private attribute
    
    public:     //setting my public access modifier
    
    
    void setBrain(string a) {
        brain = a;
    }
    string getBrain() {
        return brain;
    }
    void setBackbone(string b) {  
        backbone = b;
    }
    string getBackbone(){
        return backbone;
    }
    void setSkeleton(string c) { 
        skeleton = c;
    }
    string getSkeleton() {
        return skeleton;
    
    }
  };

  int main(){
  Vertebrae A;   
  Vertebrae B;  
  Vertebrae C;  
  A.setBrain("Brains");  
  B.setBackbone("Spines"); 
  C.setSkeleton("Boney Skeletons");  
  cout << "Vertebrates have " << A.getBrain();  //outputting the first definition of a vertebrae
  cout << ", ";  //outputting a comma
  cout << B.getBackbone(); //outputting the second definition of a vertebrae
  cout << ", and "; //outputting another comma and the word and
  cout  << C.getSkeleton();  //outputting the third definition of a vertebrae
  return 0;
 }

構造函數是 class 的一種特殊方法,它在您創建 object 時被調用。 除非您的構造函數被稱為 object,否則創建將是不完整的。

如果您尚未創建 Class 將有一個默認構造函數。

構造函數是初始化 class 對象的 class 的成員 function。 在 C++ 中,構造函數在創建對象(類的實例)時自動調用。 它是 class 的特殊成員 function。 構造函數與普通成員 function 有何不同?

構造函數在以下方面不同於普通函數:

  • 構造函數與 class 本身同名
  • 構造函數沒有返回類型
  • 創建 object 時會自動調用構造函數。
  • 如果我們不指定構造函數,C++ 編譯器會為我們生成一個默認構造函數(不需要參數並且有一個空的主體)。

此外,在 class 的構造函數的定義中,成員初始化器列表指定了直接和虛擬基以及非靜態數據成員的初始化器。 (不要與 std::initializer_list 混淆。)

默認構造函數沒有任何參數,但如果需要,構造函數可以有參數。 這有助於您在創建 object 時為其分配初始值。

還有復制構造函數,但我敢肯定,正如@NathanOliver 所說,有了一本好的 C++ 書,您將能夠在此和其他事情上更深入地了解 go。 有一種特殊類型的構造函數,它以 object 作為參數,用於將一個 object 的數據成員的值復制到另一個 object 中。

#include <iostream>
#include <string>

class Vertebrae {               // here i am declaring my class as Vertebrae
private:                        // here i am setting a private access modifier
    std::string brain;         // creating the string brain as a private attribute
    std::string backbone;      // creating the string backbone as a private attribute
    std::string skeleton;      // creating the string skeleton as a private attribute
    
public:     //setting my public access modifier
    
    void setBrain(std::string a) {
        brain = a;
    }
    
    std::string getBrain() {
        return brain;
    }
    
    void setBackbone(std::string b) {
        backbone = b;
    }
    
    std::string getBackbone(){
        return backbone;
    }
    
    void setSkeleton(std::string c) {
        skeleton = c;
    }
    
    std::string getSkeleton() {
        return skeleton;
    }
    
    // Default Constructor Example
    Vertebrae() {
        // Object initialization and other stuff eventually
    }
    
    // Parametrized Constructor Example
    Vertebrae(std::string brain, std::string backbone, std::string skeleton) {
        // Or use setMethod(...)
        this->brain = brain;
        this->backbone = backbone;
        this->skeleton = skeleton;
    }
    
    /*
    // Constructor with Member Initializer List
    Vertebrae(std::string brain, std::string backbone, std::string skeleton) : brain(brain), backbone(backbone), skeleton(skeleton) {
            // Other stuff eventually only
    }
     
     */
    
};

int main(){
    Vertebrae A;
    Vertebrae B;
    Vertebrae C;
    A.setBrain("Brains");
    B.setBackbone("Spines");
    C.setSkeleton("Boney Skeletons");
    
    std::cout << "Vertebrates have " << A.getBrain();  //outputting the first definition of a vertebrae
    std::cout << ", ";  //outputting a comma
    std::cout << B.getBackbone(); //outputting the second definition of a vertebrae
    std::cout << ", and "; //outputting another comma and the word and
    std::cout  << C.getSkeleton();  //outputting the third definition of a vertebrae
    std::cout << "\n";
    return 0;
}

只是一些提示:

就這樣。 我希望這會有所幫助。 干杯,丹尼

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM