Like Tree1Likes
  • 1 Post By whiteflags

When to use classes and functions?

This is a discussion on When to use classes and functions? within the C++ Programming forums, part of the General Programming Boards category; HI! I've recently been introduced to classes and have been just confused by the overall concept of the object oriented ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    11

    When to use classes and functions?

    HI! I've recently been introduced to classes and have been just confused by the overall concept of the object oriented part of C++...

    When it comes to classes, its not so much why we use them that confuses me, but -WHEN- to use them and -WHAT- to put in them.


    question 1:

    Should I always declare functions in the classes they're made for?

    question 2:

    What do I use the "main()" for, now that i've moved away from procedural programming?

    Do I use main() as little as possible, and try to put everything in an appropriate class while keeping the main() as clean and neat as possible ? lets say, Im making a text rpg game with a long text intro, followed by a character creation menu that uses if else statements... would I store the intro in main(), then call to a separate class for the if else statement portion?


    Im just so confused and would love if somebody could dumbify the concept of using main() along with classes and functions.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,616
    This is up to you. What do you prefer:
    Code:
    print(myClass);
    or
    Code:
    myClass.print();
    ( It's actually much more complicated than this, but hey )

    EDIT: "main()" can be the front-end of your application or just a "proxy" that calls your classes' methods. Again, it's your choise.
    Last edited by GReaper; 10-16-2011 at 06:17 PM.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    11
    print(myClass);

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,616
    Quote Originally Posted by garrettchap1 View Post
    print(myClass);
    Ok, then, let's try something else. Say you want to copy a string. What do you prefer:
    std::string myString = "Assign to myString.";
    or
    char* myString = (char*)malloc(strlen("Assign to myString.") + 1);
    strcpy(myString, "Assign to myString.");
    I believe the answer is obvious.
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    11
    of course id pick std::string myString = "Assign to myString."; !! im not familiar with pointers by the way, just incase its important in your examples

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    Quote Originally Posted by garrettchap1 View Post
    question 1:

    Should I always declare functions in the classes they're made for?
    No. If you do a bit of reading on the subject, they will be able to give you reasons why this isn't always possible. I don't expect you to understand all of this link, because it talks about features you may not know about.

    Member functions are supposed to be about things your class can do with its data. However, sometimes you can write functions for an object without touching the private members and for those kinds of functions, there is no real reason for them to be part of the class's public interface.

    question 2:

    What do I use the "main()" for, now that i've moved away from procedural programming?

    Do I use main() as little as possible, and try to put everything in an appropriate class while keeping the main() as clean and neat as possible ?
    Don't make a god object if you can help it. main() serves as much purpose in object oriented programming as it has in procedural programming. It is still a good idea to avoid globals so main() should be the entry point of your programs, instead of the constructor of some class.
    King Mir likes this.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    As a rule of thumb, I'd say that when you need to model something, such as a car, a fruit, or whatever you can think of, you make that into a class.
    A class can be seen as an object of sorts that brings together all functionality needed to use that object.
    Classes also have the additional advantage of a destructor. It is automatically called when the object is destroyed (when it goes out of scope). This way, you can automatically clean up resources used for things such as strings. For example, any memory allocated by std::string is released when it's destroyed. If you handle strings manually, then you have to get rid of the memory yourself.
    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. classes and functions
    By cpudaman in forum C++ Programming
    Replies: 20
    Last Post: 12-18-2007, 01:45 AM
  2. Help w/ classes and functions
    By Relikie in forum C++ Programming
    Replies: 7
    Last Post: 11-04-2007, 07:42 AM
  3. Classes and member functions
    By uraliss in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2006, 07:38 AM
  4. Classes being able to use other classes functions
    By rainmanddw in forum C++ Programming
    Replies: 6
    Last Post: 01-29-2006, 10:19 AM
  5. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM

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