class

I am tring to understand understand what the following program does.
I am assuming the following:

1.From line 26 "John Done" argument is passed to the accountName in the line 8.

2.accountName initializes the name data member in line 9.

3. The next step is to create the setName member function but I am not sure how the accountN in line 13 gets the argument. I know the setName is a member function that sets the account name in the object.

Could someone explaine how the argument "John Doe" passed to the setName function?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <string>
#include <iostream>

using namespace std;

class Account{
public:
  explicit Account(std::string accountName)
  : name{accountName}{

  }

void setName(std::string accountN){
    name = accountN;
}

std::string getName() const{
    return name;
}

private:
std::string name;
};

int main(){
    Account account1{"John Doe"};
    cout << account1.getName();
}
Running this helps to throw some light:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <string>
#include <iostream>

using namespace std;

class Account{
public:
  explicit Account(std::string accountName)
  : name{accountName}{

  }

void setName(std::string accountN){
    name = accountN;
}

std::string getName() const{
    return name;
}

private:
std::string name;
};

int main(){
    Account account1{"John Doe"};
    cout << account1.getName() << '\n';
    
    // CREATE ANOTHER ACCOUNT IN THE NAME OF "Betty Boop"
    Account account2("Betty Boop");
    cout << account2.getName() << '\n';
    
    // CHANGE Betty's name
    account2.setName("Bob Smith");
    cout << account2.getName() << '\n';
}


John Doe
Betty Boop
Bob Smith
Program ended with exit code: 0
Your setName() member function is used to change an already created object's name member data.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <string>
#include <iostream>

class Account
{
public:
   explicit Account(std::string acctName) : name{ acctName } { }

public:
   std::string getName() const               { return name; }
   void        setName(std::string acctName) { name = acctName; }

private:
   std::string name;
};

int main()
{
   Account acct { "John Doe" };

   std::cout << acct.getName() << '\n';

   // let's change the object's stored name using setName()
   acct.setName("Joe Blow");

   std::cout << acct.getName() << '\n';
}
John Doe
Joe Blow
3. The next step is to create the setName member function


No. In this example, setName() is not used.

 
Account account1{"John Doe"};


calls the constructor as per steps 1 & 2.

 
cout << account1.getName();


calls .getName() member function.

setName() although defined in the class is not used.


Also note that when passing containers (such as string) as a function argument, these are usually passed by ref and not by value as here. Passing by value causes a copy to be undertaken which is not neccessary here.

 
explicit Account(const std::string& accountName)  : name{accountName}{ }

Last edited on
Passing by value causes a copy to be undertaken which is not neccessary here.


As long as you move from the parameter, passing by-value is actually not horrible since the data must be copied once anyway. The difference is merely one additional move.

This is suboptimal, but to correct it you'll need to throw away that overrated DRY principle and essentially write the same code twice - by adding a redundant move constructor.

Get optimal behavior by passing string_view instead.
Topic archived. No new replies allowed.