Pure Virtual Functions. beginner help sought.

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2009
Posts: 31
Reputation: Carrots is an unknown quantity at this point 
Solved Threads: 0
Carrots Carrots is offline Offline
Light Poster

Pure Virtual Functions. beginner help sought.

 
0
  #1
May 30th, 2009
Hi,

Hoping someone can tel me where I'm going wrong, or what I should do.

I'm trying to use some pure virtual functions on some polymorphic classess but am unsure how to do it correctly.

Hiopefully this code will explain it a bit better:

vehicle.h :
  1. using namespace std;
  2.  
  3. class Vehicle
  4. {
  5. public:
  6. Vehicle(int publicmpg = 0);
  7.  
  8. protected:
  9.  
  10. int protectedmpg;
  11. };
  12.  
  13. Vehicle::Vehicle(int publicmpg)
  14. {
  15. protectedmpg = publicmpg;
  16. }
  17.  
  18. /////////////////////////////////
  19.  
  20. class VectorClass
  21. {
  22. public:
  23.  
  24. //Declaring pure virtual function here causing Vehicle to become the ABC
  25. virtual void AddACar() = 0;
  26.  
  27. protected:
  28.  
  29. };
  30.  
  31. /////////////////////////////////
  32.  
  33. class SportsCar : public Vehicle
  34. {
  35. public:
  36. SportsCar(int publicsixspeed = 0, int publicmpg = 0);
  37.  
  38. //pure virtual member function for SportsCar
  39. void AddACar()
  40. {
  41. cout << "Inside myfunc() for SportsCar objects" << endl;
  42. }
  43.  
  44. protected:
  45. int protectedsixspeed;
  46. };
  47.  
  48. SportsCar::SportsCar(int publicsixspeed, int publicmpg) : Vehicle(publicmpg)
  49. {
  50. protectedsixspeed = publicsixspeed;
  51. }



main.cpp :
  1. #include <iostream>
  2. #include <vector>
  3. #include "Vehicle.h"
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. vector<Vehicle*> vectorname;
  10.  
  11. int publicmpg = 11;
  12. int publicsixspeed = 22;
  13.  
  14. SportsCar node(publicmpg, publicsixspeed);
  15.  
  16. SportsCar *ptrToSportsCar;
  17.  
  18. ptrToSportsCar = &node;
  19.  
  20. vectorname.push_back( ptrToSportsCar );
  21.  
  22. cout << "past push" << endl;
  23.  
  24.  
  25. vectorname[0]->AddACar();
  26.  
  27.  
  28. cout << "Past myfunc() call " << endl;
  29.  
  30. system ("PAUSE");
  31. return 0;
  32. }

Visual Studio reports:

'DeleteACar' : is not a member of 'Vehicle'
I'm going to use DeleteACar() to delete nodes from the Vector by passing which node as an argument, but before I attempt to write the function, I'm stuck as how to correctly configure a pure virtual function.

Any advice or advice is most appreciated.

Thanks!

Carrots.
Last edited by Carrots; May 30th, 2009 at 6:38 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 32
Reputation: Ahmed_I is an unknown quantity at this point 
Solved Threads: 1
Ahmed_I Ahmed_I is offline Offline
Light Poster

Re: Pure Virtual Functions. beginner help sought.

 
0
  #2
May 30th, 2009
in line 33 you should inherit from VectorClass to use the virtual method AddACar().
i just wanted to warn cuz the method AddACar() u used in main
isn't the virtual function that u declared in VectorClass.

Secondly i dont see any member function or attributes u declared named DeleteACar from where did the compiler get it??? plz tell the line that the error occured in.
Last edited by Ahmed_I; May 30th, 2009 at 7:09 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 31
Reputation: Carrots is an unknown quantity at this point 
Solved Threads: 0
Carrots Carrots is offline Offline
Light Poster

Re: Pure Virtual Functions. beginner help sought.

 
0
  #3
May 30th, 2009
Thanks very much Ahmed !!

Where am I going wrong here?

I've tried:

class SportsCar : public VectorClass

class SportsCar : public Vehicle, public VectorClass

and

class SportsCar : public VectorClass, public Vehicle

None seem to work, where am I going wrong?


EDIT - Thanks Ahmed, just seen your edit and just getting my head around it. I haven't added any functionality to the function apart from the cout as I wanted to get a simplem function working before creating it fully.
Last edited by Carrots; May 30th, 2009 at 7:20 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 32
Reputation: Ahmed_I is an unknown quantity at this point 
Solved Threads: 1
Ahmed_I Ahmed_I is offline Offline
Light Poster

Re: Pure Virtual Functions. beginner help sought.

 
0
  #4
