// ヘッダーファイル #ifndef STUDENT_H #define STUDENT_H class Student { public : friend void Print_Student(); // フレンド宣言 Student(const int m_student_id、const char * m_student_name); // パラメータを使用して 生徒を構築(const Student&other); //構造をコピーprivate : void printStudentInfo(); // 生徒の情報を出力void SetStudentName(const char * m_student_name); // 生徒の名前を設定 static int School_Id; // 学校IDを定義する const int StudentId; // 学生IDを定義する char * StudentName; // 学生名を定義する }; int Student :: School_Id = 1 ; // クラスの静的メンバーはクラスの外部で初期化する必要がある void Print_Student(); #endif
// cppファイル
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include " Student.h " using namespace std; // 带参构造 学生::学生(int型 m_student_id、CONST のchar * m_student_name):StudentId(m_student_id) { この - > StudentName = 新しい CHAR [STRLEN(m_student_name)+ 1 ]。 strcpyの(この - > StudentName、m_student_name)。 } // コピー贝构造 Student :: Student(const Student&OTHER):StudentId(other.StudentId) { この - > StudentName = 新しい新しい CHAR [STRLEN(other.StudentName)+ 。1 ]; strcpyの(この - > StudentName、other.StudentName); } // 学生情報印刷 ボイド学生:: printStudentInfo() { cout << " School id:" << School_Id << " \ tStudent ID:" << StudentId << " \ tStudent name:" << StudentName << endl; } // 学生を設定名前 void学生::SetStudentName( const CHAR * m_student_name) { strcpyの(この - > StudentName、m_student_name); } ボイドPrint_Student() { 学生TEMP1(1、" ボブ" ); temp1.printStudentInfo(); // プリント学生情報1 temp1.SetStudentName(「スモールJust " ); Student temp2(temp1); temp2.printStudentInfo(); // 学生2の情報を印刷 } int main() { Print_Student(); return 0 ; }