C++构造函数详解-清华大学教程要点

下载需积分: 32 | PPT格式 | 8.81MB | 更新于2024-08-19 | 40 浏览量 | 3 下载量 举报
收藏
"这篇资料来自《C++清华大学-谭浩强》,主要讲解了C++中的构造函数,并提及C++语言的一些基本特点。" 在C++编程中,构造函数扮演着至关重要的角色,它是类的一个特殊成员函数,专门用于初始化新创建的对象。以下是关于构造函数的几个关键知识点: 1. **构造函数的命名规则**:构造函数的名称必须与它所在的类名称完全相同,这是其最显著的特征。例如,如果有一个名为`Person`的类,那么它的构造函数也会命名为`Person()`。 2. **无返回类型**:不同于一般的成员函数,构造函数在定义时不能指定返回值类型,即使`void`也不行。这是因为构造函数的目的是为了初始化对象,它的执行过程自然伴随着对象的创建,所以无需返回任何值。 3. **函数重载**:一个类可以有多个构造函数,每个构造函数有不同的参数列表,这称为构造函数的重载。这种机制允许我们在创建对象时根据需要采用不同的初始化方式,提供更大的灵活性。 4. **初始化数据成员**:构造函数的主要任务是初始化对象的数据成员,确保对象在创建时处于正确的初始状态。可以通过成员初始化列表(member initializer list)来实现,这种方式可以更高效且避免了不必要的默认构造和赋值操作。 5. **默认构造函数**:如果类没有定义任何构造函数,编译器会自动生成一个默认构造函数,它没有参数且不做任何实际的初始化操作。 6. **带参数的构造函数**:有时我们需要在创建对象时提供一些初始值,这时可以定义带有参数的构造函数,参数可以用来设置对象的初始状态。 7. **拷贝构造函数**:当一个对象被用作另一个对象的初始值或作为函数参数时,拷贝构造函数会被调用。默认的拷贝构造函数执行浅复制,但通常我们需要自定义以执行深复制,特别是在处理包含动态分配内存的对象时。 8. **移动构造函数**:在C++11引入了移动语义,移动构造函数用于优化资源的转移,它可以从另一个临时对象中“窃取”资源,而不是复制。 9. **构造函数的作用域**:构造函数只在类的实例化过程中被调用,一旦对象创建完成,构造函数就不再被执行。 此外,资料中还提到了C++语言的一些核心特点: 1. **结构化编程**:C++支持结构化编程,通过函数、循环和条件语句,可以组织代码使其清晰易读。 2. **面向过程与面向对象**:C++是C语言的扩展,同时支持面向过程和面向对象的编程范式,提供了丰富的类和对象机制。 3. **高效性**:由于C++允许直接访问硬件,它编译后的代码执行效率高,适用于系统级编程和游戏开发。 4. **可移植性**:C++编写的程序可以在多种平台和设备上运行,只需少量或无需修改,这得益于其标准库的跨平台兼容性。 5. **学习曲线**:虽然C++提供了强大的功能,但这也意味着学习曲线相对较陡,特别是对于初学者,调试和理解程序可能更具挑战性。 理解这些概念是掌握C++编程的关键步骤,尤其是在深入学习类、对象、继承、多态等面向对象特性时,构造函数的作用更加凸显。通过实践和不断学习,可以逐步克服编程中遇到的困难,提高C++编程技能。

相关推荐

filetype

优化以下代码:#include <iostream> #include<string.h> using namespace std; class Book { private:     char bookname[30];     char authers[30];     char publishing_house[40];     int pages;     float price; public:     char getbookname();     char getauthers();     char getpublishing_house();     int getpages();     float getprice();          void setbookname(char *a);     void setauthers(char *a);     void setpublishing_house(char *a);     void setpages(int a);     void setprice(float a);     Book(char*a,char*b,cahr*c,int d,float e)     {         strcpy(bookname,a);         strcpy(authers,b);         strcpy(publishing_house,c);         pages=d;         price=e;     } }; char getbookname() {     char*a=bookname;     return a; } char getauthers() {     char*a=authers;     return a; } char getpublishing_house() {     char*a=publishing_house;     return a; } int getpages() {     int a=pages;     return a; } float getprice() {     float a=price;     return a; } void setbookname(char *a) {     strcpy(bookname,a); } void setauthers(char *a) {     strcpy(authers,a); } void setpublishing_house(char *a) {     strcpy(publishing_house,a); } void setpages(int a) {     pages=a; } void setprice(float a) {     price=a; } int main() {      Book a(char a="《C++程序设计》",char b="谭浩强编制",char c="清华大学出版社",d=484,e=36.00);      Book b(char a="《吹牛大王历险记》",char b="拉斯伯等编著",char c="天津人民出版社",d=149,e=12.80);     cout<<a.getbookname()<<"  "<<a.getauthers()<<"  "<<a.getpublishing_house()<<"  "<<"页数:"<<a.getpages()<<"价钱:"<<a.getprice()<<endl;     cout<<b.getbookname()<<"  "<<b.getauthers()<<"  "<<b.getpublishing_house()<<"  "<<"页数:"<<b.getpages()<<"价钱:"<<b.getprice()<<endl;     return 0; }

100 浏览量
filetype

#include<iostream> using namespace std; class Book { private: string bookname; string authtors; string publishing_house; int pages; double price; public: Book() { bookname = ""; authtors = ""; publishing_house = ""; pages = 0; price = 0; } Book(string bk, string auth, string pub, int pag,double pri) { bookname = bk; authtors = auth; publishing_house = pub; pages = pag; price = pri; } ~Book(){} string getBookname() const { return bookname; } void setBookname(char*name){ bookname=name; } string getAuthtors() const { return authtors; } void setAuthtors(char* auth) { authtors = auth; } string getPublishing_house() const { return publishing_house; } void setPublishing_house(char* pub) { publishing_house = pub; } int getPages() const { return pages; } void setPages(int pag) { pages = pag; } double getPrice() const { return price; } void setPrice(double pri) { price = pri; } }; int main() { Book book1("《C++程序设计》", "谭浩强编著", 485, "清华大学出版社", 36.00); Book book2("《吹牛大王历险记》", "拉斯伯等编著", 149, "天津人民出版社", 12.80); cout << "书名:" << book1.getBookname() << endl; cout << "作者:" << book1.getAuthtors() << endl; cout << "页数:" << book1.getPages() <<"页"<< endl; cout << "出版社:" << book1.getPublishing_house() << endl; cout << "价格:" << book1.getPrice() <<"元"<< endl; cout << endl; cout << "书名:" << book2.getBookname() << endl; cout << "作者:" << book2.getAuthtors() << endl; cout << "页数:" << book2.getPages() << "页" << endl; cout << "出版社:" << book2.getPublishing_house() << endl; cout << "价格:" << book2.getPrice() << "元" << endl; return 0; }这个代码如何修改

155 浏览量