Howdy~
how could I make use of them after creating some class of my own ? in my book I found something like this:
cc -c ***
what's that ???
Howdy~
how could I make use of them after creating some class of my own ? in my book I found something like this:
cc -c ***
what's that ???
Never end on learning~
Havent got a clue what that means but to use a class make sure you include it in the source and use it like any other objects. here is a very brief example:
Code:#include <iostream.h> class Example { public: Example( ) { cout << "Example object created" << endl; } }; void main( ) { Example eg; }
Yeah black you can do it go go go!
Here's another example:
/*Start code*/
#include <iostream.h>
class hehe {
public:
void view(void) { cout << "Hello!" << endl; }
};
int main(void)
{
hehe haha;
haha.view();
}
/*End code*/
All men are created equal. But some are more equal than others.
Visit me at http://www.angelfire.com/my/billholm
Oops! I forgot the return 0.
/*Start code*/
#include <iostream.h>
class hehe {
public:
void view(void) { cout << "Hello!" << endl; }
};
int main(void)
{
hehe haha;
haha.view();
return 0;
}
/*End code*/
All men are created equal. But some are more equal than others.
Visit me at http://www.angelfire.com/my/billholm
I don't know what cc -c does, but it looks like a Unix or Linux command to compile.
Truth is a malleable commodity - Dick Cheney
I didnt recognize it either, but it is the way what my book told me to use my own class.Originally posted by salvelinus
I don't know what cc -c does, but it looks like a Unix or Linux command to compile.
cc is a standard compiler for most unix system. But usually, the 'cc' term is used generally to refer to any available compiler in your system. Your book is used that because the author has no way of knowing what compiler you are using: bcc, gcc, tcc etc...
If you use Borland then, you just add 'b' and it becomes bcc (obviously hehehe).
All men are created equal. But some are more equal than others.
Visit me at http://www.angelfire.com/my/billholm
Here is a fun starter class, a container for floating point values. Add functions like Average(), etc...
Code:class Float { private: float data; int count; Float *next; public: int Count() { count = 0; //...reset count... Float *t = this; //...assign a pointer to *this* instance... while(t->next) t = t->next, count++; return count; } float Add() { float total = 0; Float *t = this; while(t->next) t = t->next, total += t->data; return total; } bool Push( float num ) { Float *t = this; while(t->netx) t = t->next; Float *newbie = new Float; if( newbie == NULL) return false; newbie->data = num; return true; } void Kill() { Float *t = this; Float *goner = NULL; while(t->next) { if(goner != NULL) delete goner; t = t->next; goner = t; } delete goner; } }; int main() { char str[100]; Float container; do{ printf(" Enter a number! \n To exit, type 'x', then press "enter" ... \n\n\n"); fgets(str, 100, stdin); if(strstr( str, "x") || strstr( str, "X")) break; container.Push( atof(str) ); }while(1); printf(" %i Numbers Were Entered \n\nThe Sum Total Is %.2f ", container.Count() , container.Add() ); getch(); container.Kill(); }
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
Woops, forgot the most important aspect of the whole paradigm - the constructor/destructor (I am writing this at work ) :
NOTE: The program will crash without these!!!!
Float() : data(0), next(0), count(0) {}
~Float() { Float::Kill(); }
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
Geez, some help I am...you will have to change the Push function too
Code:bool Push( float num ) { Float *t = this; while(t->netx) t = t->next; Float *newbie = new Float; if( newbie == NULL) return false; newbie->data = num; t->next = newbie; //...the revision...sorry about that... return true; }
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
>>I am writing this at work
Hehe better do it fast. Your boss might catch you doin' something else.
All men are created equal. But some are more equal than others.
Visit me at http://www.angelfire.com/my/billholm
thanx guys~
in fact, I just want to figure out how I could make my class is contained in my code.
for an instance, I wonder if I could use the user-defined class myClass in the direct way as follows:
#include <myClass>
or other format ?
thanx for any information.
Never end on learning~
You wouldn't #include <myClass>
You'd include the header file that contained the declarations (and possibly the definition) of the class. This might be named myClass.h, in which case you'd #include <myClass.h>
Truth is a malleable commodity - Dick Cheney
thanx, salvelinus.Originally posted by salvelinus
You wouldn't #include <myClass>
You'd include the header file that contained the declarations (and possibly the definition) of the class. This might be named myClass.h, in which case you'd #include <myClass.h>
and what about next step please ? how could the compiler know what inside myClass.h ? I mean what about the definition which are not contained in the myClass.h~
Never end on learning~
Well, if the definitions aren't contained in myClass.h, only the declarations, you implement the definitions in another .cpp file (unix/linux might call these some other extensions, but it's a regular source file) and #include <myClass.h> in that file, let's say myClass.cpp.
Then you all you still need in the main source file is #include <myClass.h>. The compiler or linker does the rest, as long as you keep them in the same directory so the compiler or linker can find them. (You can complicate this by setting different pathways, but no need for that here).
You can define and declare the class in a .h file, I believe, it's just not "good design" to do so. If you use templates in MSVC, you have to do it that way.
There's no rule I know of that says you can only have definitions/declarations for the class myClass in myClass.h or myClass.cpp. You could define/declare other classes in those files as well, although you might want some logical grouping to it. If you declared/defined classes in other files, like otherClasses.h, just #include those as well.
Truth is a malleable commodity - Dick Cheney