Thread: Class for vehicles

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    48

    Unhappy Class for vehicles

    Hi everyone, I wanted to write a class allowing a user to enter information about his or her car. I already have an idea of how to write it but for some reason I feel that something is wrong in the way I've written it.
    I haven't completed the code since I just started it but I wanted it to take in all of the information (doors, cylinders, and transmission) and output the type of car the user has (suv/sportrscar). I've done it with the doors already just to give you an example.

    Please let me know if I'm on the right track or if I will cause a lot of errors if I continue to complete the code this way.

    Code:
    #include <iostream.h>
    
    
    class Vehicle
    {
      public:
    
        Suv();
    	Sportscar();
      private:
        int doors;
        int cylinders;
    	int name;
    	int color;
    	int transmission;
    };
    
    
    Vehicle::Suv()
    {
     cout<<" How many doors does the car have? ";
     cin>>doors;
    	if (doors>=4)
    	 cout<<" You are driving an SUV"<<endl;
    	else
    		cout<<" You are driving a sportscar"<<endl;
    	
    
     
    }
    
    Vehicle::Sportscar()
    {
    	cout<<" How many doors does the car have? ";
    	cin>>doors;
    	
    	if (doors<4)
    		cout<<" You are driving a sportscar"<<endl;
    		else
    		cout<<" You are driving an SUV"<<endl;
    }
    
    int main()
    {
    	Vehicle c;
    	
    	c.Suv();
    	c.Sportscar();
    
    return 0;
    }

  2. #2
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    Well, I'm pretty new to C++, so I can't help you much. But I did notice one thing. If you have a compiler that's up to the standard then you will get a error about deprecated headers. Simply change

    Code:
    #include <iostream.h>
    to

    Code:
    #include <iostream>
    -SirCrono6
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Maybe something like this :
    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    class Vehicle
    {
      public:
      Vehicle(int, char*);
      ~Vehicle();
      void print();
      private:
      char *type;
      int doors;
    };
    
    Vehicle::Vehicle(int d, char *t)
    {
        doors = d;
        int len = strlen(t);
        type = new char[len+1];  
        strcpy(type,t);
    }
    
    Vehicle::~Vehicle()
    {
        delete type;
    }
    
    void Vehicle::print()
    {
        cout << "The " << type << " has " << doors << " doors." << endl;
    }
    
    int main()
    {
      char buf[] = "SUV";
      Vehicle c(4, buf);
      c.print();
      system("PAUSE");	
      return 0;
    }
    You might also want to look into inheritance where you have one base class 'vehicular' with a suv and sportcar class that inherit from the base class.

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    For the transmission type, I'd use an enum:
    Code:
    class Vehicle
    {
    public:
      enum transmission_type = { manual, automatic };
    private:
      transmission_type transmission;
    };
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  2. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 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