Thread: C++ Classes

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    2

    C++ Classes

    Hey, i've been studying c++ for a while now. I've recently been learning about the class datatype. What exactly would you use a class to do? Thanks for taking time. have a good day.

  2. #2
    Banned
    Join Date
    Jun 2005
    Posts
    594
    a class let you have data that protected, private, and public
    which can help safeguard stuff. a class also lets you have a
    bunch of function inside it that you can get to thru the class.

    i dont know if that awnser your question, here a example
    of what a class looks like :

    Code:
    #include <iostream>
    using namespace std;
    
    class math
    {
        public:
    	int add(int a, int b)
    	{
    	        return (a+b);
                    }
    };
    
    int main()
    {
        math plus; //Let you use the word plus to talk to the functions in the class
        //To use a function inside the class you would
        cout << plus.add(1, 3) << endl;
        //Another way to use the function
        int value = plus.add(1, 3);
        cout << value << endl;
        cin.get();
        return 0;
    }
    I jsut typed that out real fast i might of made a typo but that
    the general construct, so people will put there function
    else where and just have the declartion of it inside the class.

    A Class Tutorial
    Last edited by ILoveVectors; 06-26-2005 at 08:27 PM.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    2
    Hey, thanks for the info. That gives me a better understanding of a class. Knowing when to use the constructors and destructors is still confusing, but all is well.. just got to look at the tutorials.
    Thanks.
    --Alex

  4. #4
    Banned
    Join Date
    Jun 2005
    Posts
    594
    A contructor, you can place statment in to collect data or display
    data when a class is created.

    For a destructor you can place data in it to state when the class
    is done and you destroy it. A destructor can also contain code
    to do any memory managment you need. such as freeing memory.
    Last edited by ILoveVectors; 06-26-2005 at 08:25 PM.

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    very important. 1st learn to use structures. later you can think in classes

  6. #6
    *this
    Join Date
    Mar 2005
    Posts
    498
    Quote Originally Posted by xErath
    very important. 1st learn to use structures. later you can think in classes
    I think classes are easier consept to learn first, then you can apply the concepts to structures. I learned classes first, and they come before structures in the curriculum i took.

  7. #7
    Banned
    Join Date
    Jun 2005
    Posts
    594
    interesting since i always that struct were just class
    without functions and less data protection,
    id think struct would be easier to learn.
    then agian some of the things you can use struct for are
    actually prolyl more difficult then the concept of a class,
    so that prolly why you learned them after classes.

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    In C++, the only difference between a struct and a class is the default access specifier.

    class defaults to private, whereas struct defaults to public.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  9. #9
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Structs and classes are the same thing in C++. Structs just default to public where as classes are default to private.
    Woop?

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. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM