第10课: 构造函数与析构函数
构造函数
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string name;
int age;
public:
// 默认构造函数
Student() {
name = "未命名";
age = 0;
cout << "调用默认构造函数" << endl;
}
// 带参数的构造函数
Student(string n, int a) {
name = n;
age = a;
cout << "调用带参数构造函数" << endl;
}
void display() {
cout << "姓名:" << name << ",年龄:" << age << endl;
}
};
int main() {
Student s1; // 调用默认构造函数
Student s2("张三", 20); // 调用带参数构造函数
s1.display();
s2.display();
return 0;
}
初始化列表
#include <iostream>
#include <string>
using namespace std;
class Person {
private:
string name;
int age;
const int id; // 常量成员必须用初始化列表
public:
// 使用初始化列表(推荐方式)
Person(string n, int a, int i) : name(n), age(a), id(i) {
cout << "创建Person对象" << endl;
}
void display() {
cout << "ID:" << id << ", 姓名:" << name << ", 年龄:" << age << endl;
}
};
int main() {
Person p("李四", 25, 1001);
p.display();
return 0;
}
拷贝构造函数
#include <iostream>
#include <string>
using namespace std;
class Book {
private:
string title;
double price;
public:
// 普通构造函数
Book(string t, double p) : title(t), price(p) {
cout << "创建书籍:" << title << endl;
}
// 拷贝构造函数
Book(const Book &other) {
title = other.title;
price = other.price;
cout << "拷贝书籍:" << title << endl;
}
void display() {
cout << "书名:" << title << ",价格:" << price << endl;
}
};
int main() {
Book book1("C++编程", 89.0);
Book book2 = book1; // 调用拷贝构造函数
Book book3(book1); // 也是调用拷贝构造函数
book1.display();
book2.display();
return 0;
}
析构函数
#include <iostream>
#include <string>
using namespace std;
class Array {
private:
int *data;
int size;
public:
// 构造函数
Array(int s) : size(s) {
data = new int[size];
cout << "分配内存,大小:" << size << endl;
}
// 析构函数:对象销毁时自动调用
~Array() {
delete[] data;
cout << "释放内存,大小:" << size << endl;
}
void setValue(int index, int value) {
if (index >= 0 && index < size) {
data[index] = value;
}
}
int getValue(int index) {
if (index >= 0 && index < size) {
return data[index];
}
return 0;
}
};
int main() {
{
Array arr(5);
arr.setValue(0, 100);
cout << "值:" << arr.getValue(0) << endl;
} // arr离开作用域,自动调用析构函数
cout << "程序继续执行" << endl;
return 0;
}
深拷贝与浅拷贝
#include <iostream>
#include <cstring>
using namespace std;
class String {
private:
char *str;
public:
// 构造函数
String(const char *s) {
str = new char[strlen(s) + 1];
strcpy(str, s);
cout << "创建字符串:" << str << endl;
}
// 深拷贝构造函数
String(const String &other) {
str = new char[strlen(other.str) + 1];
strcpy(str, other.str);
cout << "深拷贝字符串:" << str << endl;
}
// 析构函数
~String() {
cout << "删除字符串:" << str << endl;
delete[] str;
}
void display() {
cout << "字符串内容:" << str << endl;
}
};
int main() {
String s1("Hello");
String s2 = s1; // 深拷贝,各自有独立的内存
s1.display();
s2.display();
return 0;
} // 两个对象都能正确析构
委托构造函数(C++11)
#include <iostream>
#include <string>
using namespace std;
class Rectangle {
private:
double width;
double height;
public:
// 主构造函数
Rectangle(double w, double h) : width(w), height(h) {
cout << "创建矩形:" << width << "x" << height << endl;
}
// 委托构造函数
Rectangle() : Rectangle(1.0, 1.0) {
cout << "使用默认尺寸" << endl;
}
Rectangle(double side) : Rectangle(side, side) {
cout << "创建正方形" << endl;
}
double getArea() {
return width * height;
}
};
int main() {
Rectangle r1; // 默认1x1
Rectangle r2(5.0); // 正方形5x5
Rectangle r3(3.0, 4.0); // 矩形3x4
cout << "面积:" << r1.getArea() << endl;
cout << "面积:" << r2.getArea() << endl;
cout << "面积:" << r3.getArea() << endl;
return 0;
}
练习题
- 创建一个动态数组类,实现构造、析构和拷贝构造函数
- 设计一个链表节点类,正确管理内存
- 实现一个字符串类,支持深拷贝
- 创建一个资源管理类,演示RAII(资源获取即初始化)