简体   繁体   English

我如何在将我的类分成 ah 和 .cpp 文件时使用聚合?

[英]how can i use aggregation while separating my classes into a .h and .cpp files?

Context语境

My professor gave me a task to make a program using aggregation between 2 classes while also separating the classes into a .h and .cpp files.我的教授给了我一个任务,让我使用 2 个类之间的聚合来制作一个程序,同时将这些类分成.h.cpp文件。

My solution我的解决方案

The header file containing the class declaration:包含 class 声明的 header 文件:

#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();

};

and my.cpp file:和我的.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;
}

The two classes are used in my main.cpp file:这两个类在我的main.cpp文件中使用:

#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;
}

Problem问题

I run into this error:我遇到了这个错误:

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 

You can either:您可以:

  • replace void origin::address(){cout << country.location;} by void origin::address(){country.location();}void origin::address(){cout << country.location;}替换为 void void origin::address(){country.location();}

  • or by void origin::address(){cout << country.locate;} if you put the locate member as a public variable.或者by void origin::address(){cout << country.locate;}如果您将locate成员作为公共变量。

Also, few remarks:另外,几点说明:

  • Generally you would prefer avoiding exposing private members, so the first solution should be prefered.通常您希望避免暴露私有成员,因此应该首选第一种解决方案。

  • the instruction using namespace std; using namespace std; is usually considered bad practice, and should be avoided, as the cost of possible risks does not overweight the benefit of not having to type std::cout (see this question for more information)通常被认为是不好的做法,应该避免,因为可能的风险成本不会超过不必键入std::cout的好处(有关更多信息,请参见此问题

  • In terms of naming convention, I would have exchanged the names of locate and location : location should be the member variable and locate the action (function) of getting the location.在命名约定方面,我会交换locatelocation的名称: location 应该是成员变量和locate获取位置的动作(函数)。

  • Prefer using a constructor and intialization lists rather than getter/setter.更喜欢使用构造函数和初始化列表而不是 getter/setter。

  • Your output formatting should be very separate from the logic of your classes, for example implementing a << operator for your class.您的 output 格式应该与您的类的逻辑完全分开,例如为您的 class 实现<<运算符。

Following this logic, your code should look more like:按照这个逻辑,您的代码应该更像:

#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);
  };

and for the ostream operator:对于 ostream 运营商:

ostream& operator<<(ostream& os, const medicalCompany& co)
{
    os << co._ceoName << " " co._phoneNumber << ' ' << co._email;
    return os;
}

This would allows to write code like this in your main:这将允许在您的 main 中编写这样的代码:

int main() {
  medicalCompany o("jack", "ouremail@company.com","2342352134","Germany")
  std::cout << o << std::endl;   
  return 0;
}

The code is not functional and you would have to edit it to fit your formating requirement, but you have the idea:) Good luck!该代码不起作用,您必须对其进行编辑以满足您的格式要求,但您有想法:)祝您好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用生成.cpp和.h文件的自定义生成器? - How can I use custom builders that generate .cpp and .h files? 如果我可以在.h文件中包含所有C ++代码,为什么要使用.cpp文件? - Why to use .cpp files if I can have all of my C++ code in .h file? 如何在子目录内使用.cpp和.h文件? - How to use .cpp and .h files inside subdirectories? 如何将具有继承的类拆分为不同的.h / .cpp文件? - How do I split classes with inheritance up into different .h/.cpp files? 如何在Qt中正确添加类(.cpp和.h文件) - How to properly add classes(.cpp and .h files) in Qt 我如何使用一堆.h和.cpp文件创建可以在另一个c ++项目中使用的静态库 - How do i use a bunch of .h and .cpp files to create a static library that can be used in another c++ Project 将类分为.h和.cpp文件时出现“未命名类型”错误 - 'Does not name a type' error while separating class into a .h and .cpp file C++如何在使用相同的.h时使用相同的.h并选择不同的.cpp? - C++ how can i use the same .h and choose different .cpp when using the same .h? 将代码分为.h和.cpp文件时的链接器错误 - Linker Error when separating code into .h and .cpp files 如何布局我的 C++ 程序? (我应该把 .h 和 .cpp 文件放在哪里?) - How do I layout my C++ program? (where should I put the .h and .cpp files?)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM