Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

oop-info hidding

my program:

BOX.H
-------
#ifndef BOX_H
#define BOX_H

class Box
{
private:
int length;
int width;

public:
//constructor
Box();

//methods
void setValue(int length,int width);
int getArea();

//destructor
~Box();
};

#endif
--------
************************

BOX.cpp
--------
#include "box.h"

//constructor
Box::Box()
{
length = 8;
width = 8;
}

//setter
void Box::setValue(int length,int width)
{
Box::length = length;
Box::width = width;
}

//getter
int Box::getArea()
{
return (Box::length * Box::width);
}

//destructor
Box::~Box()
{
length = 0;
width = 0;
}
---------
****************************

BoxMain.cpp
---------
#include <iostream>
#include "box.h"

int main()
{
Box small, medium, large;

small.setValue(5,7);

large.setValue(15,20);

//display results
std::cout << "\nThe small box area is " << small.getArea();
std::cout << "\nThe medium box area is " << medium.getArea();
std::cout << "\nThe large box area is " << large.getArea() << std::endl;

return 0;
}
----------
*****************************

My question:

As you can see in the methods, i have declared the variables inside the setter methods same as the private objects in the class.
My aim was to be able to use same name as you can see.
If i DO NOT prefix the private data with Box:: as shown below

//setter
void Box::setValue(int length,int width)
{
length = length;
width = width;
}
i DO NOT get the desired output since the compiler is confused.

But if i prefix the private data with the Box:: as shown below:

//setter
void Box::setValue(int length,int width)
{
Box::length = length;
Boxx::width = width;
}
the program works fine!

So, i want to ask if the way i did is correct or not?

Thanks for ur attention!

Kind Regards,
wakish
[2264 byte] By [wakish] at [2007-11-11 7:00:21]
# 1 Re: oop-info hidding
This is not the common C++ programming style. In fact, using something like Box::length is very misleading since it may suugest that length is a static class member or that Box is a namespace.
First, a decent C++ compiler should have no problem compiling this:

void Box::setValue(int length,int width)
{
length = length;
width = width;
}

The compiler knows that the righthand side length and the one on the left are differnt. The problem is that human readers may find it very confusing so the solution is to use a different name for the function parameter:

void Box::setValue(int len,int wid)
{
length = len;
width = wid;
}

Some folks prefer to add an underscore before data members names:

void Box::setValue(int length,int width)
{
_length = length;
_width = width;
}

In any case: Box::length is uncalled for and undesirable either.
Danny at 2007-11-11 21:02:47 >
# 2 Re: oop-info hidding
ok..thats what i was fearing..

but in java we have "this" which allow:

this.lenght=length;
this.width=width;

return this.lenght;

etc...

but do we have somehting similar in C++??
wakish at 2007-11-11 21:03:52 >
# 3 Re: oop-info hidding
First, a decent C++ compiler should have no problem compiling this:

void Box::setValue(int length,int width)
{
length = length;
width = width;
}

The compiler knows that the righthand side length and the one on the left are differnt.

This seems weird.... there are 4 possible cases.
1) setting the length defined in setValue equal to itself
2) setting the length defined in class equal to itself
3) setting the setValue lenght equal to the class memeber length
4) setting the class memeber length equal to the setValue lenght

how would the compiler know which is the intended meaning? suppose that it always assume you'll never set a value equal to itself, how does it tell if it's option 3 or 4 that is desired?

I think in Java, it will just treat it as if you are setting the value equal to itself.
rssmps at 2007-11-11 21:04:46 >
# 4 Re: oop-info hidding
Yea, I guess you're right. The compiler treast

length = length;

in the following manner: it treats 'length' as name of the argument passed to the function, not as the data member's name. You can overcome this by adding an explicit this->:

this->length=length;

In this case, the first length is of course the object's data member, whereas the latter is the argument, as before. However, for obvious reasons I wouldn't recommend this style. Ideally, you'd choose slightly different names for the data members and the parameters. In either case, doing something like Box::length is pretty clumsy and misleading.
Danny at 2007-11-11 21:05:46 >
# 5 Re: oop-info hidding
I think in Java, it will just treat it as if you are setting the value equal to itself.

Nops, in Java it's totally different.
And Java provides for such usage.

I'm surprised that C++ dn't cater for this
:SICK:
wakish at 2007-11-11 21:06:45 >