一般将类的声明放在.h文件中,类中成员函数的定义放在.cpp文件中 /*person.h*/ # ifndef __PERSON_H__ # define __PERSON_H__ # include <iostream> using namespace std; class person { private : int age; string name; public : person ( int age, const string& name) ; void whoami ( void ) ; } ; # endif /*person.cpp*/ # include "person.h" person:: person ( int age, const string& name) { this -> age= age; this -> name= name; } void person:: whoami ( void ) { cout<< "我是: " << name<< endl; } # ifndef __STUDENT_H__ # define __STUDENT_H__ # include "person.h" class student : public person { private : float score; public : student ( int age, const string& name, float score) ; void whoami ( void ) ; } ; # endif /*student.cpp*/ # include "student.h" student:: student ( int age, const string& name, float score) : person ( age, name) { this -> score= score; } void student:: whoami ( void ) { person:: whoami ( ) ; cout<< "我的成绩是: " << score<< endl; } /*main.cpp*/ # include "student.h" int main ( void ) { students1 ( 22 , "刘备" , 81.5 ) ; s1. whoami ( ) ; return 0 ; } g++ person.cpp student.cpp main.cpp ./a.out