Thread: Why are classes useful?

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    204

    Why are classes useful?

    Why do classes make it easier to write a program in C++ than to do the same in C?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Basically classes help you take all the properties and behaviors of an object in your program, and combine them into a single interface, then re-use that interface wherever you need that type of object in your program.

    Take strings for example. In C, if you are working with strings, copying them, concatenating them, getting their length, finding substrings, etc, you have to do a lot of work to make sure you have the right amount of memory available, then copy or concatenate the strings, etc. In C++, all that work is put together inside a string class that works like a built-in type. You copy C++ strings with =, combine strings with + or +=, and do other operations with member functions like find(), substr() or length(). Once you have that string class written, you just re-use it over and over as a new type without having to worry about the implementation details.

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    it took me a while to ... get why they were useful. I think time will teach you better than anything else, though Daved gave the example I was going to give. Having functions and operators associated with a variable is very very handy. Saves you having to remember functions manipulate variables etc. I love them

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    For small programs, I wouldn't say object oriented programming is "easier". (It might be easier if you learned C++ / OOP as your first programming language..)

    But for large complex projects, it's easier to design and maintain the program. And, it's easier to break into tasks so that several programmers can work in parallel.

    OOP seems to work well with GUI applications, where the user interaction is less rigid than traditional text-based programs.

    When it comes to C vs. C++, it's a bit of a paradox. Some things, like strings or vectors (dynamic arrays) are easier in C++. But overall, the C++ language is much more complex and harder to learn than C. For example, there are a couple of referece books that cover the entire C language, but you can't find a book that covers every function in the ANSI/ISO C++ standard libraries (except for the language standard itself).

  5. #5
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Thinking in C++ by Bruce Eckel will teach you why OOP is good and how classes work their way into OOP. (OOP = Object Oriented Programming)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM