my program:
Code: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
i DO NOT get the desired output since the compiler is confused.Code://setter void Box::setValue(int length,int width) { length = length; width = width; }
But if i prefix the private data with the Box:: as shown below:
the program works fine!Code://setter void Box::setValue(int length,int width) { Box::length = length; Boxx::width = width; }
So, i want to ask if the way i did is correct or not???
Thanks for ur attention!
Kind Regards,
wakish



LinkBack URL
About LinkBacks


