C类的构造函数是如何实现其初始化过程的?

2026-04-16 23:101阅读0评论SEO资源
  • 内容介绍
  • 相关推荐

本文共计1292个文字,预计阅读时间需要6分钟。

C类的构造函数是如何实现其初始化过程的?

在上一章中,初步介绍了类的部分内容。必须熟练掌握类的内容与结构体相同,好了。当我们创建一个类时,有时需要初始化,这时就需要构造函数。构造函数的格式为:`C`

上一章中, 初步的介绍了类的部分内容, 必须熟练掌握

部分内容与结构体相同

好了, 当我们创建一个类时, 有的时候需要初始化, 这时候就需用到构造函数

构造函数的格式为:

<ClassType>(<List>) { //Do something... }

<ClassType> 为类的名字

<List> 为参数列表

在类创建的时候, 都会调用构造函数

但是有的时候不写自己的构造函数的话, 系统会调用默认的构造函数 (也就是什么都不做)

但是当写了构造函数后, 系统就不会调用默认的构造函数

构造函数的声明必须写在类中, 但是实现可以写在外头 (如果不知道如何将成员函数外部实现的看这里):

1 class Student 2 { 3 string name; 4 public: 5 Student(string) 6 }; 7 Student::Student(string name) 8 { 9 this->name=name;//这里可以直接访问类的成员 10 }

备注: 在第9行用到了 this 指针, 不明白的看这里

这样一来, 在声明类的时候, 可以调用构造函数, 其格式为:

<ClassType> 变量名(<List>)

<List> 指该类的构造函数的参数列表

构造函数必须这样写, 不得写在声明后的其余地方

1 #include <iostream> 2 #include <string> 3 using namespace std; 4 class Student 5 { 6 string name; 7 public: 8 Student(string); 9 string GetName(); 10 }; 11 Student::Student(string name) 12 { 13 this->name=name; 14 } 15 string Student::GetName() 16 { 17 return name; 18 } 19 int main() 20 { 21 Student s; 22 s.Student("TweeChalice");//Error!!! 23 cout<<s.GetName()<<endl; 24 }

必须写成这样:

1 #include <iostream> 2 #include <string> 3 using namespace std; 4 class Student 5 { 6 string name; 7 public: 8 Student(string); 9 string GetName(); 10 }; 11 Student::Student(string name) 12 { 13 this->name=name; 14 } 15 string Student::GetName() 16 { 17 return name; 18 } 19 int main() 20 { 21 Student s("TweeChalice"); 22 cout<<s.GetName()<<endl; 23 }

现在又有一个问题了, 如果我没有使用任何构造函数怎么办, 如:

1 #include <iostream> 2 #include <string> 3 using namespace std; 4 class Student 5 { 6 string name; 7 public: 8 Student(string); 9 string GetName(); 10 }; 11 Student::Student(string name) 12 { 13 this->name=name; 14 } 15 string Student::GetName() 16 { 17 return name; 18 } 19 int main() 20 { 21 Student s;//Error!!! 22 cout<<s.GetName()<<endl; 23 }

报错:

[Error] no matching function for call to ‘Student::Student()‘

也就是说, 变量 s 的构造函数已经不存在了, 那么就不符合类的声明规则 (看上文), 自然就会报错

那怎么办? ----- 自己写出系统默认的构造函数

C类的构造函数是如何实现其初始化过程的?

先认识一下该关键字:

default

这个大家肯定很熟悉, 但是很少用在除了 switch 语句中的其他地方

我们只需要显示出系统默认的构造函数:

1 #include <iostream> 2 #include <string> 3 using namespace std; 4 class Student 5 { 6 string name; 7 public: 8 Student()=default;//Do nothing... 9 Student(string); 10 string GetName(); 11 }; 12 Student::Student(string name) 13 { 14 this->name=name; 15 } 16 string Student::GetName() 17 { 18 return name; 19 } 20 int main() 21 { 22 Student s; 23 cout<<s.GetName()<<endl; 24 }

这下就正常运行了... (然而结果是什么都没有, 因为 name 根本没有被赋值)

有的时候还需要多重的构造函数, 如:

1 class Student 2 { 3 int ID; 4 string name; 5 public: 6 Student(int);//Set student ID 7 Student(string);//Set student name 8 int GetID(); 9 string GetName(); 10 }; 11 Student::Student(int ID) 12 { 13 this->ID=ID; 14 } 15 Student::Student(string name) 16 { 17 this->name=name; 18 } 19 string Student::GetName() 20 { 21 return name; 22 } 23 int Student::GetID() 24 { 25 return ID; 26 }

这时候构造时, 系统会根据给出的数据自动选择适合的构造函数:

#include <iostream> #include <string> using namespace std; class Student { int ID; string name; public: Student(int); Student(string); int GetID(); string GetName(); }; Student::Student(int ID) { this->ID=ID; } Student::Student(string name) { this->name=name; } string Student::GetName() { return name; } int Student::GetID() { return ID; } int main() { Student s(8);//Set student ID cout<<s.GetID(); return 0; }

或:

#include <iostream> #include <string> using namespace std; class Student { int ID; string name; public: Student(int); Student(string); int GetID(); string GetName(); }; Student::Student(int ID) { this->ID=ID; } Student::Student(string name) { this->name=name; } string Student::GetName() { return name; } int Student::GetID() { return ID; } int main() { Student s("TweeChalice"); cout<<s.GetName(); return 0; }

本文共计1292个文字,预计阅读时间需要6分钟。

C类的构造函数是如何实现其初始化过程的?

在上一章中,初步介绍了类的部分内容。必须熟练掌握类的内容与结构体相同,好了。当我们创建一个类时,有时需要初始化,这时就需要构造函数。构造函数的格式为:`C`

上一章中, 初步的介绍了类的部分内容, 必须熟练掌握

部分内容与结构体相同

好了, 当我们创建一个类时, 有的时候需要初始化, 这时候就需用到构造函数

构造函数的格式为:

<ClassType>(<List>) { //Do something... }

<ClassType> 为类的名字

<List> 为参数列表

在类创建的时候, 都会调用构造函数

但是有的时候不写自己的构造函数的话, 系统会调用默认的构造函数 (也就是什么都不做)

但是当写了构造函数后, 系统就不会调用默认的构造函数

构造函数的声明必须写在类中, 但是实现可以写在外头 (如果不知道如何将成员函数外部实现的看这里):

1 class Student 2 { 3 string name; 4 public: 5 Student(string) 6 }; 7 Student::Student(string name) 8 { 9 this->name=name;//这里可以直接访问类的成员 10 }

备注: 在第9行用到了 this 指针, 不明白的看这里

这样一来, 在声明类的时候, 可以调用构造函数, 其格式为:

<ClassType> 变量名(<List>)

<List> 指该类的构造函数的参数列表

构造函数必须这样写, 不得写在声明后的其余地方

1 #include <iostream> 2 #include <string> 3 using namespace std; 4 class Student 5 { 6 string name; 7 public: 8 Student(string); 9 string GetName(); 10 }; 11 Student::Student(string name) 12 { 13 this->name=name; 14 } 15 string Student::GetName() 16 { 17 return name; 18 } 19 int main() 20 { 21 Student s; 22 s.Student("TweeChalice");//Error!!! 23 cout<<s.GetName()<<endl; 24 }

必须写成这样:

1 #include <iostream> 2 #include <string> 3 using namespace std; 4 class Student 5 { 6 string name; 7 public: 8 Student(string); 9 string GetName(); 10 }; 11 Student::Student(string name) 12 { 13 this->name=name; 14 } 15 string Student::GetName() 16 { 17 return name; 18 } 19 int main() 20 { 21 Student s("TweeChalice"); 22 cout<<s.GetName()<<endl; 23 }

现在又有一个问题了, 如果我没有使用任何构造函数怎么办, 如:

1 #include <iostream> 2 #include <string> 3 using namespace std; 4 class Student 5 { 6 string name; 7 public: 8 Student(string); 9 string GetName(); 10 }; 11 Student::Student(string name) 12 { 13 this->name=name; 14 } 15 string Student::GetName() 16 { 17 return name; 18 } 19 int main() 20 { 21 Student s;//Error!!! 22 cout<<s.GetName()<<endl; 23 }

报错:

[Error] no matching function for call to ‘Student::Student()‘

也就是说, 变量 s 的构造函数已经不存在了, 那么就不符合类的声明规则 (看上文), 自然就会报错

那怎么办? ----- 自己写出系统默认的构造函数

C类的构造函数是如何实现其初始化过程的?

先认识一下该关键字:

default

这个大家肯定很熟悉, 但是很少用在除了 switch 语句中的其他地方

我们只需要显示出系统默认的构造函数:

1 #include <iostream> 2 #include <string> 3 using namespace std; 4 class Student 5 { 6 string name; 7 public: 8 Student()=default;//Do nothing... 9 Student(string); 10 string GetName(); 11 }; 12 Student::Student(string name) 13 { 14 this->name=name; 15 } 16 string Student::GetName() 17 { 18 return name; 19 } 20 int main() 21 { 22 Student s; 23 cout<<s.GetName()<<endl; 24 }

这下就正常运行了... (然而结果是什么都没有, 因为 name 根本没有被赋值)

有的时候还需要多重的构造函数, 如:

1 class Student 2 { 3 int ID; 4 string name; 5 public: 6 Student(int);//Set student ID 7 Student(string);//Set student name 8 int GetID(); 9 string GetName(); 10 }; 11 Student::Student(int ID) 12 { 13 this->ID=ID; 14 } 15 Student::Student(string name) 16 { 17 this->name=name; 18 } 19 string Student::GetName() 20 { 21 return name; 22 } 23 int Student::GetID() 24 { 25 return ID; 26 }

这时候构造时, 系统会根据给出的数据自动选择适合的构造函数:

#include <iostream> #include <string> using namespace std; class Student { int ID; string name; public: Student(int); Student(string); int GetID(); string GetName(); }; Student::Student(int ID) { this->ID=ID; } Student::Student(string name) { this->name=name; } string Student::GetName() { return name; } int Student::GetID() { return ID; } int main() { Student s(8);//Set student ID cout<<s.GetID(); return 0; }

或:

#include <iostream> #include <string> using namespace std; class Student { int ID; string name; public: Student(int); Student(string); int GetID(); string GetName(); }; Student::Student(int ID) { this->ID=ID; } Student::Student(string name) { this->name=name; } string Student::GetName() { return name; } int Student::GetID() { return ID; } int main() { Student s("TweeChalice"); cout<<s.GetName(); return 0; }