-2

Take a look at the following source code:

#include <iostream>
#include <string>

#ifndef _PATIENT_H_
#define _PATIENT_H_

#include "Base.cpp"

class Patient: public Base
{
private:
    std::string name_;
    std::string date_of_birth_;
    std::string sex_;
public:
    void set_name(std::string & name)
    {
        name_ = name;
    }
    std::string get_name() const
    {
        return name_;
    }
    void set_date_of_birth(std::string & date_of_birth)
    {
        date_of_birth_ = date_of_birth;
    }
    std::string get_date_of_birth() const
    {
        return date_of_birth_;
    }
    void set_sex(std::string & sex)
    {
        sex_ = sex;
    }
    std::string get_sex() const
    {
        return sex_;
    }
};
#endif

I have two questions here:

  1. Should #include <iostream> and #include <string> be inside #define _PATIENT_H_?
  2. In the setter accessors, how should I write the const keyword:
    a. void set_name(const std::string & name)
    b. void set_name(std::string const & name)

?

user366312
  • 16,949
  • 65
  • 235
  • 452
  • 1: Yes, 2: Whichever you like more. – tkausl Mar 20 '21 at 19:13
  • 1
    Fyi, `#include "Base.cpp"` - preprocess -including *anything* .cpp is a code smell. There should be a `Base.h` there, and proper build steps to build `Base.cpp` and whatever else you're building. – WhozCraig Mar 20 '21 at 19:14
  • 1
    1. Yes. 2. Most developers that I work with are *Const West* (the const appears first), but there is a smaller cadre of developers that I work with that prefer *East Const* (the const is after the type). In the code given, it doesn't matter to the compiler. Whichever you find more readable. – Eljay Mar 20 '21 at 19:15
  • 1
    Nothing in the patient class uses anything from iostream. Don't include what you don't use. And that inclusion of `Base.cpp` looks suspect to any C++ savvy programmer. – StoryTeller - Unslander Monica Mar 20 '21 at 19:16
  • Also please read [What are the rules about using an underscore in a C++ identifier?](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) Any and all symbols beginning with an underscore and followed by an upper-case letter (like e.g. `_PATIENT_H_`) are reserved everywhere. – Some programmer dude Mar 20 '21 at 19:16
  • @Eljay - "*most*", *"smaller"*... citation required. – StoryTeller - Unslander Monica Mar 20 '21 at 19:17
  • 2
    @StoryTeller-UnslanderMonica • I updated my comment to reflect the source of my qualifiers. I'm in the *East Const* camp. – Eljay Mar 20 '21 at 19:19
  • [NL.26](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rl-const) says `const` first is "far more common, conventional". I couldn't find a poll of *Const West* versus *East Const* preferences. (Apparently the Tabs-v-Spaces and Where-the-Curly-Braces-Go holy wars weren't enough.) – Eljay Mar 20 '21 at 19:25
  • @Eljay IIRC, the point is that east const is allegedly more consistent. E.g. `int *const` (right-to-left "const pointer to int") is similar to `int const` (right-to-left "const int"). – HolyBlackCat Mar 20 '21 at 19:32
  • @HolyBlackCat • Yes, that's the rationale us *East Const-ers* use to advocate & evangelize it. It's more const-sistent (*ba-dum-tiss*). – Eljay Mar 20 '21 at 19:54

1 Answers1

2
  1. It doesn't matter, but I would prefer the include guard as the first thing in the file. The reasons are:

    • The preprocessor has to do less things when you include the file more than once.

    • The preprocessor can recognize this as a classical include guard, and handle it in an optimized manner.

    • It's more common, and it's easier to notice if you have a lot of #includes.

  2. It doesn't matter. const std::string is more traditional, but some people use std::string const claiming it to be easier to read.

And an obligatory reminder that including .cpp files is not recommended.

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207