介紹
類的分離式寫法,使得代碼更加規(guī)范,增強了閱讀性。
分離式寫法的規(guī)則:
1.類的變量:寫在類的里面
2.成員函數(shù):類中寫函數(shù)的聲明,函數(shù)的定義寫在類體外。
3.寫在類外函數(shù)定義時,類名前加限定(Object: :),其中,::理解為表示范圍的符號。
代碼演示
頭文件:Object.h
#ifndef _OBJECT_H_#define _OBJECT_H_class Student{private: char name[32]; int age;public: void SetName(char* pName); void SetAge(int age); void Printf_Stu();};#endif源文件:Object.cpp
#include <stdio.h>#include <string.h>#include "Object.h"void Student::SetName(char* pName){ strcpy(this->name, pName);}void Student::SetAge(int age){ this->age = age;}void Student::Printf_Stu(){ printf("name = %s, age = %d/n", this->name, this->age);}int main(){ Student stu; stu.SetName("aaaa"); stu.SetAge(30); stu.Printf_Stu(); return 1;}總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對武林網(wǎng)的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
新聞熱點
疑難解答