Advertisement

Data Structures, Classes, and all that Funk(tm)

Started by July 08, 2000 01:59 AM
21 comments, last by Goblin 24 years, 5 months ago
Well, it''s strange, because:

In a book I have, Learn C++ in 24 Hours, it doesn''t even talk about data structures (so far), only classes... but it will do things like:

    class Cat{public:int inline getAge();private:int Age;}Cat::getAge(){return(Age);}    


... and this is supposed to be more efficient. How the heck is this any more efficient? Wouldn''t it be a lot easier to make Age public?

And just a quick, stupid question... I realize that private information in classes in only available to stuff inside the class, (i.e. methods), but is the class itself global? Or is it just like anything else, that if it is defined the function it is local, and outside, it is global? (Don''t worry, just a bit confused for now).

Thanks!

- The Goblin (madgob@aol.com)
Having "Getter" and "Setter" functions is not more efficient time wise (although if you define them inline it doesn''t matter) but it''s just a design issue. It''s keeping the separation between the "interface" and the "implementation." But it''s not necessarily more efficient except from a design standpoint.

The class is a data type and can be used wherever it''s been declared. It''s ''global'' in the same way that any struct or data type is global, but data members of the class are not global. Except for static members, which are *sort of* global in that you don''t need an instance of the class to access them, but they still belong to the class.

I hope I''ve helped, and not confused you
Advertisement
Well, so I''m just double-checking...

    #include <iostream>class Cat{public:int Age;};void addAge();int main(){class Dog{public:int Age;};Cat Mittens;Dog Fido;addAge();return 0;}void addAge(){Mittens.Age++;Fido.Age++;}    


So, I know that was probably a big waste of time, but I''m just double-checking that this would get some kind of error that Fido.Age could not be accessed through addAge(), since it isn''t global, right?

Sorry to ask such silly questions, I just want to make sure I get this stuff right.

Thanks!


"Death to all who oppose me."
- The Goblin (madgob@aol.com)

This topic is closed to new replies.

Advertisement