C ++ friends, constants, static members

// Header file
#ifndef STUDENT_H
#define STUDENT_H

class Student
{
public:
    friend void Print_Student ();   // Friend declaration 
    Student ( const  int m_student_id, const  char * m_student_name); // 
    Student with parameter construction ( const Student & other); // Copy construction 
private :
     void printStudentInfo (); // Output student Information 
    void SetStudentName ( const  char * m_student_name); // Set student name 
    static  int School_Id; // Define school id 
    const  int StudentId;    // Define student id
    char * StudentName; // define student name 
};
 int Student :: School_Id = 1 ; // static members in the class need to be initialized outside the class

void Print_Student(); 


#endif

// cpp file

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include "Student.h"
using namespace std;
//带参构造
Student::Student(int m_student_id, const char* m_student_name) :StudentId(m_student_id)
{
    this->StudentName = new char[strlen(m_student_name) + 1];
    strcpy(this->StudentName, m_student_name);
}
//拷贝构造
Student::Student(const Student& other) :StudentId(other.StudentId)
{
    this->StudentName = new char[strlen(other.StudentName) + 1];
    strcpy(this->StudentName, other.StudentName);
}
// print student information 
void Student :: printStudentInfo ()
{
    cout << "学校id:" <<School_Id<< "\t学生学号id:"<<StudentId << "\t学生姓名:"<<StudentName << endl;
}
// Set student name 
void Student :: SetStudentName ( const  char * m_student_name)
{
    strcpy(this->StudentName, m_student_name);
}
void Print_Student()
{
    Student temp1(1, "小明");    
    temp1.printStudentInfo ();    // print student information 1 
    temp1.SetStudentName ( " Xiao Gang " );

    Student temp2(temp1);
    temp2.printStudentInfo (); // print student 2 information 
}

int main ()
{
    Print_Student();
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/shenji/p/12680528.html