具体是这样的,假设有父类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帮忙,单文件执行没问题的,多文件怎么???
我实现顺序如下,我主要是想问文件之间的编译问题:
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帮忙,单文件执行没问题的,多文件怎么???
解决方案 »
- 求助:IOleObjectImpl::GetMiscStatus: Not implemented
- 如何把若干个机器的地址,配置到一个多播地址里面?
- MFC中,怎样拦下所有的窗口绘制的操作?
- 有关TreeCtrl 与ListCtrl 之间数据交换? 十万火急!!!
- listcontrol单击问题..??
- 有谁知道可以下载整个网站的工具?
- @@@@@ 本姑娘有难,哪位高手出招! 关于<<短时傅里叶变换程序>> @@@@@
- 请介绍一本比较好一点的计算机体系结构方面的书
- 十万火急!ZyXEL omni.net PLUS V4.18是一个什么样的东西!
- 在VB VC 中跑的很好的COM 为什么在delphi 中就不行了?
- #define WM_COMM WM_USER+100,是什么意思?是更改消息映射的什么?不懂
- 如何定义在VC中定义全局公用变量和全局公用函数?
#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);
}
#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
=================
#pragma once
是 Microsoft Visual Studio .NET 才有的吧
搞定了
搞定了
多谢nlstone