C++中的类、对象、函数、构造函数、析构函数

在C++中,(class)是用于定义对象的模板或蓝图。类中可以包含数据成员(变量)和成员函数(方法)。对象是类的实例,通过类定义创建的实体。构造函数和析构函数是特殊的成员函数,用于对象的初始化和清理。

类和对象

定义类

类是通过class关键字定义的。类包含数据成员和成员函数。

class MyClass {
private:
    int data; // 数据成员

public:
    // 成员函数
    void setData(int value) {
        data = value;
    }

    int getData() const {
        return data;
    }
};

创建对象

对象是类的实例,通过类的构造函数创建。

int main() {
    MyClass obj; // 创建MyClass类的对象obj
    obj.setData(5);
    std::cout << "Data: " << obj.getData() << std::endl;
    return 0;
}

构造函数

构造函数是用于初始化对象的特殊成员函数。它们在对象创建时自动调用。构造函数的名称与类名相同,并且没有返回类型。

class MyClass {
private:
    int data;

public:
    // 默认构造函数
    MyClass() : data(0) {
        std::cout << "Default constructor called" << std::endl;
    }

    // 带参数的构造函数
    MyClass(int value) : data(value) {
        std::cout << "Parameterized constructor called" << std::endl;
    }

    // 拷贝构造函数
    MyClass(const MyClass& other) : data(other.data) {
        std::cout << "Copy constructor called" << std::endl;
    }

    int getData() const {
        return data;
    }
};

析构函数

析构函数是用于清理对象的特殊成员函数。它们在对象生命周期结束时自动调用。析构函数的名称与类名相同,并且在前面加上~。析构函数也没有返回类型。

class MyClass {
private:
    int data;

public:
    MyClass() : data(0) {
        std::cout << "Constructor called" << std::endl;
    }

    ~MyClass() {
        std::cout << "Destructor called" << std::endl;
    }

    void setData(int value) {
        data = value;
    }

    int getData() const {
        return data;
    }
};

示例1

将上面几段代码连接起来,输入数据测试结果:

#include <iostream>

class MyClass {
private:
    int data;

public:
    // 默认构造函数
    MyClass() : data(0) {
        std::cout << "Default constructor called" << std::endl;
    }

    // 带参数的构造函数
    MyClass(int value) : data(value) {
        std::cout << "Parameterized constructor called" << std::endl;
    }

    // 拷贝构造函数
    MyClass(const MyClass& other) : data(other.data) {
        std::cout << "Copy constructor called" << std::endl;
    }

    // 析构函数
    ~MyClass() {
        std::cout << "Destructor called" << std::endl;
    }

    void setData(int value) {
        data = value;
    }

    int getData() const {
        return data;
    }
};

int main() {
    MyClass obj1; // 调用默认构造函数
    MyClass obj2(10); // 调用带参数的构造函数
    MyClass obj3 = obj2; // 调用拷贝构造函数

    obj1.setData(5);
    std::cout << "obj1 Data: " << obj1.getData() << std::endl;
    std::cout << "obj2 Data: " << obj2.getData() << std::endl;
    std::cout << "obj3 Data: " << obj3.getData() << std::endl;

    return 0;
}

输出

Default constructor called
Parameterized constructor called
Copy constructor called
obj1 Data: 5
obj2 Data: 10
obj3 Data: 10
Destructor called
Destructor called
Destructor called

示例2

实现一个用于管理银行账户的类,这个类将包含以下功能:

  • 创建一个新账户。
  • 存款。
  • 取款。
  • 查询账户余额。
    . 定义银行账户类

定义一个名为BankAccount的类,它包含以下成员:

数据成员:账户余额(balance)。
成员函数:
    构造函数:用于初始化账户余额。
    存款函数:用于向账户存入金额。
    取款函数:用于从账户取出金额。
    查询余额函数:用于获取当前余额。
    析构函数:用于在对象销毁时输出消息。
#include <iostream>

class BankAccount {
private:
    double balance; // 账户余额

public:
    // 默认构造函数
    BankAccount() : balance(0.0) {
        std::cout << "Default constructor called. Account created with balance: " << balance << std::endl;
    }

    // 带参数的构造函数
    BankAccount(double initialBalance) : balance(initialBalance) {
        std::cout << "Parameterized constructor called. Account created with balance: " << balance << std::endl;
    }

    // 存款函数
    void deposit(double amount) {
        if (amount > 0) {
            balance += amount;
            std::cout << "Deposited: " << amount << ". New balance: " << balance << std::endl;
        } else {
            std::cout << "Invalid deposit amount!" << std::endl;
        }
    }

    // 取款函数
    void withdraw(double amount) {
        if (amount > 0 && amount <= balance) {
            balance -= amount;
            std::cout << "Withdrawn: " << amount << ". New balance: " << balance << std::endl;
        } else {
            std::cout << "Invalid withdraw amount or insufficient funds!" << std::endl;
        }
    }

    // 查询余额函数
    double getBalance() const {
        return balance;
    }

    // 析构函数
    ~BankAccount() {
        std::cout << "Destructor called. Account with balance " << balance << " is being closed." << std::endl;
    }
};

int main() {
    BankAccount account1; // 使用默认构造函数
    account1.deposit(100.0);
    account1.withdraw(30.0);
    std::cout << "Current balance: " << account1.getBalance() << std::endl;

    BankAccount account2(500.0); // 使用带参数的构造函数
    account2.deposit(150.0);
    account2.withdraw(200.0);
    std::cout << "Current balance: " << account2.getBalance() << std::endl;

    return 0;
}

输出

Default constructor called. Account created with balance: 0
Deposited: 100. New balance: 100
Withdrawn: 30. New balance: 70
Current balance: 70
Parameterized constructor called. Account created with balance: 500
Deposited: 150. New balance: 650
Withdrawn: 200. New balance: 450
Current balance: 450
Destructor called. Account with balance 70 is being closed.
Destructor called. Account with balance 450 is being closed.

总结

  • 类:通过class关键字定义,包含数据成员和成员函数。
  • 对象:类的实例,通过构造函数创建。
  • 函数:类中的成员函数,用于操作类的对象。
  • 构造函数:用于初始化对象,在对象创建时自动调用。
  • 析构函数:用于清理对象,在对象生命周期结束时自动调用。
  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI1.0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值