C++ カスタム名前空間、コンストラクター、デストラクター
名前空間
.h ヘッダー ファイル
注: .h ヘッダー ファイルでの山括弧 < > と二重引用符 " " の使用の違いは、
ヘッダー ファイルの検索パスが異なることです。
山括弧 < > を使用すると、コンパイラはヘッダーを検索します。 file (.h );
二重引用符 " " で囲むと、コンパイラはまず現在のディレクトリでヘッダー ファイルを検索し、見つからない場合はシステム パスに移動して検索します。
(つまり、二重引用符を使用すると、山括弧を使用した場合よりも検索パスが 1 つ多く追加されます)
#ifndef SWL_CONSTRUCTOR_
#define SWL_CONSTRUCTOR_
#include <iostream>
#include <string>
using namespace std;
/*********************************************
* 命名空间:College_Student 大学生
* 类:Electronic_information 电子信息
公有的:姓名
私有的:年龄、分数
* 类:Software_engineering 软件工程
公有的:姓名
私有的:年龄、分数
*********************************************/
namespace College_Student {
class Electronic_information {
public:
char *m_name;
Electronic_information();
Electronic_information(char *name, int age, float score);
~Electronic_information();
private:
int m_age;
float m_score;
};
class Software_engineering {
public:
char *m_name;
Software_engineering();
~Software_engineering();
private:
int m_age;
float m_score;
};
}
#endif
コンストラクターとデストラクター
C++ には、クラス名と同じ名前で戻り値がなく、ユーザーが呼び出す必要のない特別なメンバー関数があり、この関数はオブジェクトの作成時に自動的に実行されます。この特別なメンバー関数がコンストラクター (Constructor) です。詳細については、.h ヘッダー ファイルを参照してください。パラメーターの有無にかかわらず、定義と具体的な実装。
プログラム
swl-constructor.cpp のコードは次のとおりです (例)。
#include "swl-constructor.h"
using namespace College_Student;
Electronic_information::Electronic_information()
{
cout << "Electronic_information 无参构造函数执行" << endl;
}
Electronic_information::Electronic_information(char *name, int age, float score)
{
m_name = name;
m_age = age;
m_score = score;
cout << "Electronic_information 有参构造函数执行 姓名: " << name << " 年龄: " << age << endl;
}
Electronic_information::~Electronic_information()
{
cout << "Electronic_information 析构函数执行" << endl;
}
Software_engineering::Software_engineering()
{
cout << "Software_engineering 无参构造函数执行" << endl;
}
Software_engineering::~Software_engineering()
{
cout << "Software_engineering 析构函数执行" << endl;
}
main.c コードは次のとおりです (例)。
#include "swl-constructor.h"
using namespace std;
int main(int argc, char** argv)
{
char my_name[] = "小霖";
//无参构造
// College_Student::Electronic_information william;
College_Student::Software_engineering swl;
//有参构造
// College_Student::Electronic_information william("小林", 23, 90.5f); //报错:无法将参数 1 从“const char [5]”转换为“char *”
//解决方法:方法一:强制类型转换(char*) 方法二:声明变量char name[]再赋值,传参
// College_Student::Electronic_information william((char*)"小林", 23, 90.5f); //正常运行
College_Student::Electronic_information william(my_name, 23, 90.5f); //正常运行
cout << "Ending !!!" << endl;
system("pause");
return 0;
}
結果
コードは次のとおりです(例)。
Software_engineering 无参构造函数执行
Electronic_information 有参构造函数执行 姓名: 小霖 年龄: 23
Ending !!!
请按任意键继续. . .
Electronic_information 析构函数执行
Software_engineering 析构函数执行
要約する
パラメーターを渡すときは、パラメーターのデータ型に注意してください ("Xiaolin", 23, 90.5f); // エラー: パラメーター 1 を " const char [5] " から " char * " に変換できません
コンストラクター(Constructor)の名前はクラス名と同じで、オブジェクト作成時に実行され、初期化などによく使われます。
デストラクター名の前には ~ 記号が付きます。一般的に、割り当てられたメモリを解放したり、開いているファイルを閉じたりするために使用されます。
注: デストラクターにはパラメーターがなく、オーバーロードできないため、クラスはデストラクターを 1 つだけ持つことができます。
コンストラクターの関数本体でメンバー変数に値を 1 つずつ代入することも、初期化リストを使用することもできます。
//构造函数的初始化列表
Electronic_information::Electronic_information(char *name, int age, float score)
: m_name(name)
, m_age(age)
, m_score(score)
{
cout << "Electronic_information 有参构造函数执行 姓名: " << name << " 年龄: " << age << endl;
}