Kourosh23 wrote: |
---|
Feel free to mention suggestions |
Here are a few
1 2 3 4 5
|
name() // default constructor, takes no parameter.
{
first_name = "Jack";
last_name = "Peter";
}
|
|
This first constructs both members as empty string objects, only to destroy them in the assignment operators.
Followng Core Guidelines rule C.49: Prefer initialization to assignment in constructors
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rc-initialize this should be:
1 2 3
|
name(): first_name{"Jack"}, last_name{"Peter"}
{ // constructor body is generally empty
}
|
and going further, your initial values are constants, so C.48: Prefer in-class initializers to member initializers in constructors for constant initializers
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rc-in-class-initializer
1 2 3 4 5
|
class name
{
private:
string first_name = "Jack";
string last_name = "Peter";
|
and drop that constructor entirely.
Next,
1 2 3
|
string get_first_name() {
return first_name;
}
|
|
This member function makes no changes to the class, it has to be const:
Con.2: By default, make member functions const
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rconst-fct
1 2 3
|
string get_first_name() const {
return first_name;
}
|
but, more significantly, your getters are trivial, and you could simplify this further:
C.131: Avoid trivial getters and setters
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rh-get
also applying C.2: Use class if the class has an invariant; use struct if the data members can vary independently
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rc-struct
and C.4: Make a function a member only if it needs direct access to the representation of a class
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rc-member
and for good measure SL.io.50: Avoid endl
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rio-endl
also restoring the #include you missed and applying a couple more style tweaks..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
#include <iostream>
#include <string>
struct name
{
std::string first_name = "Jack";
std::string last_name = "Peter";
};
std::string get_result(const name& n) {
return n.first_name + " " + n.last_name;
}
int main()
{
name use;
std::cout << use.first_name << " " << use.last_name << "\n\n";
// or
std::cout << get_result(use) << '\n';
}
|
now of course if you don't want to make it possible to alter the strings once the object is created, then you have an invariant and it has to be a class with getters:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#include <iostream>
#include <string>
class name
{
std::string first_name = "Jack";
std::string last_name = "Peter";
public:
std::string get_first_name() const { return first_name; }
std::string get_last_name() const { return last_name; }
};
std::string get_result(const name& n) {
return n.get_first_name() + " " + n.get_last_name();
}
int main()
{
name use;
std::cout << use.get_first_name() << " " << use.get_last_name() << "\n\n";
// or
std::cout << get_result(use) << '\n';
}
|