Thread: oop-info hiding

  1. #1
    ~Team work is the best!~ wakish's Avatar
    Join Date
    Sep 2005
    Posts
    85

    Question oop-info hiding

    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
    Code:
    //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:
    Code:
    //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

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Never knew that it worked your way too.
    The usual way is
    Code:
    void Box::setValue(int length,int width)
    {
    	this->length = length;
    	this->width = width;
    }
    Kurt

  3. #3
    ~Team work is the best!~ wakish's Avatar
    Join Date
    Sep 2005
    Posts
    85
    yeah..in java we have this:

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

    return this.lenght;

    etc...

    but is it similar in C++??


    And btw "this->variable" is same as "this.variable", right??

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Try it. In c++ there is no this.length. In c++ this is a pointer.
    Kurt

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    From what I've seen, another way would be to adopt some naming convention for member variables, e.g. a trailing underscore. You would then be able to do this:
    Code:
    void Box::setValue(int length, int width)
    {
        length_ = length;
        width_ = width;
    }
    And btw "this->variable" is same as "this.variable", right??
    "this->variable" is the same as "(*this).variable".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Mapping and Moving Relationships
    By Mario F. in forum Tech Board
    Replies: 7
    Last Post: 12-14-2006, 10:32 AM
  2. Question about getting an info class from another Form
    By Joelito in forum C# Programming
    Replies: 0
    Last Post: 10-16-2006, 01:02 PM
  3. Help doing an e-mail program in c...
    By Tyler_Durden in forum C Programming
    Replies: 88
    Last Post: 01-02-2005, 03:12 PM
  4. Info hiding?
    By Mecnels in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 04-07-2003, 02:02 PM
  5. whats it mean by info hiding?
    By correlcj in forum C++ Programming
    Replies: 6
    Last Post: 11-12-2002, 10:29 AM