Why use Classes instead of Structs?

This is a discussion on Why use Classes instead of Structs? within the C++ Programming forums, part of the General Programming Boards category; The way I see it is you can do everything classes do in structs and quicker. For Example: Code: //class ...

  1. #1
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204

    Thumbs up Why use Classes instead of Structs?

    The way I see it is you can do everything classes do in structs and quicker. For Example:

    Code:
    //class
    
    class object
    {
         private:
              int number;
         public:
              void setnumber(int set) {number=set;};
              int getnumber() {return number;};
    };
    
    int main()
    {
              object test;
              test.setnumber(12);
              cout<<test.getnumber();
    }
    Code:
    //struct
    
    struct object
    {
              int number;
    };
    
    int main()
    {
              object test;
              test.number=12;
              cout<<test.number;
    }
    Thanks.

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Because most people use classes for more than just grouping a bunch of variables together.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by yaya View Post
    The way I see it is you can do everything classes do in structs and quicker.
    Missed the point about public/private members?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Also because C++ focuses on OOP, classes are an important part of it. Not making everything public, and accsessable to every function is known as good practice. But then again, C programmers will argue that they do use OOP techniques when they code. It is purly done by choice, but the main point is to use a struct when you have small amount of grouped data and a class when you have a large amount, thus needing to be more carful over which part of the program you give the acsess to.
    I'm just trying to be a better person - My Name Is Earl

  5. #5
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    Oh, I see. Thankyou, and sorry if it seemed like an ultra-stupid question.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    Get/Set for every member is not really necessary, though. If I need such a thing, then I just make the thing public.
    The better choice is to make a real class with OOP focus instead of just a data struct...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    Quote Originally Posted by Elysia
    Get/Set for every member is not really necessary, though. If I need such a thing, then I just make the thing public.
    Making the variable public though kills the entire point of accessors (and thus to a small degree OOP) - making it easier to change how a chunk of data is handled by just changing one method and thus also aiding debugging. Even if I will never change how the data is handled, just potentially reducing the time I spend debugging makes it more than worth it.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    Yes, making all variables public is a bad idea.
    But there's really no point in just adding members, making them private, and then implementing get/set for them. If you're going to do that, you might just as well make them public.
    But if they have another purpose, then it's a good idea to have them remain protected or private.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    Yes, making all variables public is a bad idea.
    But there's really no point in just adding members, making them private, and then implementing get/set for them. If you're going to do that, you might just as well make them public.
    But if they have another purpose, then it's a good idea to have them remain protected or private.
    But what if you have an int member which only has a valid range of 1 to 100?
    If you make it public, anyone can set it to an invalid number, but with a Set function, you can return an error if they try to set it to something bad...
    Also, for multi-threaded applications, Get/Set functions allow you to lock that piece of code while the value is being changed. You can also add some extra logging for debugging purposes...

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    Quote Originally Posted by cpjust View Post
    But what if you have an int member which only has a valid range of 1 to 100?
    If you make it public, anyone can set it to an invalid number, but with a Set function, you can return an error if they try to set it to something bad...
    Then you are using that variable for something specific, so you shouldn't make it public. In that case, you would probably want to make the class emulate the behavior of an int, as well, eliminating the need for get/set.

    Also, for multi-threaded applications, Get/Set functions allow you to lock that piece of code while the value is being changed. You can also add some extra logging for debugging purposes...
    Good points.
    But then again, I haven't had the need for that (although it doesn't mean someone else won't).

    So anyway... if you think it's worth it, then sure, there's nothing wrong with it. But generally, I find it to be more typing/overhead/functions that necessary.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,046
    Quote Originally Posted by Elysia View Post
    But there's really no point in just adding members, making them private, and then implementing get/set for them. If you're going to do that, you might just as well make them public.
    But if they have another purpose, then it's a good idea to have them remain protected or private.
    Yes there is. It adds encapsulation, and allows you to later change how they are implemented.

    For example let say you have a circle object. And you need to get it's area. You could have the circle calculate it's area each time it is needed. Or you could decide it is better for optimization reasons to just add an extra area member. Having an accessor method hides this choice from the code using the method. This allows the programmer to switch between the two choices without changing the API and code using the object.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  12. #12
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    So anyway... if you think it's worth it, then sure, there's nothing wrong with it. But generally, I find it to be more typing/overhead/functions that necessary.
    That seems like you're just programming for today, rather than the tomorrow. When version 2.0 of your program is in development and all of the sudden the requirements change a lot, you'll be glad you spent the extra time adding data encapsulation, especially if your program is a core component in several other programs, since you probably need to maintain API compatibility for those other programs...

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    But, all of this is as I mentioned, that it uses that member for something. If the variable is used for anything--anything at all, then you should consider making it private instead. But if all you do is use it as a struct, then I don't see the point.

    You're right cpjust, in that I haven't had the need to worry for such things. What you mention does make sense, and might be a good idea. It actually shines new light upon the situation.
    But it depends much on the need.
    I think I'm going to have to ponder on that one and its usability within my own environment.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structs or classes?
    By legit in forum C++ Programming
    Replies: 4
    Last Post: 06-28-2009, 10:16 AM
  2. classes, structs and unions
    By Luciferek in forum C++ Programming
    Replies: 24
    Last Post: 08-09-2008, 10:26 AM
  3. Are structs and classes the same thing?
    By dwks in forum C++ Programming
    Replies: 6
    Last Post: 11-25-2005, 02:21 PM
  4. Classes or Structs?
    By DeanDemon in forum C++ Programming
    Replies: 12
    Last Post: 12-05-2002, 02:58 AM
  5. Classes or Structs faster for Lists
    By White Rider in forum C++ Programming
    Replies: 24
    Last Post: 04-05-2002, 02:57 PM

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