May 30th, 2009
please could u tell me more about your problem do u want to solve the compiler's error or do u want to know how to configure a pure virtual function?
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 31
Reputation: Carrots is an unknown quantity at this point 
Solved Threads: 0
Carrots Carrots is offline Offline
Light Poster

Re: Pure Virtual Functions. beginner help sought.

 
0
  #5
May 30th, 2009
Solve the compiler's error for sure, that would be great.

Thanks very much, your taking the time to help me out is most apreciated 100%!
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 32
Reputation: Ahmed_I is an unknown quantity at this point 
Solved Threads: 1
Ahmed_I Ahmed_I is offline Offline
Light Poster

Re: Pure Virtual Functions. beginner help sought.

 
0
  #6
May 30th, 2009
ok the compiler's error is
  1. DeleteACar' : is not a member of 'Vehicle'
  2.  

and in your code i dont see any data or function named DeleteACar
so could u please tell check your code for DeleteACar.

But i think that u declared a vector with type Vehicle* and u pushed a pointer with type Sportscar .
when u say
vectorname[0]->AddACar();
it means that u call AddACar() of the vehicle class which vehicle class hasnt this method .

So the solution is let the vehicle class be derived from VectorClass and implement AddACar() method for the vehicle class.
Your problem is in Polymorphism .
I hope that this is the solution for your problem.
Last edited by Ahmed_I; May 30th, 2009 at 8:05 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 119
Reputation: JugglerDrummer is an unknown quantity at this point 
Solved Threads: 15
JugglerDrummer's Avatar
JugglerDrummer JugglerDrummer is offline Offline
Junior Poster

Re: Pure Virtual Functions. beginner help sought.

 
0
  #7
May 30th, 2009
AddAcar should be a method of the vector, and probably deleteacar too judging by their names. SportsCar should inherit only from vehicle. The virtual function should be declared in vehicle (don't need =0; after function declaration ) and then overriden in sportscar.
92% of all statistics are made up on the spot.

If you found a post helpful, please click the "give XXX reputation" link, to show your appreciation to those who helped you. Thanks! :D
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 31
Reputation: Carrots is an unknown quantity at this point 
Solved Threads: 0
Carrots Carrots is offline Offline
Light Poster

Re: Pure Virtual Functions. beginner help sought.

 
0
  #8
May 30th, 2009
Noticing the 'solved threads' username tag, I can now say this thread is fully solved aswell!

Thank you so much, the classes are now fully working.

This is a great forum.

Here are the working classes now.

Using the VectorClass as the Interface makes everything flow down through the inherited classes nicely.


  1. using namespace std;
  2.  
  3.  
  4. class VectorClass
  5. {
  6. public:
  7.  
  8. //Declaring pure virtual function here causing VectorClass to become an A.B.C.
  9. virtual void DeleteACar() = 0;
  10.  
  11. protected:
  12.  
  13. };
  14.  
  15. /////////////////////////////////
  16.  
  17.  
  18. class Vehicle : public VectorClass
  19. {
  20. public:
  21. Vehicle(int publicmpg = 0);
  22.  
  23. protected:
  24.  
  25. int protectedmpg;
  26. };
  27.  
  28. Vehicle::Vehicle(int publicmpg)
  29. {
  30. protectedmpg = publicmpg;
  31. }
  32.  
  33. /////////////////////////////////
  34.  
  35.  
  36.  
  37. /////////////////////////////////
  38.  
  39. //class SportsCar : public Vehicle
  40. class SportsCar : public Vehicle
  41. {
  42. public:
  43. SportsCar(int publicsixspeed = 0, int publicmpg = 0);
  44.  
  45. //pure virtual member function for SportsCar
  46. void DeleteACar()
  47. {
  48. cout << "Inside DeleteACar() for SportsCar objects" << endl;
  49. }
  50.  
  51. protected:
  52. int protectedsixspeed;
  53. };
  54.  
  55. SportsCar::SportsCar(int publicsixspeed, int publicmpg) : Vehicle(publicmpg)
  56. {
  57. protectedsixspeed = publicsixspeed;
  58. }
  59.  
  60.  
  61. //create the virtual function
  62.  
  63. //void DeleteACar(pointer to car or location within vector[])
  64. void DeleteACar()
  65. {
  66.  
  67. // int x = vectorsize();
  68.  
  69. cout << "Vector size is : "<< size() << endl;
  70.  
  71. //for(int a = 0;a< vector size();a++)
  72.  
  73. //{
  74. // if vectorname[a] = match
  75.  
  76. // {
  77. // delete vectorname[a];
  78. // break;
  79. // }
  80. //}
  81.  
  82. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC