[英]I'm trying to learn how to properly separate class header and code
我有一個大學時期的班級項目,該項目“有效”,但結構不正確。
它具有Date類和Person類。
在我的“工作”代碼中,所有類數據(構造函數,成員和函數)都包含在每個類的單個頭文件中(每個類一個單獨的文件)。
該程序將文本文件數據(人員,總裁和演員)加載到矢量中,在控制台上對數據進行排序,過濾和打印,然后將其保存到文件中。
在Person類的構造函數(和函數)中,為“ birthdate”對象實例化Date類。 然后在程序中使用生日。
我遇到的問題是:
如果所有類代碼都在每個類的頭文件中,則可以輕松地在Person構造函數和成員中實例化“ Date”,並且將Date設為Person的“ Parent”類。 但是,如果我不做這兩個事情,那么“ Person”對“ Date”一無所知,並且無法實例化Date中的任何內容。
如果我在Person.h中使用#include“ Date.h”,這會產生更多問題,並且也不起作用。
當然,“ Date”不是“ Person”的適當父類,但這是我可以修改代碼使其大聲笑的唯一方法。 多年前,當我在大學里第一次編寫代碼時,我從未想過如何正確地划分類代碼,以使其能夠編譯和運行。 頭文件中所有類代碼的“工作代碼”是我的hack。 我想學習正確的方法。
我在下面粘貼了最簡單的示例,說明什么“有效”和什么無效。 使該代碼與分為頭文件和.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。
為防止重新定義錯誤,請在頭文件中添加包含防護。 為了便於攜帶,請使用:
#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文件中,任何#include之后都可以使用“使用命名空間XXXXX”)
您有2個問題恕我直言。
首先是斯科特·朗厄姆的答案將解決的警衛問題。
第二個是名稱空間問題。
我認為在您的工作源中,您(直接通過stdafx.h或另一個include間接在)上:
#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(字符串-> std :: string並刪除方法實現)
#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
因此它實際上應該有一個防護裝置-為簡單起見,此處未顯示
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.