Results 1 to 9 of 9

Thread: Classes?

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2000
    Location
    New Hampshire
    Posts
    32

    Exclamation

    Ok i have this c++ book that says leran c++ in 24 hours. and uh i think i might of been stuck more than 24 hours on a chapter alone on classes. It doesnt sound right or something. so if any of you guys would be so kind to explain how a class is created or how it works, that will be so helpfull to me.

    :-)
    Factor777

  2. #2
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    Ahh you are asking a big complex question with a bunch of different answers. But I will take a stab at it.

    But first a recommendation, you should get a different book. If this book starts off with classes in Chapter 1 and you dont know anything about C++, this is not going to be a good book for you. I would recommend Beginning Visual C++ by Ivor Horton, also if you search though the forms there have been a ton of other threads about starting books.

    Think of a class as a group of various functions that do a different tasks. Like if I had a class called CMath (I made this up). Inside that class I could have, CMath::Add(), CMath::Sub(), CMath::Multi(), and CMath:iv(). So if I wanted to call this class and a particular function, one of the many ways you could call it,

    Code:
    CMath math;
    math.Add()
    There are a bunch of different classes that are default or MFC classes that are part of VC++. Like CButton, CEdit, CForm, CWinsock, and on and on. You can also make your own classes. You can do this by going into Class View, right click on the top of the tree, and choose new class.

    I hope this helps some. Again I would get a different book to start with.
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I would suggest not using ClassWizard to create your own classes, as it seems to mungify them (what a word!) a bit :-(.

    Basically, here's a template:

    ClassName.h
    Code:
    #ifndef __CLASSNAME_H__
    #define __CLASSNAME_H__
    
    class ClassName {
    public:
        int myfunc();
    };
    
    #endif // __CLASSNAME_H__
    ClassName.cpp
    Code:
    #include "ClassName.h"
    
    int ClassName::myfunc() {
        return 5;
    }
    That's a lot simpler than what ClassWizard generates.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  4. #4
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    Parksie is right, I should have said I only use the class wizard when I am using MFC classes. Its better to make your own when ever possible.
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  5. #5
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    In case the problem you had with classes wasn't how they were made, but what they were about, I'll try to explain a bit more

    Firstly, before you learn about classes you really ought to know about structures, or user defined types. In the case of structures, they are for organising many items of data relating to the same thing. In Pascal, they are called 'records', just as you might have a record in a database. So clearly, it is useful to have a way to group data about one thing together, and be able to keep lots of data about different items.

    The next step is classes. Classes allow you to keep not only data together, but the operations that you might want to perform on that data. Classes can include functions to manipulate and modify the data. These functions are also commonly called methods, or behaviours.

    The benefit of all this is that you can describe an object in a model you have designed for your program. You can define classes of objects that will manage their own data on request, and will yeild information derived from their data and accept new data also on request. This is the basis of encapsulation, and important OO principle. There are 5 others, which I won't go into, but basically OO allows you to model a program more intuitively. Some modelling languages, such as UML, can be more or less implemented straight into a set of classes with objects of those classes created to interact with each other.
    Harry.

    "From one thing, know ten thousand things."

  6. #6

    Thread Starter
    Member
    Join Date
    Sep 2000
    Location
    New Hampshire
    Posts
    32

    Post

    oh mean like unions & structures?
    Code:
    union automobile {
    	int year;
    	char model[8];
    	int engine_power;
    	float weight:
    }
    i could just replace "union" with "struct" to make a structure.
    well i believe i have that down. please go on.

    i sort of understand what parksie just wrote but how does classes form windows in windows(9x-2000)
    this is what i would really like to know. so anyway i guess i should start of simple.

    so could u explain this a little more parksie? pleazzzzzzz

    ClassName.h
    Code:
    #ifndef __CLASSNAME_H__
    #define __CLASSNAME_H__
    
    class ClassName {
    public:
        int myfunc();
    };
    
    #endif // __CLASSNAME_H__
    ClassName.cpp
    Code:
    code: #include "ClassName.h"
    
    int ClassName::myfunc() {
        return 5;
    }

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Erm...on unions...they're like structures, but all the items start at the same memory location. For example, you can do this:
    Code:
    union x {
        long lval;
        short sval;
    };
    sval overwrites the first 2 bytes of lval (which is 4 bytes).

    Classes have absolutely nothing to do with Windows, or windows

    The template...in the .h file, the preprocessor directives (#something) are just there to stop the code being executed twice...not too hard to see.

    The .cpp file just defines the code for the defined functions.

    ClassName::myfunc() tells the compiler that it's not declaring code for a global function, but for one that's a member of class ClassName.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  8. #8

    Thread Starter
    Member
    Join Date
    Sep 2000
    Location
    New Hampshire
    Posts
    32

    Smile

    hmmmm WOW! the book never said that! thats cool.

    but im still not any closer to windows Obj. Oriented Programming?

    well any way i feel dumb.

    im going to read

    "how to think in c++ 1 & 2" will this get me to program win32 apps?

    or maybe i totally mis-understood windows programming.

    thanks for all the help earlier, appreciate it.

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    A book on C++ will not teach you how to write Windows programs. You use the Platform SDK and tutorials such as the one at www.winprog.org for that.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width