具体是这样的,假设有父类person,子类student,teacher,worker,均是公有继承
我实现顺序如下,我主要是想问文件之间的编译问题:
person.h
person.cpp
student.h
student.cpp
teacher.h
teacher.cpp
worker.h
worker.cpp
我用的是#ifndef语句,每个文件我大体说一下吧:
//在person.h中//
#ifndef Person
#define Personclass Person
{};
#endif
//在person.cpp中//
#include <iostream>
#include <string.h>
#include "Person.h"
using namespace std;
函数...//在student.h中//
#ifdef Person
#define Student
class Student 
{ };
#endif//在student.cpp中//
#include <iostream>
#include <string.h>
#include "Person.h"
#include "Student.h"
using namespace std;
函数...
//在teacher.h中//
#ifdef Person
#define Teacher
class Teacher
{};
#endif//在teacher.cpp//
#include <iostream>
#include <string.h>
#include "Person.h"
#include "Teacher.h"
using namespace std;
函数...//在worker.h//
#ifdef Person
#define Worker
class Worker
{};
#endif//在Worker.cpp中//
#include <iostream>
#include <string.h>
#include "Person.h"
#include "Worker.h"
using namespace std;
函数...//在main.cpp中//
#include <iostream>
#include <string.h>
#include "Person.h"
#include "Student.h"
#include "Teacher.h"
#include "Worker.h"
using namespace std;void Myfun(Person &ps)
{
ps.Display();
}void main()
{
Student st("dimmy",20,"221010");
Myfun(st);
}
我知道一定有头文件重定义了,还有在工程中要添加文件吗???
急,请GGJJ帮忙,单文件执行没问题的,多文件怎么???

解决方案 »

  1.   

    单文件的源程序如下:
    #include <iostream>
    #include <string.h>
    using namespace std;class Person
    {
    public:
    Person(char*,int,char*);
    static Person *First();               //返回数据为静态成员
    Person *Next();
    virtual void Display()=0;
    ~Person(){}
    protected:
    static Person *pFirst;
    Person *pNext;
    char name[20];
    int age;
    char ID[20];
    };class Student:public Person
    {
    public:
    Student(char *,int,char *);
    virtual void Display();
    ~Student(){}
    protected:
    int grade;
    int average;
    };class Teacher:public Person
    {
    public:
    Teacher(char *,int,char *);
    virtual void Display();
    ~Teacher(){}
    protected:
    int salary;
    };class Worker:public Person
    {
    public:
    Worker(char *,int,char *);
    virtual void Display();
    ~Worker(){}
    protected:
    int wage;
    };Person *Person::pFirst=0;Person::Person(char *pName,int pAge,char *pID)
    {
    strncpy(name,pName,sizeof(name));
    name[sizeof(name)-1]='\0';
    strncpy(ID,pID,sizeof(ID));
    ID[sizeof(ID)-1]='\0';
    age=pAge;
    pNext=0; if(pFirst==0)
    pFirst=this;
    else
    {
    Person *ps=pFirst;
    while (ps->pNext)
    {
    ps=ps->pNext;
    }
    ps->pNext=this;
    }
    }Person *Person::First()
    {
    return pFirst;
    }Person *Person::Next()
    {
    return pNext;
    }void Person::Display()
    {
    return;
    }Student::Student(char *pName,int pAge,char *pID):Person(pName,pAge,pID)
    {
    cout<<"Constructing a student: "<<name<<endl;
    //grade=new int;
    cout<<"Input grade: "<<endl;
    cin>>grade;
    //average=new int;
    cout<<"Input average: "<<endl;
    cin>>average;
    }void Student::Display()
    {
    cout<<"name: "<<name<<endl;
    cout<<"age: "<<age<<endl;
    cout<<"ID: "<<ID<<endl;
    cout<<"grade: "<<grade<<endl;
    cout<<"average: "<<average<<endl;
    }
    Teacher::Teacher(char *pName,int pAge,char *pID):Person(pName,pAge,pID)
    {
    cout<<"Constructing a teacher: "<<name<<endl;
    //salary=new int;
    cout<<"Input salary: "<<endl;
    cin>>salary;
    }void Teacher::Display()
    {
        cout<<"name: "<<name<<endl;
    cout<<"age: "<<age<<endl;
    cout<<"ID: "<<ID<<endl;
    cout<<"salary: "<<salary<<endl;
    }Worker::Worker(char *pName,int pAge,char *pID):Person(pName,pAge,pID)
    {
    cout<<"Constructing a worker: "<<name<<endl;
    //wage=new int;
    cout<<"Input wage: "<<endl;
    cin>>wage;
    }void Worker::Display()
    {
        cout<<"name: "<<name<<endl;
    cout<<"age: "<<age<<endl;
    cout<<"ID: "<<ID<<endl;
    cout<<"wage: "<<wage<<endl;
    }void Myfun(Person &ps)
    {
    ps.Display();
    }void main()
    {
    Student st("dimmy",20,"221010");
    Myfun(st);
    Teacher te("Mr Wu",24,"22");
    Myfun(te);
    Worker wo("Miss li",26,"22");
    Myfun(wo);
    }
      

  2.   

    //在student.h中//
    #include person.h
    #ifndef Student
    #define Studentclass Student : public Person
    { };
    #endif//在student.cpp中//
    #include <iostream>
    #include <string.h>
    #include "Student.h"
    using namespace std;
    函数...另,#define的东东最好全部大写,为了防止多次编译一般如下:#ifndef PERSON_H
      

  3.   

    TO: Mackz(在相互) 
    =================
    #pragma once 
    是 Microsoft Visual Studio .NET 才有的吧
      

  4.   

    搞定了
    搞定了
    搞定了
    多谢nlstone