C ++-分离头文件和实现文件

时间:2018-10-14 22:45:00

标签: c++

因此,我已经创建了这段代码,但是我很难分离文件。到目前为止,当我创建头文件时,它运行良好,但是当我为dctorType,patientType和billType创建.cpp文件时,它说未定义,头文件personType被重新定义。请帮助

#include <iostream>

#include <string>

using namespace std;

// Base class personType

class personType

{

public:

void print()const;

//Function to output the first name and last name

//in the form firstName lastName.

void setName(string first, string last);



string getFirstName()const;

string getLastName()const;

personType(string first = "", string last = "");

//Constructor

//Sets firstName and lastName according to the parameters.

//The default values of the parameters are null strings.

//Postcondition: firstName = first; lastName = last  

private:

string firstName; //variable to store the first name

string lastName; //variable to store the last name

};

void personType::print() const

{

cout << "Person FirstName="<<firstName << " LastName=" << lastName<< endl;

}

void personType::setName(string first, string last)

{

firstName = first;

lastName = last;

}

string personType::getFirstName() const

{

return firstName;

}

string personType::getLastName() const

{

return lastName;

}

//constructor

personType::personType(string first, string last)

{

firstName = first;

lastName = last;

}

// --------------------Start your code from here

class doctorType: public personType

{

public:

// doctorType(string first,string last,string special);

void print()const;

void setSpeciality(string special);

string getSpeciality() const;

doctorType(string first, string last, string special);

//private:

string firstName; //variable to store the first name

string lastName;

string speciality;

};

void doctorType::print() const

{

cout << "Doctor FirstName="<<firstName << " LastName=" << lastName<< " Speciality="<<speciality<<endl;

}   

void doctorType::setSpeciality(string special)

{

speciality = special;

}

string doctorType::getSpeciality() const

{

return speciality;

}

doctorType::doctorType(string first, string last,string special)

{

firstName = first;

lastName = last;

speciality = special;

}

class patientType:public personType

{

public:

patientType(string first="", string last = "", int id1=0,int age1=0,int dob1=0);

void setId(int id1);

void setAge(int age1);

void setDob(int dob1);

int getId() const;

int getAge() const;

int getDob() const;

void print() const;

//private:

string firstName;

string lastName;

int id;

int age;

int dob;

};

patientType::patientType(string first,string last,int id1,int age1,int dob1)

{

firstName = first;

lastName=last;

id = id1;

age = age1;

dob = dob1;

}

void patientType::setId(int id1)

{

id = id1;

}

int patientType::getId() const

{

return id;

}

void patientType::setAge(int age1)

{

age = age1;

}

int patientType::getAge() const

{

return age;

}

void patientType::setDob(int dob1)

{

dob=dob1;

}

int patientType::getDob() const

{

return dob;

}

void patientType::print() const

{

cout << "Patient FirstName="<<firstName << " LastName=" << lastName<< " Id="<<id<<" Age="<<age<<" Dob="<<dob<<endl;

}  

class billType

{

public:

billType(doctorType &d,patientType &p);

void setCharge(double chrg);

double getCharge() const;

void print() const;

private:

double charge;

string patient_first;

string patient_last;

int patient_Id;

int patient_Age;

int patient_Dob;

string doctor_first;

string doctor_last;

string doctor_speciality;

};

billType::billType(doctorType &d, patientType &p)

{

//double charge;

patient_first = p.firstName;

patient_last = p.lastName;

patient_Id = p.id;

patient_Age = p.age;

patient_Dob = p.dob;

doctor_first = d.firstName;

doctor_last = d.lastName;

doctor_speciality = d.speciality;

}

void billType::setCharge(double chrg)

{

charge = chrg;

}

double billType::getCharge() const

{

return charge;

}

void billType::print() const

{

cout << "Patient FirstName="<<patient_first << " LastName=" << patient_last<< " Id="<<patient_Id<<" Age="<<patient_Age<<" Dob="<<patient_Dob<<endl;

cout << "patient's Doctor FirstName="<<doctor_first << " LastName=" << doctor_last<< " Speciality="<<doctor_speciality<<endl;

cout<<"Hospital charges="<<charge<<endl;

}

//--------------------driver program

int main()

{



personType person1("Lisa", "Regan");

doctorType doctor1("Sarah", "Conner", "Dentist");

patientType patient1("Sam", "Fire",200,100,1916);

billType b1(doctor1, patient1);

b1.setCharge(250.66);

cout << "<personType> Printing...\n";

person1.print();

cout << endl;

cout << "<doctorType> Printing...\n";

doctor1.print();

cout << endl;

cout << "<patientType> Printing...\n";

patient1.print();

cout << endl;

cout << "<billType> Printing...\n";

b1.print();

cout << endl;

return 0;

}

1 个答案:

答案 0 :(得分:0)

personType标头如下:

#ifndef PERSONTYPE_H
#define PERSONTYPE_H
#include <string>


using namespace std;

class personType
{
public:

    void print()const;
    //Function to output the first name and last name
    ////in the form firstName lastName.
    void setName(string first, string last);
    string getFirstName()const;
    string getLastName()const;
    personType(string first = "", string last = "");
    ////Constructor
    ////Sets firstName and lastName according to the parameters.
    ////The default values of the parameters are null strings.
    //Postcondition: firstName = first; lastName = last
    string firstName; //variable to store the first name
    string lastName; //variable to store the last name

};
#endif

我所做的唯一更改是将firstName和lastName公开,而不是私有。 (并添加了标头后卫,这非常重要。)

我不会显示每个文件,因为它们几乎相同,我将让您完成工作。 这是BillType.h:

  #ifndef BILLTYPE_H
    #define BILLTYPE_H
    #include <string>
    #include "patientType.h"
    #include "doctorType.h"

    class billType

    {

    public:

        billType(doctorType &d,patientType &p);
        void setCharge(double charge);
        double getCharge() const;
        void print() const;

    private:

        double charge;
        string patient_first;
        string patient_last;
        int patient_Id;
        int patient_Age;
        int patient_Dob;
        string doctor_first;
        string doctor_last;
        string doctor_speciality;

    };

    #endif

由于我们同时包括了患者和医生,又包括了人,因此我们只需要在驱动程序中包含billType.h。 该驱动程序几乎与您现在拥有的驱动程序相同。在驱动程序的方法“ billType”中,我建议您使用getter而不是直接调用变量。我还建议您在构造函数中使用初始化器列表。

要记住的重要一点是,如果要包括自定义头文件,请使用#include "filename.h"