Click to See Complete Forum and Search --> : Classes?
factor777
Feb 13th, 2001, 05:12 AM
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
Technocrat
Feb 13th, 2001, 11:31 AM
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::Div(). So if I wanted to call this class and a particular function, one of the many ways you could call it,
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.
parksie
Feb 13th, 2001, 12:07 PM
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
#ifndef __CLASSNAME_H__
#define __CLASSNAME_H__
class ClassName {
public:
int myfunc();
};
#endif // __CLASSNAME_H__
ClassName.cpp
#include "ClassName.h"
int ClassName::myfunc() {
return 5;
}
That's a lot simpler than what ClassWizard generates.
Technocrat
Feb 13th, 2001, 12:10 PM
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.
HarryW
Feb 13th, 2001, 01:11 PM
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.
factor777
Feb 13th, 2001, 02:19 PM
oh mean like unions & structures?
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. :D
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 :D
ClassName.h
#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;
}
parksie
Feb 13th, 2001, 02:29 PM
Erm...on unions...they're like structures, but all the items start at the same memory location. For example, you can do this:
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 :p
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.
factor777
Feb 13th, 2001, 05:08 PM
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. :)
parksie
Feb 13th, 2001, 05:10 PM
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.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.