需要帮助理解我的程序(c++)中的构造函数



好了,大家好,我是c++编程新手。到现在为止,我已经在大学里做了两个月了。我给我的教授寄了一份我的程序的副本,以帮助我理解它的不同部分。他告诉我,我提交的程序需要一个构造函数。我对构造函数的了解是它们必须与程序中的类具有相同的名称。谁能给我解释一下程序中的构造函数是什么。谢谢你

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

构造函数是类的成员函数,用于初始化类的对象。在c++中,创建对象(类的实例)时自动调用构造函数。它是类的一个特殊成员函数。构造函数与普通成员函数有何不同?

构造函数与普通函数的区别如下:

  • 构造函数与类本身同名
  • 构造函数没有返回类型
  • 创建对象时自动调用构造函数
  • 如果不指定构造函数,c++编译器会为我们生成一个默认构造函数(不需要参数,并且有一个空体)。

此外,在类的构造函数定义中,成员初始化项列表指定了直接基类、虚基类和非静态数据成员的初始化项。(不要与std::initializer_list混淆)

默认构造函数没有任何参数,但如果需要,构造函数可以有参数。这可以帮助您在对象创建时为其分配初始值。

也有复制构造函数,但我相信有一本好的c++书,就像@NathanOliver说的,你将能够更深入地了解这个和其他事情。有一种特殊类型的构造函数,它接受一个对象作为参数,用于将一个对象的数据成员的值复制到另一个对象中。

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

只是一些提示:

  • 注意可以在类外访问的私有和公共方法(我说的是你的"setter")。方法)。
  • 使用std而不指定"使用命名空间"(查看这里:为什么"使用命名空间std;"被认为是不好的做法?

。我希望这对你有所帮助。欢呼,丹尼

Constructor是类的一个特殊方法,在创建对象时调用。除非调用构造函数,否则对象的创建将是不完整的。

如果您没有创建一个默认构造函数,则该类将有一个默认构造函数。

最新更新