[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 187
  • Last Modified:

Class functions (Standards)

Most books I have seen on C++ include an example similar to:
class foo
{
  private:
    int x;
  public:
    int GetX();
    void SetX(int);
}
int foo::GetX()
{
  return x;
}
void foo::SetX(int NX)
{
  x = NX;
}

My question is, is this just a method of showing you how classes work without bogging it down to much by doing something like:
void foo::SetX(int NX)
{
  if (NX <5)
    x =5;
  else
    x = NX;
}

Or is there another reason they don't just do:
class foo
{
  public:
    int x;
}
0
List244
Asked:
List244
3 Solutions
 
AxterCommented:
Hi List244,
> Or is there another reason they don't just do:

Yes.  Encapsolation.
You're trying to limit external code accessing your data directly.

Cheers!
0
 
List244Author Commented:
But, why do you care if they have direct access if you blindly assign anyway?
0
 
PaulCaswellCommented:
Hi List244,

We have laboured for many years with only three levels of scope: local, static, and global. With objects you have four levels, a new one called 'instance' with several levels of secrecy. Making 'x' public would place 'x' in the least secret scope it is able to achieve without leaving the object. This situation does not follow simplicity principle "if its simple and does what you want then it is right". In this case, its "a stitch in time". You should always make instance variables in the most secret place that is feasable for its use. :-)

Paul
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
PaulCaswellCommented:
Hi List244,

>>But, why do you care if they have direct access if you blindly assign anyway?
The benefits take a bit to describe.

In very large or complex software packages there are always bugs. The worst type to track down is memory corruption. In many cases, changing any of the code results in its disappearance. Horrible! But imagine if you absolutely knew that the variable being corrupted was only 'visible' by a very few functions? Think how much time you'd save debugging!

Besides, how do you know that the first method isnt more efficient than the second?

Paul
0
 
List244Author Commented:
>>Besides, how do you know that the first method isnt more efficient than the second?

First method, meaning having the functions rather than a global?
0
 
dennis_georgeCommented:
Hi,

I think the main reason people choose to have accessor functions is to have flexibility in your code.....

if you make a variable public then everybody might access it directly.... But suppose in future you want to add some control over accessing your member variable or additional check or anything you have to do a lot of work (all over ur project) to implement it if the variable is public.... but if you had used accessor this problem have been solved pretty easily without much change....

Dennis
0
 
PaulCaswellCommented:
>>First method, meaning having the functions rather than a global?
Yes!

Paul
0
 
List244Author Commented:
Thank you everyone for response, I have split the points.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now