View Single Post
  #10 (permalink)  
Old 07-26-2007, 03:19 PM
rpgfan3233 rpgfan3233 is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jul 2007
Posts: 118
iTrader: (0)
rpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura about
Quote:
Originally Posted by HelloWorld View Post
Thanx a lot rpgfan3233, it's really helpful



Is using then the same thing as when you declare an instance variable in Java? private String cin; for example...
No, because using in C++ is similar to import in Java. It gives you more convenient access to members of a package/namespace.

When you declare an instance variable like that, it would be comparable to declaring a member of a class, a struct or even a global variable, though the latter can be misused rather often, especially when moving from C to C++. The reason why I say it can be like a member of any of those is because an instance variable is inside a wrapper - the class. In C++, you can use global variables to affect your entire file. That file in C++ is like the class that you create in Java. It can also be a member of a class or a struct as well. After all, those two are wrappers themselves:
PHP Code:
class SomeClass {
public:
    
//constructor for the class
    
SomeClass () : _exampleVal(42) { } //initializes private member variable _exampleVal to 42

    //destructor for the class
    
~SomeClass () { _exampleVal 0; } //not really necessary, but it is nice to have 0s anyway

    //returns _exampleVal since you can't get its value directly
    
inline int getValue () { return _exampleVal; }

    
//sets _exampleVal since you can't set its value directly
    
inline void setValue (int value) { _exampleVal value; }
private:
    
int _exampleVal;
}; 
Note that in that case, the class keyword could probably have been replaced with the struct keyword. struct in C++ was carried over from C, and it is a sort of container for various things. In C++, the idea of public, private and protected members was introduced, so struct is similar to class.

Now you are probably wondering what the difference is between struct and class if they do basically the same thing. Firstly, within a struct all members are public by default whereas all members are private in a class. The above code example could have left out the private section entirely and moved the private member variable to a location inside the class before the public section. That is the only difference defined by the C++ standard, though your compiler may also distinguish them in some other way. However, many programmers have their own conventions to help them decide when to use which one. A discussion of when to use what can be found at the following Web page: Struct vs Class - C++

I personally agree with the post about using structs when they contain only variables and no functions and data hiding isn't as much of an issue. With things that require functions and C++ features such as inheritance, private/protected members, etc., however, classes are recommended.

__________________
"C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off."
-- Bjarne Stroustrup, creator of what is now known as C++
For more quotes by Bjarne Stroustrup, check out http://www.research.att.com/~bs/bs_faq.html#really-say-that.
Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to rpgfan3233 For This Useful Post:
HelloWorld (07-26-2007)