Thread: Beginning with classes

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    85

    Beginning with classes

    Hey, I'm starting up with classes in C++ so I made a little task to aid in learning them. Basically, I'm just wondering how it looks. Is the design good? Any flaws? I'm not worried about the actual car, I'm just trying to get an idea of how things should be done. Thanks!
    Code:
    // car.cpp
    
    #include <iostream>
    #include <stdlib.h>
    #include <windows.h>
    
    using namespace std;
    
    enum { OFF, STARTED, ACCELE, DECEL };
    
    class Car
    {
        public:
            Car();
            ~Car() { /* nothing */ };
            int GetSpeed();
            void Start();
            void Accelerate();
            void Decelerate();
            void Stop();
            
        private:
            int Speed;
            int State;
            int Time;
    };
    
    int main()
    {
        Car Corvette;
        int n;
        
        cout << "Ultra Car Simulator 3000!!\n\n";
        cout << "Controls:\n";
        cout << "   1. Start\n";
        cout << "   2. Accelerate\n";
        cout << "   3. Decelerate\n";
        cout << "   4. Current Speed\n";
        cout << "   5. Stop\n";
        cout << "   6. Quit\n";
        
        while (n != 6)
        {
            cin >> n;
            switch (n)
            {
                case 1:
                    Corvette.Start();
                    break;
                case 2:
                    Corvette.Accelerate();
                    break;
                case 3:
                    Corvette.Decelerate();
                    break;
                case 4:
                    cout << "Current speed: " << Corvette.GetSpeed() << ".\n";
                    break;
                case 5:
                    Corvette.Stop();
                    break;
                case 6:
                    break;
                default:
                    cout << "The car doesn't understart that command.\n";
                    break;
            }
        }
    
        cout << "\nVroom!\n";
        
        system("PAUSE");
        
        return 0;
    }
    
    Car::Car()
    {
        Speed = 0;
        State = OFF;
    }
    
    int Car::GetSpeed()
    {
        int Time2 = GetTickCount();
        if (State == ACCELE)
            Speed += (Time2 - Time)/100;
        else if (State == DECEL)
        {
            Speed -= (Time2 - Time)/100;
            if (Speed < 0)
            {
                Speed = 0;
                State = STARTED;
            }
        }
        Time = GetTickCount();
    
        return Speed;
    }
    
    void Car::Start()
    {
        if (State == OFF)
        {
            cout << "Car started.\n";
            State = STARTED;
        }
        else
            cout << "Car already started.\n";
    }
    
    void Car::Accelerate()
    {
        if (State == OFF)
            cout << "Car needs to be started.\n";
        else if (State == ACCELE)
            cout << "Car already accelerating.\n";
        else
        {
            cout << "Car accelerating.\n";
            State = ACCELE;
            Time = GetTickCount();
        }
    }
    
    void Car::Decelerate()
    {
        if (State == OFF)
            cout << "Car needs to be started.\n";
        else if (State == DECEL)
            cout << "Car already decelerating.\n";
        else
        {
            cout << "Car decelerating.\n";
            State = DECEL;
            Time = GetTickCount();
        }
    }
    
    void Car::Stop()
    {
        if (State == OFF)
            cout << "Car already stopped\n";
        else
        {
            cout << "Car stopped\n";
            State = OFF;
            Speed = 0;
        }
    }

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Don't use cout inside of member functions. Pass a reference to an ostream instead (can use a default value however)

    Example:
    Code:
    void Car::Start(std::ostream &out)
    {
        if (State == OFF)
        {
            out << "Car started.\n";
            State = STARTED;
        }
        else
            out << "Car already started.\n";
    }

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Pass a reference to an ostream instead (can use a default value however)
    Actually, it's better not to print anything at all inside of the class implementation except in a few special cases:

    1) Debugging statements (that are turned off with a compile switch for production release)
    2) Member functions whose sole purpose is to print something.

    Other than that, return values, status flags and exceptions should be preferred for notification purposes. The application knows what messages to print and how to print them if necessary, the object usually does not.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    85
    Alrighty, thanks for the input. So basically I need to have the functions return a value and handle the output in the main function, or create a method to handle the output. I'm thinking something like:

    Code:
    void Car::Already(int State)
    {
        switch (Type)
        {
        case STARTED:
            cout << "...";
        ...
    }
    But I guess that means I should also pass in a reference to an ostream? Or, could I create an ostream as a member of the class? Alrighty, thanks again for you help!

    Sean

  5. #5
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    Thantos and Prelude have already showed you the method to handle the output, right ?

    blow me ... ...

  6. #6
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    int n;
    ....
    while (n != 6)
    This is bad. You need to initialize 'n'.

    after "int n;", the value of 'n' is undefined (could even be 6 in which case your program wouldnt enter the loop).

    edit:
    >> system("PAUSE");

    this is a less than preferable solution, search the board to find out why.
    Last edited by Perspective; 09-25-2004 at 11:21 AM.

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Quote Originally Posted by Thantos
    void Car::Start(std:stream &out)
    {
    if (State == OFF)
    {
    out << "Car started.\n";
    State = STARTED;
    }
    else
    out << "Car already started.\n";
    }
    Quote Originally Posted by Prelude
    return values, status flags and exceptions should be preferred for notification purposes
    Quote Originally Posted by Hermitsky
    Thantos and Prelude have already showed you the method to handle the output, right ?
    It would appear that way, now wouldn't it?

  8. #8
    Registered User
    Join Date
    Jul 2003
    Posts
    85
    Please forgive my ignorance almighty C++ Masters!

    Thanks Perspective

  9. #9
    Registered User
    Join Date
    Sep 2004
    Posts
    28

    ..

    WOW, you program good cars, my cars going 287023 mph and it still hasn't crashed or come apart!(going all day)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM