類構(gòu)造函數(shù)是類中的一種特殊類型的函數(shù)。
當定義類的新實例時調(diào)用構(gòu)造函數(shù)。
它在創(chuàng)建新對象時初始化,并確保數(shù)據(jù)成員包含有效值。
類構(gòu)造函數(shù)與類具有相同的名稱。
Box(),例如是Box類的構(gòu)造函數(shù)。
構(gòu)造函數(shù)不返回值,因此沒有返回類型。
如果您沒有為類定義構(gòu)造函數(shù),編譯器將提供默認構(gòu)造函數(shù)。
一般來說,如果Class_name是一個類,如果value的類型為Type_name,則該語句
Class_name * pclass = new Class_name (value);
調(diào)用此構(gòu)造函數(shù):
Class_name (Type_name);
可能有微小的轉(zhuǎn)換,例如:
Class_name(const Type_name &);
實例化MyBook類的多個對象,并使用MyBook構(gòu)造函數(shù)指定每個MyBook對象創(chuàng)建時的課程名稱。
#include <iostream>
#include <string>
using namespace std;
class MyBook {
public:
// constructor initializes courseName with string supplied as argument
MyBook( string name ) {
setCourseName( name ); // call set function to initialize courseName
}
// function to set the course name
void setCourseName( string name )
{
courseName = name; // store the course name in the object
}
// function to get the course name
string getCourseName()
{
return courseName; // return object"s courseName
}
// display a welcome message to the MyBook user
void displayMessage()
{
// call getCourseName to get the courseName
cout << "Welcome to the grade book for\n" << getCourseName()
<< "!" << endl;
}
private:
string courseName; // course name for this MyBook
};
int main() {
// create two MyBook objects
MyBook MyBook1( "C++ Programming" );
MyBook MyBook2( "C++" );
// display initial value of courseName for each MyBook
cout << "MyBook1 created for course: " << MyBook1.getCourseName()
<< "\nMyBook2 created for course: " << MyBook2.getCourseName()
<< endl;
}
上面的代碼生成以下結(jié)果。
復制構(gòu)造函數(shù)的形式對于任何類是相同的:
Type::Type(const Type& object) { // Code to duplicate of object... }
默認構(gòu)造函數(shù)沒有參數(shù),唯一的目的是允許創(chuàng)建一個對象。
class Box { private: double length {1}; double width {1}; double height {1}; public: // The default constructor that is supplied by the compiler... Box() { // Empty body so it does nothing... } // Function to calculate the volume of a box double volume() { return length*width*height; } };
以下代碼為Box類添加構(gòu)造函數(shù)。
#include <iostream>
// Class to represent a box
class Box {
private:
double length {1.0};
double width {1.0};
double height {1.0};
public:
// Constructor
Box(double lengthValue, double widthValue, double heightValue)
{
std::cout << "Box constructor called." << std::endl;
length = lengthValue;
width = widthValue;
height = heightValue;
}
// Function to calculate the volume of a box
double volume()
{
return length*width*height;
}
};
int main()
{
Box firstBox {80.0, 50.0, 40.0}; // Create a box
double firstBoxVolume {firstBox.volume()}; // Calculate the box volume
std::cout << "Volume of Box object is" << firstBoxVolume << std::endl;
}
上面的代碼生成以下結(jié)果。
函數(shù)成員的定義可以放在類定義之外。
類構(gòu)造函數(shù)也是如此。
#include <iostream>
class Box {
private:
double length {1.0};
double width {1.0};
double height {1.0};
public:
// Constructors
Box(double lengthValue, double widthValue, double heightValue);
Box(); // No-arg constructor
double volume(); // Function to calculate the volume of a box
};
// Constructor definition
Box::Box(double lengthValue, double widthValue, double heightValue)
{
std::cout << "Box constructor called." << std::endl;
length = lengthValue;
width = widthValue;
height = heightValue;
}
Box::Box() {} // No-arg constructor
// Function to calculate the volume of a box
double Box::volume()
{
return length*width*height;
}
int main()
{
Box firstBox {80.0, 50.0, 40.0}; // Create a box
double firstBoxVolume{firstBox.volume()}; // Calculate the box volume
std::cout << "Volume of Box object is" << firstBoxVolume << std::endl;
}
上面的代碼生成以下結(jié)果。
class Box { private: double length; double width; double height; public: // Constructors Box(double lv = 1.0, double wv = 1.0, double hv = 1.0); Box(); // No-arg constructor double volume(); // Function to calculate the volume of a box };
更多建議: