我正在尝试学习如何正确分离类头和代码

时间:2015-01-28 19:09:40

标签: c++ visual-studio-2012 windows-7-x64

我有一个我从大学做过的课程项目,#34;工作"但是没有正确构建。

它有一个Date类和一个Person类。

在我的工作中#34;代码,所有类数据(构造函数,成员和函数)都包含在每个类的单个头文件中(每个类的单独文件)。

程序将文本文件数据(人员,总统和演员)加载到矢量中,对控制台上的数据进行排序,过滤和打印,并将其保存到文件中。

在Person类构造函数(和函数)中,Date类被实例化为" birthdate"宾语。然后在节目中使用生日。

我遇到的问题是:

我可以很容易地实例化"日期"在Person构造函数和成员中,如果所有类代码都在每个类的头文件中,并且我将Date设为" Parent"人类。但如果我不做这两件事,那么" Person"我不知道"日期"并且无法从Date实例化任何内容。

如果我使用#include" Date.h"在Person.h中,这会产生更多问题,也无法发挥作用。

当然,"日期"对于" Person"来说,它不是一个合适的父类,但这是我可以破解代码使其工作的唯一方法。当我多年前第一次在大学编写代码时,我从未弄清楚如何正确划分类代码以便编译和运行。 "工作代码"在头文件中的所有类代码是我的黑客。我想学习如何以正确的方式做到这一点。

我已经粘贴了以下最简单的例子"工作"什么不是。任何使这个代码与分为header和.cpp文件的类一起工作的技巧都将非常受欢迎。

什么有效:

class Date {
    private:
        int month;
        int day;
        int year;

    public:
        Date::Date() {}
        virtual ~Date() {}
        Date( int aMonth, int aDay, int aYear ) {
            this->month = aMonth;
            this->year = aYear;
            this->day = aDay;
        }

        // "Getter" functions for the Date class
        int getMonth(){return this->month;}
        int getYear() {return this->year;}
        int getDay() {return this->day;}

        // "Setter" functions for the Date class
        void setDay( int aDay ){ this->day = aDay; }
        void setMonth( int aMonth )  { this->month = aMonth; }
        void setYear( int aYear ) { this->year = aYear; }
};

class Person : public Date{
    private:
        string title;
        string firstName;
        string lastName;
        Date birthDate;

    public:
        Person::Person() {}
        Person(string title, string firstName, string lastName, Date birthDay);
        virtual ~Person() {}

        //"Getter" functions for the Person class
        string getTitle() { return title; }
        string getFirstName() { return firstName; }
        string getLastName() { return lastName; }
        Date getBirthDay() { return birthDate; }

        //"Setter" functions for the Person class
        void setTitle(string Title) { this->title = Title; }
        void setFirstName(string fName) { this->firstName = fName; }
        void setLastName (string lName) { this->lastName = lName; }
        void setBirthday (Date aBirthday) { this->birthDate = aBirthday; }
};

什么不起作用(不编译):

Date.h

class Date {
    private:
        int month;
        int day;
        int year;
    public:
        Date();
        virtual ~Date() {}
        Date( int aMonth, int aDay, int aYear );

        //Getters and setters
        int getMonth();
        int getYear() ;
        int getDay();
        void setDay( int aDay );
        void setMonth( int aMonth ) ;
        void setYear( int aYear ) ;

};

Date.cpp

#include "Date.h"
Date::Date() {}
Date::Date( int aMonth, int aDay, int aYear ) {
    this->month = aMonth;
    this->year = aYear;
    this->day = aDay;
}

int Date::getMonth(){return this->month;}
int Date::getYear() {return this->year;}
int Date::getDay()  {return this->day;}

//"Setter" functions for the Date class
void Date::setDay( int aDay ){ this->day = aDay; }
void Date::setMonth( int aMonth )  { this->month = aMonth; }
void Date::setYear( int aYear ) { this->year = aYear; }

Person.h

#include <string>

class Person{
    private:
        string title;
        string firstName;
        string lastName;
        Date birthDate;

    public:
        //Person::Person() {}
        Person(string title, string firstName, string lastName, Date birthDay); 

        //"Getter" functions for the Person class
        string getTitle() { return title; }
        string getFirstName() { return firstName; }
        string getLastName() { return lastName; }
        Date getBirthDay() { return birthDate; }

        //"Setter" functions for the Person class
        void setTitle(string Title) { this->title = Title; }
        void setFirstName(string fName) { this->firstName = fName; }
        void setLastName (string lName) { this->lastName = lName; }
        void setBirthday (Date aBirthday) { this->birthDate = aBirthday; }
    };

Person.cpp

#include "Person.h"
#include <iostream>
using namespace std;

//Person class constructor with 0 arguments
Person::Person(string atitle, string afirstName, string alastName, Date abirthDay) {
    title = atitle,
          firstName = afirstName,
          lastName = alastName,
          birthday = abirthDay)
}

//"Getter" functions for the Person class
string Person::getTitle() { return title; }
string Person::getFirstName() { return firstName; }
string Person::getLastName() { return lastName; }
Date Person::getBirthDay() { return birthDate; }

//"Setter" functions for the Person class
void Person::setTitle(string Title) { this->title = Title; }
void Person::setFirstName(string fName) { this->firstName = fName; }
void Person::setLastName (string lName) { this->lastName = lName; }
void Person::setBirthday (Date aBirthday) { this->birthDate = aBirthday; }

操作系统说明

我在Windows 7 PC上使用MS Visual Studio 2012 Update 4.

2 个答案:

答案 0 :(得分:2)

要防止重新定义错误,请在头文件中添加包含防护。要便携,请使用:

#if !defined(THE_HEADER_NAME_H)
#define THE_HEADER_NAME_H
// Usual code and declarations here.
#endif

在Visual Studio中,您可以使用:

#pragma once
// Usual code and declarations here.

在标题中,不要写(在全局范围级别)

using namespace XXXXX

因为这会导致包括你的所有文件也使用他们可能不希望强加给他们的名称空间XXXXX。如果使用命名空间中的类型,请使用命名空间键入整个类型名称,因此请使用:

std::string getTitle()

(在cpp文件中,你可以在#includes之后使用'使用命名空间XXXXX')

答案 1 :(得分:1)

你有2个问题恕我直言。

首先是Scott Langham的答案将解决的警卫问题。

第二个是名称空间问题。

我认为在您的工作源中,您(直接通过stdafx.h或其他包括间接地直接):

#include <string>

using namespace std;

建议不要在using namespace std;标头文件中添加.h ...但这意味着您应该使用std::string代替string

date.h

class Date {
    private:
        int month;
        int day;
        int year;
    public:
        Date::Date() { }                                                //Date class constructor with 0 arguments
        virtual ~Date() {}                                              //Date class destructor
        Date( int aMonth, int aDay, int aYear ) {                       //Date class constructor with 3 arguments
            this->month = aMonth;                                       // Set the private member data with the argument data
            this->year = aYear;
            this->day = aDay;
            }       
        //"Getter" functions for the Date class
        int getMonth(){return this->month;}                             
        int getYear() {return this->year;}
        int getDay() {return this->day;} 

        //"Setter" functions for the Date class
        void setDay( int aDay ){ this->day = aDay; }                    
        void setMonth( int aMonth )  { this->month = aMonth; }  
        void setYear( int aYear ) { this->year = aYear; }

};

person.h(string - &gt; std :: string和remove method implementation)

#include <string>
#include "Date.h"

class Person : public Date{

private:                                                                //Private data members
    std::string title;
    std::string firstName;
    std::string lastName;
    Date birthDate;

public:
    Person::Person() {}                                                 //Person class constructor with 0 arguments
    Person(std::string title, 
        std::string firstName, 
        std::string lastName, 
        Date birthDay);                                                 //Person class constructor with 4 arguments

    virtual ~Person(){}                                                 //Person class destructor

    //"Getter" functions for the Person class
    std::string getTitle();
    std::string getFirstName();
    std::string getLastName();
    Date getBirthDay();

    //"Setter" functions for the Person class
    void setTitle(std::string Title);
    void setFirstName(std::string fName);
    void setLastName (std::string lName);
    void setBirthday (Date aBirthday);
};

person.cpp(已修复包含和构造函数)

#include <string>
#include "Person.h"
#include <iostream>
using namespace std;

//Person class constructor with 0 arguments
Person::Person(string atitle, string afirstName, string alastName, Date abirthDay) {
    title = atitle,
          firstName = afirstName,
          lastName = alastName,
          birthDate = abirthDay;
}

//"Getter" functions for the Person class
string Person::getTitle() { return title; }
string Person::getFirstName() { return firstName; }
string Person::getLastName() { return lastName; }
Date Person::getBirthDay() { return birthDate; }

//"Setter" functions for the Person class
void Person::setTitle(string Title) { this->title = Title; }
void Person::setFirstName(string fName) { this->firstName = fName; }
void Person::setLastName (string lName) { this->lastName = lName; }
void Person::setBirthday (Date aBirthday) { this->birthDate = aBirthday; }

它甚至可以在没有警卫的情况下使用,但无论如何,警卫都是一种很好的做法。

事实上,Date.h中包含Person.h,它应该有一个警卫 - 为简单起见,此处未显示

相关问题