Hard to get classes

This is a discussion on Hard to get classes within the C++ Programming forums, part of the General Programming Boards category; Hello! I wonder if you could give me some examples on how a well designed class should look like. I ...

  1. #1
    Unregistered
    Guest

    Unhappy Hard to get classes

    Hello!

    I wonder if you could give me some examples on how a well designed class should look like. I have try to use classes now when programming in C++, but I never seem to get the hang of it.

    I will write a name for a class, and create some variables for it, and maybe someone can tell me how you should go about accessing them (Sorry if I don´t use the correct words all the time when im writing, but im Swedish, so you´ll have to excuse me on that)

    Ok now here it is:

    class Person
    {
    public:
    Person();
    ~Person();
    // what methods should you use here?
    private:
    char foreName[30];
    char sureName[30];
    char gender[30];
    int age;
    };

    I would use set and get functions to access these variables, but I think that it can get kinda annoying if you have a whole lot of variables to access. How would you do this?

  2. #2
    Registered User kitten's Avatar
    Join Date
    Aug 2001
    Posts
    109
    Accessing class' variables via set and get functions is a common way to do it. And there is no other good way if the variables are private (like they should always be). So examples of accessor functions for your class would be:
    Code:
    const char* const GetForeName(void) const
      { return foreName; } 
    void SetForeName(const char* const NewName)
      { strcpy(NewName, foreName); }
    int GetAge(void) const
      { return age; }
    Making error is human, but for messing things thoroughly it takes a computer

  3. #3
    Unregistered
    Guest

    Unhappy Think it´s alot of code for almoast nothing!!

    Originally posted by kitten
    Accessing class' variables via set and get functions is a common way to do it. And there is no other good way if the variables are private (like they should always be). So examples of accessor functions for your class would be:
    Code:
    const char* const GetForeName(void) const
      { return foreName; } 
    void SetForeName(const char* const NewName)
      { strcpy(NewName, foreName); }
    int GetAge(void) const
      { return age; }
    Thanks for you´re answer, But is this really the way you would do it? If you have lots of variables, it will be awful many methods, for almoast nothing!

    I know that this is the secure way of doing it, but if you make the variables public, you could do without no method what so ever. Eaven if it is a not so god way of doing it.

    I do want to do it the C++ way, but I think that struct is so much easier, less work..

  4. #4
    Unregistered
    Guest

    Thumbs up Ooh and...

    Nice Anime picture kitten...

  5. #5
    Registered User kitten's Avatar
    Join Date
    Aug 2001
    Posts
    109
    Well, nobody prevents you from making your variables public, and it's not illegal or something Making them private is just a commonly used method because it's safer and more bug free.

    ....Thanx
    Making error is human, but for messing things thoroughly it takes a computer

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    32

    Smile A game...

    Ok the say that I would use classes, to make some kind of RPG game.

    In that game I have a character, a couple of areas to explore, some monsters that you can encounter, and different items you can find in each area.

    How would you divide this in classes, cause when I use classes I usually end up with to many, or to few classes.

    would you have a class for each of it, like:

    class Character,class Monster,class Items

    and maybe a class for each Area, or a world class, or something like that.

    How would you do it?

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    65
    Create classes as many as you want. Also try to use inheritance to group these classes to make things easier. For example create a base class, monster for example. And derive all your monsters from that class, dragon, hydra, griphon classes for example. All monsters should have something common for most of the time.

    For characters again make a base class character, derive fighter, mage, bard from it for example. You can use the same analogy for most of the time for different objects.

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    If you have an unmanageable number of variables in a class, your class is too big. Redesign it, maybe make base and derived classes or separate classes altogether. There's not a built in data type numbers, for instance. There's int, float, double, etc.
    In Java I think everything is derived ultimately from a base class called Object, but can't swear to that.

  9. #9
    Registered User
    Join Date
    Nov 2001
    Posts
    32

    Thumbs up Thanx..

    Thanks for the advise guys, I will try to follow it. If anyone has anything more to add about classes, I would highly apriciate if you could post them here for me...

  10. #10
    Registered User
    Join Date
    Nov 2001
    Posts
    53

    Possible other way of handling classes..?

    Hmmm..... now, I may be completely wrong about this (so please don't leap on me for suggesting it ).

    I think one way of doing this which prevents writing a load of code which you don't necessarily *need* to write would be to put together a sort of base class which houses the get and set routines (though not really base, if you see what i mean).

    It depends on what you want to use it for of course, but this kind of thing may be useful... I'm sure there are many ways of extending the base class (VarClass in this little bit I wrote) to hold multiple variable types (or if you're lazy you could just write one for each ). The MainClass doesn't really have to be there obviousy but I kind of guessed that's what you were getting at..

    If anyone would like to suggest a different way of doing this kind of thing (like a default method for classes... is there one?? then you could analyse the call and figure out the variable to operate on... hmmm I dunno) that would be interesting...

    - Almosthere

    #include <iostream.h>

    class VarClass {

    protected :
    int MyVar;

    public :
    VarClass() { };
    VarClass(int n) { MyVar = n; };

    int get() { return(MyVar); };
    void set(int n) { MyVar = n; }

    };

    class MainClass {

    public :
    VarClass *Teete;
    MainClass() {
    Teete = new VarClass;
    }

    };

    void main(void) {

    MainClass HooHoo;

    HooHoo.Teete->set(10);
    cout << "Teete is... " << HooHoo.Teete->get() << endl;

    }

  11. #11
    Registered User
    Join Date
    Nov 2001
    Posts
    32

    Unhappy Another question...

    If I would want to make an array of objects, how would I do this with classes, when I do it with structs it might look something like this:

    typedef struct Character
    {
    // some variables
    }c;

    const int size = 5;
    Character charArray[size];

    But how would i do this with classes, is it the same as with structs? I have tried to do it with a class.

    I define a class in a .h file and include it to a cpp file, where I try to use the array of objects, but it doesent work..

    I could really need some help with this..

    Thanx!!

  12. #12
    Registered User kitten's Avatar
    Join Date
    Aug 2001
    Posts
    109
    Array with classes is done exactly the same way you would do array with structs. In a matter of fact only difference between struct and class is that default visibility is public in struct and private in class.
    Code:
    class MyClass
    {
      private:  // default visibility
      // whatever...
    };
    
    struct MyStruct
    {
      public:  // default visibility
      // whatever...
    };
    
    
    MyClass ArrayOfClass[10];
    MyStruct ArrayOfStruct[10];
    Making error is human, but for messing things thoroughly it takes a computer

  13. #13
    Registered User
    Join Date
    Nov 2001
    Posts
    53
    Well... not the only difference....

    Ah
    "Technology has merely provided us with a more efficient means for going backwards" - Aldous Huxley

  14. #14
    Registered User
    Join Date
    Nov 2001
    Posts
    32

    Unhappy Thanx Kitten..

    Could you help me with this, When Im trying to use my array of objects, I get an error from the compiler, It looks like this:

    error LNK2001: unresolved external symbol "public: __thiscall Monster::Monster(void)
    error LNK2001: unresolved external symbol "public: __thiscall Monster::Monster(void)

    (2 externals)

    "It only happens when I use the array"
    ex.
    monsterArray[0].name("BrainLeach");

    I create the array of objects in mine header file:

    class Monster
    {
    public:
    // the methods
    private:
    // some variables
    };
    const int size = 5;
    Monster monsterArray[size];

  15. #15
    Registered User kitten's Avatar
    Join Date
    Aug 2001
    Posts
    109
    Well... not the only difference....
    Yes it is
    Making error is human, but for messing things thoroughly it takes a computer

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-06-2005, 08:11 PM
  2. Abstract classes
    By cisokay in forum C++ Programming
    Replies: 17
    Last Post: 05-29-2005, 10:39 AM
  3. Trinary Hard Drive
    By nickname_changed in forum Tech Board
    Replies: 14
    Last Post: 05-13-2005, 11:01 AM
  4. Problems with memory allocation in classes
    By MathFan in forum C++ Programming
    Replies: 6
    Last Post: 01-09-2005, 02:05 AM
  5. Sharing a variable between classes of different .CPP files
    By divingcrab in forum C++ Programming
    Replies: 5
    Last Post: 07-07-2002, 03:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21