(实验3-3) 构造函数的调用

本文通过实例演示了在创建普通对象、对象数组及动态创建和销毁对象时构造函数与析构函数的调用时机。通过观察输出结果,理解C++中对象生命周期管理的基本原则。
在C++面向对象编程中,构造函数析构函数是重要的概念,以下结合参考内容介绍相关实验可能涉及的内容。 ### 构造函数析构函数基础实验 可以通过定义一个简单的类来了解构造函数析构函数的基本使用。例如定义一个`C_student`类,包含学号、姓名和班级等成员变量,同时定义构造函数析构函数: ```cpp #include<bits/stdc++.h> using namespace std; class C_student{ public: int Number_; string Name_; int Class_; // 构造函数 C_student(int number, string name, int classNum) { Number_ = number; Name_ = name; Class_ = classNum; cout << "C_student对象被创建" << endl; } // 析构函数 ~C_student() { cout << "C_student对象被销毁" << endl; } void print(){ cout << "我的名字是" << Name_ << endl << "我的学号是" << Number_ << endl << "我的班级是" << Class_ << endl; } }; int main(){ C_student student(1, "张三", 101); student.print(); return 0; } ``` 在这个实验中,可以观察到对象创建时构造函数调用,对象销毁时析构函数调用。 ### 构造函数的分类及调用实验 构造函数可以分为默认构造函数、有参构造函数和拷贝构造函数。可以通过实验来观察不同构造函数调用情况。 ```cpp #include <iostream> using namespace std; class Test { public: // 默认构造函数 Test() { cout << "默认构造函数调用" << endl; } // 有参构造函数 Test(int value) { cout << "有参构造函数调用,值为: " << value << endl; } // 拷贝构造函数 Test(const Test& other) { cout << "拷贝构造函数调用" << endl; } }; int main() { Test t1; // 调用默认构造函数 Test t2(10); // 调用有参构造函数 Test t3(t2); // 调用拷贝构造函数 return 0; } ``` ### 析构函数调用时机实验 可以通过不同的对象创建和销毁方式来观察析构函数调用时机。例如使用局部对象和动态分配的对象: ```cpp #include <iostream> using namespace std; class Test { public: Test() { cout << "Test对象被创建" << endl; } ~Test() { cout << "Test对象被销毁" << endl; } }; int main() { cout << "p1" << endl; { Test t1; // t1 的生命周期就只在这个大括号内 } // 因此在这个位置 t1 的析构函数就会被调用 cout << "p2" << endl; Test *t2 = new Test; delete t2; // t2 所指对象的析构函数在此被调用 cout << "p3" << endl; { Test *t3 = new Test; } // t3 所指对象的析构函数并不会被调用,因为没有使用 delete 运算符 return 0; } ``` ### 构造函数调用规则实验 实验可以验证构造函数调用规则,如如果程序员提供了有参构造,那么编译器不会提供默认构造函数,但是会提供默认的拷贝构造;如果程序员提供了拷贝构造函数,那么编译器不会提供默认的构造函数和默认的拷贝构造函数。 ### 多个对象的构造和析构实验 如果类有成员对象,那么先调用成员对象的构造函数,再调用本身的构造函数析构函数调用顺序反之。可以通过定义包含成员对象的类来进行实验。 ```cpp #include <iostream> using namespace std; class Member { public: Member() { cout << "Member对象被创建" << endl; } ~Member() { cout << "Member对象被销毁" << endl; } }; class MyClass { public: Member member; MyClass() { cout << "MyClass对象被创建" << endl; } ~MyClass() { cout << "MyClass对象被销毁" << endl; } }; int main() { MyClass obj; return 0; } ``` ### 对象的深浅拷贝实验 默认的拷贝构造函数进行了简单的赋值操作(浅拷贝),可能会引发问题,而深拷贝可以解决这些问题。可以通过实验来观察浅拷贝的问题和深拷贝的解决方法。 ```cpp #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 << "构造函数调用" << endl; } // 浅拷贝构造函数 String(const String& other) { str = other.str; cout << "浅拷贝构造函数调用" << endl; } // 深拷贝构造函数 String(const String& other, bool deepCopy) { if (deepCopy) { str = new char[strlen(other.str) + 1]; strcpy(str, other.str); cout << "深拷贝构造函数调用" << endl; } else { str = other.str; cout << "浅拷贝构造函数调用" << endl; } } // 析构函数 ~String() { delete[] str; cout << "析构函数调用" << endl; } }; int main() { String s1("Hello"); String s2(s1); // 浅拷贝 String s3(s1, true); // 深拷贝 return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值