Results 1 to 8 of 8

Thread: C++ Class (not school :o)

  1. #1

    Thread Starter
    Frenzied Member JasonLpz's Avatar
    Join Date
    Mar 2001
    Location
    Brooklyn, NY
    Posts
    1,335

    C++ Class (not school :o)

    Hi im back lol thank you all again for all the help. I know how to make and use a class but i was reading this book and got confused allitle. I think it said i can put a class in a header (h) file. Can i ? And i wanted to know if i have a cpp file with some code but not the main code can i still cal it like it was on the main file? or do i have to define it somehow or something

    so overview:

    1. Can i put a class in a .h
    2. How do i use a seperate cpp file that has fuctions and classes in it.
    - JayWare
    Live to love. Not to Hate

    Im to busy to have a site. But I got one and still working on it.

    http://dre3k.net/

  2. #2
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Re: C++ Class (not school :o)

    Originally posted by JasonLpz
    1. Can i put a class in a .h
    Yes.

    Originally posted by JasonLpz
    2. How do i use a seperate cpp file that has fuctions and classes in it.
    Why not use a header file? That's what they're for.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  3. #3

    Thread Starter
    Frenzied Member JasonLpz's Avatar
    Join Date
    Mar 2001
    Location
    Brooklyn, NY
    Posts
    1,335
    oh i c ok thanks a bunch
    - JayWare
    Live to love. Not to Hate

    Im to busy to have a site. But I got one and still working on it.

    http://dre3k.net/

  4. #4
    Hyperactive Member made_of_asp's Avatar
    Join Date
    Jul 2001
    Location
    123 Fake Street
    Posts
    394
    Usually the functions and classes themselves a are placed within a .cpp file, header files are used for class and function prototypes.
    VS.NET 2003

    Need to email me?

  5. #5

    Thread Starter
    Frenzied Member JasonLpz's Avatar
    Join Date
    Mar 2001
    Location
    Brooklyn, NY
    Posts
    1,335
    prototypes?
    - JayWare
    Live to love. Not to Hate

    Im to busy to have a site. But I got one and still working on it.

    http://dre3k.net/

  6. #6
    Hyperactive Member made_of_asp's Avatar
    Join Date
    Jul 2001
    Location
    123 Fake Street
    Posts
    394
    prototype example:

    (hi.h)

    Code:
    void Hi(); //prototype
    (hi.c)
    Code:
    void Hi()
    {
     printf("Hi\n");
    }
    include hi.h and use hi() function anywhere in your program.
    VS.NET 2003

    Need to email me?

  7. #7

    Thread Starter
    Frenzied Member JasonLpz's Avatar
    Join Date
    Mar 2001
    Location
    Brooklyn, NY
    Posts
    1,335
    oh i get it now thanks alot
    - JayWare
    Live to love. Not to Hate

    Im to busy to have a site. But I got one and still working on it.

    http://dre3k.net/

  8. #8
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771
    And when using classes it looks like this:

    car.h
    Code:
    // This is to ensure that the class onyl gets declared once
    #ifndef INCLUDED_MYPROJECT_CAR_H
    #define INCLUDED_MYPROJECT_CAR_H
    
    class car
    {
        bool driving_;
    public:
        void start();
        void stop();
        void crash();
    };
    
    #endif
    car.cpp
    Code:
    #include "car.h"
    #include <iostream>
    
    void car::start()
    {
        driving_ = true;
    }
    
    void car::stop()
    {
        driving_ = false;
    }
    
    void car::crash()
    {
        if (driving_) {
            std::cout << "CRASH!";
        } else {
            std::cout << "standing still, can't crash";
        }
    }

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