whats the difference?

case 1:
1
2
3
4
void Being::showname(Being& B)
{
	cout<<"Name: "<<GetName()<<endl;
}


case 2:

1
2
3
4
void Being::showname(Being& B)
{
	cout<<"Name: "<<B.Name;<<endl;
}




1
2
3
4
5
6
7
8
9
10
11
class Being
{

protected:
	
	string Name;
public:
string GetName(){return Name;}

void showname(Being& B);
};



im just curious what is the difference and which method is better?
Last edited on
First of all there is no such a method as ShowStat in the class definition of Being. So if you want to get an answer please show code that can be compiled.
im not asking for a solution =-= to any sort of problem. im just asking what is the difference between case 1 and case 2 logically
Last edited on
cout<<"Name: "<<GetName()<<endl; leads to more maintainable code.

For instance, if we had written:

1
2
3
4
5
6
7
8
struct being
{
    void show_name() const { std::cout << name() << '\n' ; }

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

    std::string its_name ;
};


And later modified the class to:
1
2
3
4
5
6
7
8
9
struct being
{
    void show_name() const { std::cout << name() << '\n' ; }

    std::string name() const { return its_name + " (aka: " + alias + ')' ; }

    std::string its_name ;
    std::string alias ;
};


being::show_name() requires no change; it keeps pace with being::name()
Last edited on
thank you
Topic archived. No new replies allowed.