Thread: Class vehicle with virtual function

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

    Class vehicle with virtual function

    Hi everyone, I wrote a rough draft of a code that is supposed return the type of car you are driving. I kind of got the hang of it but wanted to go ahead in the book that I am reading and implement a virtual function. I read about them but was not sure how I could implement them. I wanted to have it so that after the program looked at all of the data entered..it would then printout the sound that the car was supposed to make (horn function) sportscar would make a "beep" and suv would make a "BEEP"..something like that.
    Am i correct in the fact that virtual function is a function that can be accessed and changed by the entire code???
    Is what I am trying to do possible or do I need to start over?

    Any help would greatly be appreciated
    Thanks in advance..especially to SirCrono6, laasunde, and Zach L. for previous help


    Code:
    #include <iostream.h>
    
    
    class Vehicle
    {
      public:
    
        Suv();
    Sportscar();
    virtual horn ();
      private:
        int doors;
        int cylinders;
    	int name;
    	int color;
    	char transmission;
    	int a;
    	int m;
    };
    
    
    Vehicle::Suv()
    {
     cout<<" How many doors does the car have? ";
     cin>>doors;
    cout<<" How many cylinders does the car have? ";
    cin>>cylinders;
    cout<< " What kind of transmission does the car have? ";
    cin>> transmission;	
     if (doors>=5 && cylinders>=6 )
    	 cout<<" You are driving an SUV"<<endl;
    	else
    		cout<<" You are driving a sportscar"<<endl;
    	if (transmission=='a')
    		cout<<"with automatic transmission"<<endl;
    	else
    		cout<<"with manual transmission"<<endl;
    	
     
    }
    
    Vehicle::Sportscar()
    {
    cout<<" How many doors does the second car have? ";
    cin>>doors;
    cout<<" How many cylinders does the second car have? ";
    cin>>cylinders;
    cout<< " What kind of transmission does the second car have? ";
    cin>> transmission;	
    	
    	if (doors>=4 && cylinders>=6 )
    	 cout<<" Your second car is an SUV"<<endl;
    	else
    		cout<<" Your second car is a sportscar"<<endl;
    	if (transmission=='a')
    		cout<<"with automatic transmission<<endl";
    	else
    		cout<<"with manual transmission"<<endl;
    }
    
    int main()
    {
    	Vehicle c;
    	
    	c.Suv();
    	c.Sportscar();
    
    return 0;
    }

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Virtual functions actually work a bit differently. They deal with when you have classes deriving from one another. Basically, if you have a pointer or a reference to a base class, that actually points to a derived class, then calling a virtual function f() will call the function belonging to the derived class. If it is not virtual, then the base class's method will be called.

    Here is a short (and compilable) example that should help:
    Code:
    #include <iostream>
    
    class A
    {
    public:
      virtual void f()
      {
        std::cout << "f() called from A" << std::endl;
      }
    
      void g()
      {
        std::cout << "g() called from A" << std::endl;
      }
    };
    
    class B : public A
    {
    public:
      void f()
      {
        std::cout << "f() called from B" << std::endl;
      }
    
      void g()
      {
        std::cout << "g() called from B" << std::endl;
      }
    };
    
    int main()
    {
      B obj;
      A& ref = obj; // Base class reference pointing to an object of the derived class
      ref.f(); // Calls B::f() since f() is virtual
      ref.g(); // Calls A::g() since g() is not virtual
      std::cin.get();
    }
    So, Suv and Sportscar need to be classes that derived for vehicle, both implementing their own version of horn().
    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.

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

    Vehicle Class with Virtual Function-Almost there

    Hello, I believe that I am getting close. I just wanted to make sure that I was on the right track. I wanted to implement 2 functions for the two types of cars (suv BEEP and sportscar beep)

    Code:
    #include <iostream.h>
    
    
    class Vehicle
    {
      public:
    
        Suv();
    	Sportscar();
    	 virtual void horn()
      {
     cout << "The sound of your horn is beep" << endl;
      }
    
      void g()
      {
     cout << "The sound of your horn is BEEP" << endl;
      }
      private:
        int doors;
        int cylinders;
    	int name;
    	int color;
    	char transmission;
    	int a;
    	int m;
    };
    
    
    
    Vehicle::Suv()
    {
     cout<<" How many doors does the car have? ";
     cin>>doors;
    cout<<" How many cylinders does the car have? ";
    cin>>cylinders;
    cout<< " What kind of transmission does the car have? ";
    cin>> transmission;	
     if (doors>=5 && cylinders>=6 )
    	 cout<<" You are driving an SUV"<<endl;
    	else
    		cout<<" You are driving a sportscar"<<endl;
    	if (transmission=='a')
    		cout<<"with automatic transmission"<<endl;
    	else
    		cout<<"with manual transmission"<<endl;
    	
     
    }
    
    Vehicle::Sportscar()
    {
    cout<<" How many doors does the second car have? ";
    cin>>doors;
    cout<<" How many cylinders does the second car have? ";
    cin>>cylinders;
    cout<< " What kind of transmission does the second car have? ";
    cin>> transmission;	
    	
    	if (doors>=4 && cylinders>=6 )
    	 cout<<" Your second car is an SUV"<<endl;
    	else
    		cout<<" Your second car is a sportscar"<<endl;
    	if (transmission=='a')
    		cout<<"with automatic transmission"<<endl;
    	else
    		cout<<"with manual transmission"<<endl;
    }
    
    int main()
    {
    	Vehicle c;
    	c.Suv();
    	c.Sportscar();
    	c.g();
    
    return 0;
    }

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Sorry to have to tell you this, but you aren't really close, and you aren't on the right track. I'm not sure if you looked at Zach L.'s response. If you did, you must not have understood it, because I don't see where you implemented any of his suggestions. He showed two classes, one derived from the other. He also suggested that you make three classes, Vehicle, SUV, and Sportscar. SUV and Sportscar would derive from Vehicle in the same way that B is deriving from A in his example. Does that make sense?

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

    Thumbs up Virtual class

    Hello, I believe that now I am getting a little bit closer.

    Thanks for the input so far everyone

    Code:
    #include <iostream.h>
    
    
    class Vehicle
    {
      public:
    
        Suv();
    	Sportscar();
    	 virtual void horn()
      {
     cout << "The sound of your horn is beep" << endl;
      }
    
      void g()
      {
     cout << "The sound of your horn is BEEP" << endl;
      }
      private:
        int doors;
        int cylinders;
    	int name;
    	int color;
    	char transmission;
    	int a;
    	int m;
    };
    
    
    
    Vehicle::Suv()
    {
    
    
    	 cout<<" You are driving an SUV"<<endl;
    	
    }
    
    Vehicle::Sportscar()
    {
    
    
    	 cout<<" You are driving a sportscar"<<endl;
    	
    }
    
    int main()
    {
    Vehicle c;
    int door;
    int cylinders;
    int transmission;
    cout<<" How many doors does your car have";
    cin>>door;
    cout<<" How many cylinders does the second car have? ";
    cin>>cylinders;
    
     if (door>=5 && cylinders>=6 )
    c.Suv(),c.g() ;
    
     else
    	 c.Sportscar(),c.horn();
     
    	
    
     cout<< " What kind of transmission does the second car have? ";
    cin>> transmission;	
    
    	
    
    	
    
    return 0;
    }

  6. #6
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    You're still missing the point I'm afraid. I think you need to go back to your C++ book for an hour or two and familiarise yourself with inheritance - base classes and derived classes. Then virtual functions will become more meaningful to you. You can then come back to the example provided by Zach L and it will help you to understand how to use virtual functions. You can then try to apply it to your vehicle program.

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    The big thing you are still missing is that you need three separate classes to get your virtual functions to behave as you want them. 'Vehicle' should be your base class with the virtual 'horn' function. 'Suv ' and 'Sportscar' need to b ederived from the 'Vehicle' class, and then implement their own version of 'horn' (with the same function signature as the original).

    As Omnius suggested, it would be advisable to go back to your C++ book and look over the inheritance chapter to familiarize yourself with it a bit more.
    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.

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

    Virtual Class

    Hello everyone, I went back to the book and I believe that I have fixed some of my problems. This was a little bit more complicated than I thought. I implemented some of your suggestions to give myself a rough draft to work with. If it is still wrong I will work on this for a couple of days before I post more code.

    Thanks everyone for being patient with me

    Code:
    #include <iostream.h>
    
    class Vehicle
    {
    public:
    	
      virtual void horn()
      {
       cout << "Horn" << endl;
      }
    
    };
    
    class Suv : public Vehicle
    {
    public:
      void horn()
      {
        cout << "You cars horn goes BEEP and you are driving an SUV" << endl;
      }
    
      void b()
      {
        cout << "Your car is black with automatic transmission" << endl;
      }
    };
    
    class Sportscar : public Vehicle
    {
    public:
      void horn()
      {
        cout << "Your car's horn goes beep and you are driving a sportscar" << endl;
      }
    
      void r()
      {
        cout << "Your car is red with manual transmission" << endl;
      }
    };
    
    int main()
    {
    
    	int door;
    	int cylinders;
      cout<<" How many doors does your car have";
    cin>>door;
    cout<<" How many cylinders does your car have? ";
    cin>>cylinders;
    Suv v;  
    Sportscar c;
      Vehicle& ref = v;// Base class reference pointing to an object of the derived class
      Vehicle& refer = c;
    
     if (door>=5 && cylinders>=6 )
      
      ref.horn(),v.b(); // Calls Suv::horn() since f() is virtual
     else
      refer.horn(),c.r();
     
    }

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    You are certainly on the right path now. The virtual function with the derived classes will work just as you were wanting them to. I would however, change your other functions ('r( )' and 'b( )'). Since they store the transmission type and color, these are attributes of 'Vehicle', and could be implemented in a single function there:

    Code:
    // For a quick example...
    class Vehicle
    {
    public:
      void info( ) { cout << "The color is " << m_color << endl; }
    };
    
    // ... etc ...
    
    int main( )
    {
      Suv v;
      Sportscar r;
      v.info( );
      r.info( );
    }
    That way, you'd store the transmission type and color in the base class. You may even want to make that function virtual, so you could reimplement it to output additional information in a future derived class if necessary.

    Also, it would be advisable to make the 'Vehicle' destructor virtual to avoid nasty side-effects if you end up dealing with pointers to vehicles.
    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. Replies: 3
    Last Post: 10-28-2009, 09:25 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Problems defining classes
    By esmeco in forum C++ Programming
    Replies: 47
    Last Post: 10-24-2007, 01:13 PM
  4. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM