技术交流QQ群:1027579432,欢迎你的加入!

1.构造函数

  • 类的构造函数是类的一种特殊的成员函数,它会在每次创建类的新对象时执行。构造函数的名称与类的名称的完全相同的,并且不会返回任何数据类型,也不会返回void。构造函数用于为某些成员变量设置初始值。

  • 构造函数实例如下:

    #include "iostream"
    
        using namespace std;
    
        class Line{
            public:
                void setLength(double len);
                double getLength();
                Line();   // 构造函数
            private:
                double length;
        };
    
        // 构造函数的定义
        Line::Line(){
            cout << "Line的对象line正在被创建..." << endl;
        }
    
    
        // 成员函数的定义
        void Line::setLength(double len){
            length = len;
        }
    
        double Line::getLength(){
            return length;
        }
    
    
        int main(){
            Line line;  // 创建一个line对象,此时会去执行构造函数
            line.setLength(4.0);
            cout << "Line的长度是: " << line.getLength() << endl;
            return 0;
        }
  • 带参数的构造函数:默认的构造函数是没有任何参数的,但如果需要参数,构造函数也可以带有参数。这样在创建对象时就会给对象赋初始值,如下面的例子:

    #include "iostream"
    
        using namespace std;
    
        class Line{
            public:
                void setLength(double len);
                double getLength();
                Line(double len);   // 带参数的构造函数
            private:
                double length;
        };
    
        // 构造函数的定义
        Line::Line(double len){
            cout << "Line的对象line正在被创建, lenght = " << len << endl;
            length = len;
        }
    
    
        // 成员函数的定义
        void Line::setLength(double len){
            length = len;
        }
    
        double Line::getLength(){
            return length;
        }
    
    
        int main(){
            // 获取默认设置的长度
            Line line(10.0);  // 创建一个line对象,此时会去执行带参数的构造函数
            line.setLength(4.0);
            cout << "Line的长度是: " << line.getLength() << endl;
            return 0;
        }
  • 使用初始化列表来初始化字段,见下面的实例:

    class Box{
            public:
                void setLength(double len);
                double getLength();
                void setWidth(double wid);
                double getWidth();
                void setHeight(double hei);
                double getHeight();
    
                // 使用初始化列表来初始化字段----构造函数
                Box(double len, double wid, double hei): length(len), width(wid), height(hei){
                    cout << "Box的对象box正在被创建,length = " << len << " width = " << wid << " height = " << hei << endl; 
                }
            private:
                double length;
                double width;
                double height;
        };
    
        // Box类的成员函数定义
        void Box::setLength(double len){
            length = len;
        }
    
        double Box::getLength(){
            return length;
        }
    
        void Box::setWidth(double wid){
            width = wid;
        }
    
        double Box::getWidth(){
            return width;
        }
    
        void Box::setHeight(double hei){
            height = hei;
        }
    
        double Box::getHeight(){
            return height;
        }
    
        Box box(3.0, 4.0, 5.0);
        box.setLength(33.0);
        cout << "Box的长度是: " << box.getLength() << endl;
        box.setWidth(44.0);
        cout << "Box的宽度是: " << box.getWidth() << endl;
        box.setHeight(55.0);
        cout << "Box的高度是: " << box.getHeight() << endl;
  • 假设一个类C,具有多个字段X,Y,Z等需要进行初始化,同理,可以使用上面的语法,只需要在不同的字段使用逗号进行分隔即可。如下面的实例:

    C::C(double a, double b, double c): X(a), Y(b), Z(c){
            语句;
        }

2.析构函数

  • 类的析构函数的类的一种特殊的成员函数,它会在每次删除所创建的对象时执行。析构函数的名称与类的名称完全相同。只是在前面加上一个波浪号~作为前缀,它不会返回任何值,也不能带有任何参数。析构函数有助于跳出程序前释放内存资源。实例如下:
    #include "iostream"
    
        using namespace std;
    
        class Line{
            public:
                void setLength(double len);
                double getLength();
                Line(double len);   // 带参数的构造函数
                ~Line();  // 析构函数不能带有任何参数
            private:
                double length;
        };
    
        class Box{
            public:
                void setLength(double len);
                double getLength();
                void setWidth(double wid);
                double getWidth();
                void setHeight(double hei);
                double getHeight();
    
                // 使用初始化列表来初始化字段----构造函数
                Box(double len, double wid, double hei): length(len), width(wid), height(hei){
                    cout << "Box的对象box正在被创建,length = " << len << " width = " << wid << " height = " << hei << endl; 
                }
                // 析构函数
                ~Box(){
                    cout << "对象box正在被删除...\n";
                }
            private:
                double length;
                double width;
                double height;
        };
    
    
        // 构造函数的定义
        Line::Line(double len){
            cout << "Line的对象line正在被创建, lenght = " << len << endl;
            length = len;
        }
    
        // 析构函数定义
        Line::~Line(){
            cout << "Line的对象正在被删除..." << endl;
        }
        // // 使用初始化列表来初始化字段
        // Line::Line(double len): length(len){
        //     cout << "Line的对象line正在被创建, lenght = " << len << endl;    
        // }
    
    
        // 成员函数的定义
        void Line::setLength(double len){
            length = len;
        }
    
        double Line::getLength(){
            return length;
        }
        // Box类的成员函数定义
        void Box::setLength(double len){
            length = len;
        }
    
        double Box::getLength(){
            return length;
        }
    
        void Box::setWidth(double wid){
            width = wid;
        }
    
        double Box::getWidth(){
            return width;
        }
    
        void Box::setHeight(double hei){
            height = hei;
        }
    
        double Box::getHeight(){
            return height;
        }
    
    
        int main(){
            // 获取默认设置的长度
            Line line(10.0);  // 创建一个line对象,此时会去执行带参数的构造函数
            line.setLength(4.0);
            cout << "Line的长度是: " << line.getLength() << endl;
            cout << "-------------------------------------------\n";
            Box box(3.0, 4.0, 5.0);
            box.setLength(33.0);
            cout << "Box的长度是: " << box.getLength() << endl;
            box.setWidth(44.0);
            cout << "Box的宽度是: " << box.getWidth() << endl;
            box.setHeight(55.0);
            cout << "Box的高度是: " << box.getHeight() << endl;
            return 0;
        }
    • 注意:上面的程序中,创建了两个对象line和box,由于是先创建的line对象,后创建的是box对象;所以在执行析构函数时,必须先删除box对象,后删除line对象。
  • C++初始化类成员时,是按照声明的顺序初始化的,而不是按照出现在初始化列表中的顺序!,见下面的实例:
class Student1{
        public:
            int a;
            int b;
            // 普通成员函数
            void fprint(){
                cout << "a = " << a << ",b = " << b << endl;
            }
            // Student1(int i): b(i), a(b){} 异常顺序:发现a的值是0,b的值是2;说明初始化仅仅对b有效,对a没有起到初始化的作用
            Student1(int i): a(i), b(a){} // 正常顺序:a = b = 2
            // 默认构造函数
            Student1(){
                cout << "默认构造函数Student1\n";
            }
            // 拷贝构造函数
            Student1(const Student1& t1){
                cout << "拷贝构造函数Student1\n";
                this->a = t1.a;
            }
    };
    Student1 A(2);        //进入默认构造函数 
    A.fprint();            //输出前面初始化的结果