Jan 20, 2013 at 1:11am UTC
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 Jan 20, 2013 at 2:31am UTC
Jan 20, 2013 at 2:14am UTC
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.
Jan 20, 2013 at 2:33am UTC
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 Jan 20, 2013 at 2:34am UTC
Jan 20, 2013 at 3:42am UTC
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 Jan 20, 2013 at 3:43am UTC