Simple Question about Classes

This is a discussion on Simple Question about Classes within the C++ Programming forums, part of the General Programming Boards category; I have read a bunch of information on classes, but I am still confused about something. In Java, a class ...

  1. #1
    System.out.println("");
    Join Date
    Jan 2005
    Posts
    84

    Simple Question about Classes

    I have read a bunch of information on classes, but I am still confused about something. In Java, a class was a self-contained entity in that if I made a Car class there were two things:

    1. A Car.java file that contained all of the member functions and data


    2. The instantiation of the object in another file. Of course, the class could be created within another class, but typically, at least in my experience, the Car class would be in a seperate file. I would then instantiate the object and access its functions.


    To me, this was very intuitive. Now with C++ it appears to be a little different. With C++ the way understand it is that there are three things:

    1. Class definition which would be something similar to this:

    Code:
    class Car
    {
          public:
                    int speed;
                    int mileage; 
                    int getSpeed();
                    void setSpeed(int speed);
                    int getMileage();
                    void setMileage(int mileage);
                 
    };

    2. Class implementation which would be something similar to this:

    Code:
    int Car::getSpeed()
    {
         return (speed);
    }
    
    void Car::setSpeed(int speed2)
    {
          speed = speed2;
    }
    
    int Car::getMileage()
    {
         return (mileage);
    }
    
    void Car::setMileage(int mileage2)
    {
          mileage = mileage2;
    }

    3. Then in a seperate file, typically at least, you would instantiate an object of the class to access its functions and data. Now do you put all of this in a .hpp (the class definition and class implementation) and then #include it where you want it? Are the class definition and class implementation put in the same file? For instance:

    Car.hpp

    Code:
    #include <iostream>
    
    using namespace std;
    
    class Car
    {
          public:
                    int speed;
                    int mileage; 
                    int getSpeed();
                    void setSpeed(int speed);
                    int getMileage();
                    void setMileage(int mileage);
                 
    };
    
    int Car::getSpeed()
    {
         return (speed);
    }
    
    void Car::setSpeed(int speed2)
    {
          speed = speed2;
    }
    
    int Car::getMileage()
    {
         return (mileage);
    }
    
    void Car::setMileage(int mileage2)
    {
          mileage = mileage2;
    }
    Driver.cpp

    Code:
    #include <iostream>
    #include "Car.hpp"
    using namespace std;
    
    int main()
    {
        Car car;
        car.setSpeed(100);
        cout<<"The speed is: "<<car.getSpeed()<<"\n";
        cin.ignore();
        cin.get();
    }

    I realize that this is really a silly class, but I was just using it to try to help get my point across. When people make classes do they make them and then #include the .hpp file in the other classes they want to use it in or do they typically just put the class directly in that file?

    On this site (http://www.cprogramming.com/tutorial/lesson12.html) they directly put the class code in the file, but to me this seems to get rid of a certain level of abstraction. If it's in another file you know when I instantiate an object I have access to these functions and this member data without having to know any of the inner workings.

    Sorry if this doesn't make a lot of sense. I have the idea in my head; I am just having trouble communicating it. Thanks in advance guys.

  2. #2
    Registered User
    Join Date
    Apr 2004
    Posts
    42
    Heh heh, I've just been doing this.

    Put the class...

    Code:
    class Car
    {
          public:
                    int speed;
                    int mileage; 
                    int getSpeed();
                    void setSpeed(int speed);
                    int getMileage();
                    void setMileage(int mileage);
                 
    };
    in a header file. (.h)


    And put the implementation in a .cpp file.

    Then you do

    #include "Car.h"

    in the file where you have main(), and then make your linker link the implementation file(Car.cpp or whatever). Thats how I learned it.
    Last edited by Link_26; 06-25-2006 at 04:54 PM.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,578
    Classes are generally defined in hpp files and implemented in cpp files. Both files usually have the names of the class followed by the hpp or cpp extension. This is a typical barebones approach.

    However, the cpp file that implements the class (this is what I think is confusing you) is a contained file. It stores only the class members definitions and little else.

    So... imagining a very simple program that implements a class car...

    Code:
    --- Car.hpp ----
    class Car {
     /* class members declarations */
    };
    
    --- Car.cpp ---
    #include "Car.h"
    
    int Car::shift(int gear) {
       /* implement what happens when the driver shifts gears */
    }
    
    --- main.cpp ---
    #include "Car.h"
    
    Car myCar = "Toyota";
    
    myCar.start();
    myCar.shift(2);
    
    return 0;
    Note how the Car.cpp file that implements your class members takes no direct part on the flow of the program. It simply needs to be added to your project (along with the header file, of course). Calls to your class members will eventually access this cpp file. But the user of the class need not know anything about it.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> On this site (http://www.cprogramming.com/tutorial/lesson12.html) they directly put the class code in the file.
    That is just an example that puts everything in a single file (which is allowed) to make it easier for the user to copy the code and run it. Separating the code into files as Mario F. showed is the normal way to do it in anything but the smallest programs. In C++ the placement of class definition in one file (header) and implementation in another file (source) provides an extra layer above what Java provides to hide the implementation details.

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Code:
    class Car
    {
          public:
                    int speed;
                    int mileage; 
                    int getSpeed();
                    void setSpeed(int speed);
                    int getMileage();
                    void setMileage(int mileage);
                 
    };
    Ok....

    Since mileage and speed are public variables, why would you need functions to get/set them?

  6. #6
    System.out.println("");
    Join Date
    Jan 2005
    Posts
    84
    Thanks for the help guys. That makes a lot more sense.


    Quote Originally Posted by Bubba
    Code:
    class Car
    {
          public:
                    int speed;
                    int mileage; 
                    int getSpeed();
                    void setSpeed(int speed);
                    int getMileage();
                    void setMileage(int mileage);
                 
    };
    Ok....

    Since mileage and speed are public variables, why would you need functions to get/set them?
    You wouldn't. This was just an old class that I found that I decided to use for this example, i.e., I made this before I knew that and then just copied for this example.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Classes Question
    By kbro3 in forum C++ Programming
    Replies: 9
    Last Post: 08-14-2008, 08:43 AM
  2. Simple C# structure question
    By sketch in forum C# Programming
    Replies: 4
    Last Post: 09-14-2007, 05:29 PM
  3. Question about Classes
    By WildFire in forum C++ Programming
    Replies: 8
    Last Post: 06-18-2004, 08:57 PM
  4. Flowchart question: Classes
    By darnok in forum C++ Programming
    Replies: 6
    Last Post: 06-17-2004, 07:25 PM
  5. Simple yes/no question regarding classes
    By Ricochet in forum C++ Programming
    Replies: 5
    Last Post: 12-11-2003, 05:06 PM

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