【 C++】构造函数和成员函数

详细探讨 C++ 中的构造函数和成员函数(方法),并通过代码示例进行说明。

1. 构造函数 (Constructors)

  • 目的: 构造函数是一种特殊的成员函数,其主要目的是在创建对象时初始化对象的数据成员。
  • 名称: 构造函数的名称必须与类名完全相同。
  • 返回类型: 构造函数没有返回类型,甚至不能声明为 void
  • 调用时机: 当创建类的对象时,构造函数会自动被调用。
  • 重载: 构造函数可以被重载,这意味着你可以定义多个具有不同参数列表的构造函数。

构造函数的类型

  • 默认构造函数 (Default Constructor):

    • 不带任何参数的构造函数。
    • 如果没有显式定义任何构造函数,编译器会提供一个默认构造函数(但这个默认构造函数可能不做任何初始化工作,特别是对于内置类型成员)。
    • 如果定义了其他构造函数,编译器不再提供默认构造函数。
  • 带参数的构造函数 (Parameterized Constructor):

    • 接受一个或多个参数的构造函数。
    • 用于在创建对象时使用特定的值初始化对象。
  • 拷贝构造函数 (Copy Constructor):

    • 接受一个同类型对象的引用作为参数的构造函数。
    • 用于通过复制现有对象来创建一个新对象。
    • 如果没有显式定义,编译器会提供一个默认拷贝构造函数(执行浅拷贝)。
  • 移动构造函数 (Move Constructor) (C++11):

    • 接受一个同类型对象的右值引用作为参数的构造函数
    • 将资源从一个临时对象“移动”到新创建的对象, 而不是复制, 提升效率。

代码示例

#include <iostream>
#include <string>

class Rectangle {
private:
    double width;
    double height;

public:
    // 默认构造函数
    Rectangle() : width(0.0), height(0.0) {
        std::cout << "Default constructor called" << std::endl;
    }

    // 带参数的构造函数
    Rectangle(double w, double h) : width(w), height(h) {
        std::cout << "Parameterized constructor called" << std::endl;
    }

    // 拷贝构造函数
    Rectangle(const Rectangle& other) : width(other.width), height(other.height) {
        std::cout << "Copy constructor called" << std::endl;
    }
    
     // 移动构造函数 (C++11)
    Rectangle(Rectangle&& other) noexcept : width(std::move(other.width)), height(std::move(other.height))
    {
        other.width = 0;
        other.height = 0;
        std::cout << "Move constructor called\n";
    }

    // 成员函数(方法)
    double getArea() const {
        return width * height;
    }

    void setDimensions(double w, double h) {
        width = w;
        height = h;
    }

    void print() const{
        std::cout << "Width: " << width << ", Height: " << height << std::endl;
    }
};

int main() {
    // 使用默认构造函数创建对象
    Rectangle rect1;
    rect1.print(); // 输出 Width: 0, Height: 0

    // 使用带参数的构造函数创建对象
    Rectangle rect2(5.0, 3.0);
    rect2.print(); // 输出 Width: 5, Height: 3

    // 使用拷贝构造函数创建对象
    Rectangle rect3 = rect2; 
    rect3.print(); // 输出 Width: 5, Height: 3

    // 使用成员函数
    std::cout << "Area of rect2: " << rect2.getArea() << std::endl; // 输出 Area of rect2: 15
    rect2.setDimensions(7.0, 4.0);
    std::cout << "Area of rect2 after setDimensions: " << rect2.getArea() << std::endl; // 输出 28

    //移动构造
    Rectangle rect4 = std::move(rect2);  // 调用移动构造函数
    rect4.print();       // rect4 的值
    rect2.print();          // rect2 的width, height 已经被设置为0

    return 0;
}

2. 成员函数 (Member Functions / Methods)

  • 目的: 成员函数是定义在类内部的函数,用于操作对象的数据成员或执行与对象相关的操作。
  • 访问权限: 成员函数可以访问类的所有成员(包括私有成员)。
  • this 指针: 在成员函数内部,可以使用 this 指针来引用当前对象。this 指针是一个指向当前对象的指针。
  • const 成员函数:
    • 在函数声明的末尾加上 const 关键字,表示该函数不会修改对象的数据成员。
    • const 对象只能调用 const 成员函数。

代码示例 (接上面的 Rectangle 类)

// ... (Rectangle 类的定义) ...

int main() {
    // ... (之前的代码) ...

    const Rectangle constRect(2.0, 2.0); // 创建一个 const 对象

    // constRect.setDimensions(4.0, 4.0);  // 错误!const 对象不能调用非 const 成员函数
    std::cout << "Area of constRect: " << constRect.getArea() << std::endl; // 正确,可以调用 const 成员函数
    constRect.print(); //正确

    return 0;
}

要点总结

  • 构造函数用于初始化对象,成员函数用于操作对象。
  • 构造函数没有返回类型,名称与类名相同,可以重载。
  • 成员函数可以访问类的所有成员,this 指针指向当前对象。
  • const 成员函数不能修改对象的数据成员,const 对象只能调用 const 成员函数。
  • 理解构造函数和成员函数对于编写面向对象的 C++ 代码至关重要。

希望这些详细的解释和代码示例能帮助你更好地理解 C++ 中的构造函数和成员函数。如果有任何其他问题,请随时提出!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值