how could I use my own class ?

This is a discussion on how could I use my own class ? within the C++ Programming forums, part of the General Programming Boards category; Howdy~ how could I make use of them after creating some class of my own ? in my book I ...

  1. #1
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    Question how could I use my own class ?

    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~

  2. #2
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    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;
    }

  3. #3
    Registered User billholm's Avatar
    Join Date
    Apr 2002
    Posts
    225

    Talking

    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

  4. #4
    Registered User billholm's Avatar
    Join Date
    Apr 2002
    Posts
    225

    Lightbulb

    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

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    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

  6. #6
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    Unhappy

    Originally posted by salvelinus
    I don't know what cc -c does, but it looks like a Unix or Linux command to compile.
    I didnt recognize it either, but it is the way what my book told me to use my own class.

  7. #7
    Registered User billholm's Avatar
    Join Date
    Apr 2002
    Posts
    225

    Lightbulb

    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

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,707
    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 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;
    }

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,707
    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 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;
    }

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,707
    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 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;
    }

  11. #11
    Registered User billholm's Avatar
    Join Date
    Apr 2002
    Posts
    225

    Talking

    >>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

  12. #12
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    Post

    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~

  13. #13
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    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

  14. #14
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    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>
    thanx, salvelinus.
    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~

  15. #15
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    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

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 08:10 AM
  2. Two conceptual questions
    By AntiScience in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2007, 11:36 AM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

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