C++ 构造函数是一种特殊的函数,它在创建对象时被调用,用于初始化对象。构造函数的名称与类的名称相同,并且不会返回任何类型,也不会返回 void。构造函数可以有参数,也可以没有参数。
C++ 析构函数是一种特殊的函数,它在对象销毁时被自动调用。它的名字与类的名字相同,但是有一个波浪号“~”作为前缀。它不能带有任何参数,也不能返回任何值。它的作用是释放已分配的内存、打开文件、释放占用的其他资源等。
// 构造函数 ClassName::ClassName(parameter_list) { // 初始化代码 } // 析构函数 ClassName::~ClassName() { // 释放内存代码 }
类构造函数是类中的一种特殊类型的函数。
当定义类的新实例时调用构造函数。
它在创建新对象时初始化,并确保数据成员包含有效值。
类构造函数与类具有相同的名称。
Box(),例如是Box类的构造函数。
构造函数不返回值,因此没有返回类型。
如果您没有为类定义构造函数,编译器将提供默认构造函数。
一般来说,如果Class_name是一个类,如果value的类型为Type_name,则该语句
Class_name * pclass = new Class_name (value);
调用此构造函数:
Class_name (Type_name);
可能有微小的转换,例如:
Class_name(const Type_name &);
实例化MyBook类的多个对象,并使用MyBook构造函数指定每个MyBook对象创建时的课程名称。
#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 forn" << 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;
}
上面的代码生成以下结果。
复制构造函数的形式对于任何类是相同的:
Type::Type(const Type& object) { // Code to duplicate of object... }
默认构造函数没有参数,唯一的目的是允许创建一个对象。
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类添加构造函数。
#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;
}
上面的代码生成以下结果。
函数成员的定义可以放在类定义之外。
类构造函数也是如此。
#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;
}
上面的代码生成以下结果。
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 };
学习C++-C++ while循环while循环是初始化和更新部分的for循环;它只是一个测试条件和主体:while (test-condition)body 如果表达...
C++ 指针 vs 数组 C++ 指针指针和数组是密切相关的。事实上,指针和数组在很多情况下是可以互换的。例如,一个指向数组开头的指...
C++ 关系运算符重载 C++ 重载运算符和重载函数C++ 语言支持各种关系运算符(、、 = 、 = 、 == 等等),它们可用于比较 C++ 内置...
C++ 类成员访问运算符 - 重载 C++ 重载运算符和重载函数类成员访问运算符( - )可以被重载,但它较为麻烦。它被定义用于为一个...