如何在将 类 分成 .h 和 .cpp 文件时使用聚合?
how can i use aggregation while separating my classes into a .h and .cpp files?
上下文
我的教授给了我一个任务,让我使用 2 个 classes 之间的聚合来制作一个程序,同时将 classes 分成 .h
和 .cpp
文件。
我的解决方案
包含 class 声明的头文件:
#include <iostream>
#include <string>
using namespace std;
class medicalCompany {
private:
string ceoName;
string email;
string phoneNumber;
string locate;
public:
medicalCompany();
void Name(string n);
void mail(string m);
void phone(string p);
void location(string l);
~medicalCompany();
};
class origin {
private:
medicalCompany country;
public:
origin();
void address();
~origin();
};
和我的 .cpp 文件:
#include <iostream>
#include "function.h"
#include <string>
using namespace std;
medicalCompany::medicalCompany() {
cout << "OUR COMPANY IS GLAD TO BE OF SERVICE !" << endl;
cout << "****************************************************" << endl;
}
void medicalCompany::Name(string n){
ceoName = n;
cout << "OUR CEO IS " << endl;
cout<< ceoName << endl;
cout << "****************************************************" << endl;
}
void medicalCompany::mail(string m) {
email = m;
cout << "USE OUR EMAIL TO CONTACT US : " << endl;
cout<< email << endl;
cout << "****************************************************" << endl;
}
void medicalCompany::phone(string p) {
phoneNumber = p;
cout << "THIS IS OUR CUSTOMER SERVICE PHONE NUMBER " << endl;
cout<< phoneNumber << endl;
cout << "****************************************************" << endl;
}
void medicalCompany::location(string l) {
locate = l;
cout << " OUR COMPANY IS LOCATED IN " << endl;
cout << locate << endl;
cout << "****************************************************" << endl;
}
medicalCompany::~medicalCompany() {
cout << "thanks for trusting our company ^_^" << endl;
cout << "****************************************************" << endl;
}
origin::origin() {
cout<< "constructor 2"<<endl;
}
void origin::address() {
cout << country.location;
}
origin::~origin() {
cout << "bye" << endl;
}
我的 main.cpp
文件中使用了两个 class:
#include <iostream>
#include <string>
#include "function.h"
using namespace std;
int main() {
medicalCompany o;
o.Name("jack");
o.mail("ouremail@company.com");
o.phone("2342352134");
o.location("Germany");
origin o2;
return 0;
}
问题
我运行进入这个错误:
Severity Code Description Project File Line Suppression State
Error C3867 'medicalCompany::location': non-standard syntax; use '&' to create a pointer to member CP2_HW c:\function.cpp 41
您可以:
将void origin::address(){cout << country.location;}
替换为void origin::address(){country.location();}
或 by void origin::address(){cout << country.locate;}
如果您将 locate
成员作为 public 变量。
此外,几点评论:
通常您希望避免暴露私有成员,因此应该首选第一种解决方案。
指令 using namespace std;
通常被认为是不好的做法,应该避免,因为可能的风险成本不会超过不必键入 std::cout
(有关详细信息,请参阅 this question)
在命名约定方面,我会交换 locate
和 location
的名称:位置应该是成员变量,locate
是动作(函数)获取位置。
更喜欢使用构造函数和初始化列表而不是 getter/setter。
您的输出格式应该与您的 classes 的逻辑完全分开,例如为您的 class.[=24 实现一个 <<
运算符=]
按照这个逻辑,您的代码应该更像:
#include <iostream>
#include <string>
class medicalCompany {
private:
std::string _ceoName;
std::string _email;
std::string _phoneNumber;
std::string _location;
public:
// Constructor
medicalCompany(std::string name, std::string email, std::string phone, std::string location):
_ceoName(name),
_email(email),
_phoneNumber(phone),
_location(location)
{}
friend ostream& operator<<(ostream& os, const medicalCompany& dt);
};
对于 ostream 运算符:
ostream& operator<<(ostream& os, const medicalCompany& co)
{
os << co._ceoName << " " co._phoneNumber << ' ' << co._email;
return os;
}
这将允许在您的主程序中编写这样的代码:
int main() {
medicalCompany o("jack", "ouremail@company.com","2342352134","Germany")
std::cout << o << std::endl;
return 0;
}
该代码不起作用,您必须对其进行编辑以满足您的格式要求,但您有想法:)祝您好运!
上下文
我的教授给了我一个任务,让我使用 2 个 classes 之间的聚合来制作一个程序,同时将 classes 分成 .h
和 .cpp
文件。
我的解决方案
包含 class 声明的头文件:
#include <iostream>
#include <string>
using namespace std;
class medicalCompany {
private:
string ceoName;
string email;
string phoneNumber;
string locate;
public:
medicalCompany();
void Name(string n);
void mail(string m);
void phone(string p);
void location(string l);
~medicalCompany();
};
class origin {
private:
medicalCompany country;
public:
origin();
void address();
~origin();
};
和我的 .cpp 文件:
#include <iostream>
#include "function.h"
#include <string>
using namespace std;
medicalCompany::medicalCompany() {
cout << "OUR COMPANY IS GLAD TO BE OF SERVICE !" << endl;
cout << "****************************************************" << endl;
}
void medicalCompany::Name(string n){
ceoName = n;
cout << "OUR CEO IS " << endl;
cout<< ceoName << endl;
cout << "****************************************************" << endl;
}
void medicalCompany::mail(string m) {
email = m;
cout << "USE OUR EMAIL TO CONTACT US : " << endl;
cout<< email << endl;
cout << "****************************************************" << endl;
}
void medicalCompany::phone(string p) {
phoneNumber = p;
cout << "THIS IS OUR CUSTOMER SERVICE PHONE NUMBER " << endl;
cout<< phoneNumber << endl;
cout << "****************************************************" << endl;
}
void medicalCompany::location(string l) {
locate = l;
cout << " OUR COMPANY IS LOCATED IN " << endl;
cout << locate << endl;
cout << "****************************************************" << endl;
}
medicalCompany::~medicalCompany() {
cout << "thanks for trusting our company ^_^" << endl;
cout << "****************************************************" << endl;
}
origin::origin() {
cout<< "constructor 2"<<endl;
}
void origin::address() {
cout << country.location;
}
origin::~origin() {
cout << "bye" << endl;
}
我的 main.cpp
文件中使用了两个 class:
#include <iostream>
#include <string>
#include "function.h"
using namespace std;
int main() {
medicalCompany o;
o.Name("jack");
o.mail("ouremail@company.com");
o.phone("2342352134");
o.location("Germany");
origin o2;
return 0;
}
问题
我运行进入这个错误:
Severity Code Description Project File Line Suppression State
Error C3867 'medicalCompany::location': non-standard syntax; use '&' to create a pointer to member CP2_HW c:\function.cpp 41
您可以:
将
void origin::address(){cout << country.location;}
替换为void origin::address(){country.location();}
或
by void origin::address(){cout << country.locate;}
如果您将locate
成员作为 public 变量。
此外,几点评论:
通常您希望避免暴露私有成员,因此应该首选第一种解决方案。
指令
using namespace std;
通常被认为是不好的做法,应该避免,因为可能的风险成本不会超过不必键入std::cout
(有关详细信息,请参阅 this question)在命名约定方面,我会交换
locate
和location
的名称:位置应该是成员变量,locate
是动作(函数)获取位置。更喜欢使用构造函数和初始化列表而不是 getter/setter。
您的输出格式应该与您的 classes 的逻辑完全分开,例如为您的 class.[=24 实现一个
<<
运算符=]
按照这个逻辑,您的代码应该更像:
#include <iostream>
#include <string>
class medicalCompany {
private:
std::string _ceoName;
std::string _email;
std::string _phoneNumber;
std::string _location;
public:
// Constructor
medicalCompany(std::string name, std::string email, std::string phone, std::string location):
_ceoName(name),
_email(email),
_phoneNumber(phone),
_location(location)
{}
friend ostream& operator<<(ostream& os, const medicalCompany& dt);
};
对于 ostream 运算符:
ostream& operator<<(ostream& os, const medicalCompany& co)
{
os << co._ceoName << " " co._phoneNumber << ' ' << co._email;
return os;
}
这将允许在您的主程序中编写这样的代码:
int main() {
medicalCompany o("jack", "ouremail@company.com","2342352134","Germany")
std::cout << o << std::endl;
return 0;
}
该代码不起作用,您必须对其进行编辑以满足您的格式要求,但您有想法:)祝您好运!