Need help w/ c++

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2009
Posts: 7
Reputation: connoisseurodg is an unknown quantity at this point 
Solved Threads: 0
connoisseurodg connoisseurodg is offline Offline
Newbie Poster

Need help w/ c++

 
0
  #1
Apr 12th, 2009
I have been rushing trying to get this done by Monday 2am est. I'm trying to create a new project which consists of at least two classes: a base class and a derived class. The code of your project should include

* a composite object
* an example of inheritance
* at least one virtual and at least one pure virtual function
* at least one overloaded function
* at least one example of pointers
* at least one example of dynamic memory allocation

My code is garbled and incomplete, due to the fact I'm learning about pointers & dynamic memory allocation while putting this all together. It's definitely still a "work" in progress, and I'm moving everything around. I'm trying to determine how to represent the 'composition' portion of my code. Should I use ammo? Since the two derived classes have different amounts of it? Or Declare ammo as a virtual function since the sub-classes will represent different numbers - but both have ammo?
  1. // Week6iLab_RyanRodgers.cpp : main project file.
  2.  
  3. #include "stdafx.h"
  4. #include <iostream>
  5. #include <string>
  6. #include <iomanip>
  7. #include <fstream>
  8.  
  9. using namespace std;
  10.  
  11. class Gun //abstract class
  12. {
  13. public:
  14. Gun() { };
  15. string name;
  16. void shoot(int, double, int, int, int);
  17. virtual void reload() = 0;
  18. virtual void print() = 0; //pure virtual function
  19. virtual void damage() = 0; //virtual function
  20. virtual ~Gun(); //virtual destructor
  21. static int ammo;
  22. };
  23.  
  24. void shoot(int ammo, double hitPercent, int hit, int miss, int shotsFired)
  25. {
  26. ammo-- ;
  27. shotsFired = hit + miss;
  28. hitPercent = hit / shotsFired;
  29. miss = shotsFired - hit;
  30. void print();
  31. cout << "Shots: " << shotsFired << endl;
  32. cout << "Hit %: " << hitPercent << endl;
  33. }
  34.  
  35. class MachineGun : public Gun //example of inheritance & derived class
  36. {
  37. public:
  38. void setGunData(string name, void(*shoot)(void);
  39. void shoot();
  40. void print();
  41. };
  42.  
  43. void
  44.  
  45. class Pistol : public Gun
  46. {
  47. public:
  48. int ammo = 7;
  49. };
  50.  
  51.  
  52. int main()
  53. {
  54. Gun myGun;
  55. myGun.shotsFired=shoot();
  56. Gun *myGun();
  57. myGun = new Gun();
  58. myGun->ammo = hitPercent;
  59. return 0;
  60. }
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 376
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Need help w/ c++

 
0
  #2
Apr 12th, 2009
  1. void shoot(int ammo, double hitPercent, int hit, int miss, int shotsFired)

should be

  1. void gun::shoot(int ammo, double hitPercent, int hit, int miss, int shotsFired)

Do you know what a static data member means in C++? It seems unlikely that every gun shares the same number of bullets.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 7
Reputation: connoisseurodg is an unknown quantity at this point 
Solved Threads: 0
connoisseurodg connoisseurodg is offline Offline
Newbie Poster

Re: Need help w/ c++

 
0
  #3
Apr 12th, 2009
Originally Posted by Clockowl View Post
  1. void shoot(int ammo, double hitPercent, int hit, int miss, int shotsFired)

should be

  1. void gun::shoot(int ammo, double hitPercent, int hit, int miss, int shotsFired)

Do you know what a static data member means in C++? It seems unlikely that every gun shares the same number of bullets.
I toyed around with the code a little bit. Your posts, have helped immensely. I'm still a little bit shaky on how to print the "Shots: " and Hit %: ". Here's what I have so far:
  1. // Week6iLab_RyanRodgers.cpp : main project file.
  2. #include "stdafx.h"
  3. #include <iostream>
  4. #include <string>
  5. #include <iomanip>
  6. #include <fstream>
  7.  
  8. using namespace std;
  9.  
  10. class Gun //base class
  11. {
  12. protected:
  13. int shotsFired;
  14. int hit;
  15. int miss;
  16. double hitPercent;
  17. string name;
  18.  
  19. int ammo;
  20.  
  21. public:
  22. Gun();
  23. Gun(int); // overloaded constructor
  24. Gun(int, double);
  25. virtual ~Gun(); //virtual destructor
  26.  
  27. void shoot(int, double, int, int, int);
  28. void reload();
  29. virtual void print() = 0; //pure virtual function
  30. virtual void damage(); //virtual function
  31.  
  32. };
  33.  
  34. // Define functions for class
  35. Gun::Gun()
  36. {
  37. shotsFired = 0;
  38. hit = 0;
  39. miss = 0;
  40. hitPercent = 0.0;
  41. name = " ";
  42. ammo = 0;
  43. }
  44.  
  45. Gun::Gun(int rounds)
  46. {
  47. if (rounds >= 0) {
  48. ammo = rounds;
  49. }
  50. }
  51.  
  52. void Gun::shoot(int ammo, double hitPercent, int hit, int miss, int shotsFired)
  53. {
  54. ammo--;
  55. shotsFired = hit + miss;
  56. hitPercent = hit / shotsFired;
  57. miss = shotsFired - hit;
  58. }
  59.  
  60. void Gun::Gun(int, double)
  61. {
  62. cout << "Shots: " << shotsFired << endl;
  63. cout << "Hit %: " << hitPercent << endl;
  64. }
  65.  
  66. void Gun::shoot(int, double, int, int, int)
  67.  
  68. Gun::~Gun() { } //destructor
  69.  
  70.  
  71.  
  72. class MachineGun : public Gun //example of inheritance & derived class
  73. {
  74. public:
  75. MachineGun();
  76. void print();
  77. };
  78.  
  79.  
  80. // MachineGun class constructor, passes 30 rounds to parents
  81. // overloaded constructor
  82. MachineGun::MachineGun() :Gun(30)
  83. {
  84. name = "Machine Gun";
  85. }
  86.  
  87. void MachineGun::print()
  88. {
  89. cout << "Your machine gun has " << ammo << " shots left." << endl;
  90. }
  91.  
  92. class Pistol : public Gun
  93. {
  94. public:
  95. Pistol();
  96. void print();
  97. };
  98.  
  99. Pistol::Pistol() :Gun(7)
  100. {
  101. name = "Pistol";
  102. }
  103.  
  104. void Pistol::print()
  105. {
  106. cout << "Your pistol has " << ammo << " shots left." << endl;
  107. }
  108.  
  109.  
  110. int main()
  111. {
  112. MachineGun *mGun = new MachineGun(); // dynamic allocation of pointer
  113. mGun->print(); // prints number of bullets left
  114.  
  115. Pistol *mPistol = new Pistol();
  116. mPistol->print();
  117.  
  118. // free these pointers' memory
  119. delete(mGun);
  120. delete(mPistol);
  121. }
I have to construct the main method so that it will test each and every member function of the parent and the child classes. Could I scrap the 'damage' as a virtual function, and replace it with 'reload' as a virtual func?
Last edited by connoisseurodg; Apr 12th, 2009 at 7:55 pm. Reason: quick question
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 376
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Need help w/ c++

 
0
  #4
Apr 12th, 2009
Hmz. You want to split those classes up in files perhaps. Do you know how to do so? If you do, please do. It clears up the structure of your program a lot (and saves compiling time, yeey). But it's not necessary of course.

Oh, and normally people code classes like this..

  1. class monkey {
  2. public:
  3. protected:
  4. private:
  5. };

In that order, functions first, variables last. But again no biggy.

You seem to have an aweful lot of constructors:

  1. Gun::Gun()
  2. {
  3. shotsFired = 0;
  4. hit = 0;
  5. miss = 0;
  6. hitPercent = 0.0;
  7. name = " ";
  8. ammo = 0;
  9. }
  10.  
  11. Gun::Gun(int rounds)
  12. {
  13. if (rounds >= 0) {
  14. ammo = rounds;
  15. }
  16. }
  17.  
  18. void Gun::Gun(int, double)
  19. {
  20. cout << "Shots: " << shotsFired << endl;
  21. cout << "Hit %: " << hitPercent << endl;
  22. }

And some double functions...

  1. void Gun::shoot(int ammo, double hitPercent, int hit, int miss, int shotsFired)
  2. {
  3. ammo--;
  4. shotsFired = hit + miss;
  5. hitPercent = hit / shotsFired;
  6. miss = shotsFired - hit;
  7. }
  8.  
  9. void Gun::shoot(int, double, int, int, int)

My first suggestion is to clean the code up a bit, it'll make it a lot more readable and doesn't take very long.

Are you stuck on anything?
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 376
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Need help w/ c++

 
0
  #5
Apr 12th, 2009
...
Last edited by Clockowl; Apr 12th, 2009 at 8:00 pm. Reason: See next post, sorry.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 376
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Need help w/ c++

 
0
  #6
Apr 12th, 2009
Originally Posted by connoisseurodg View Post
Could I scrap the 'damage' as a virtual function, and replace it with 'reload' as a virtual func?
Decide that for yourself, do you know the difference between a virtual function and a non-virtual function? It's quite an advanced subject.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 7
Reputation: connoisseurodg is an unknown quantity at this point 
Solved Threads: 0
connoisseurodg connoisseurodg is offline Offline
Newbie Poster

Re: Need help w/ c++

 
0
  #7
Apr 12th, 2009
Originally Posted by Clockowl View Post
Decide that for yourself, do you know the difference between a virtual function and a non-virtual function? It's quite an advanced subject.
Advanced/brutal - this language is kicking my a$$. I somehow remain determined though. I believe the difference between a VF & N-VF is that with N-VF's the member function is selected at compile-time based on the type of the pointer (or reference) to the object. And VF's are resolved dynamically, based on the type of the object, not the type of the pointer/reference to that object.

I think I'm getting caught up in the specifics/differences between the pure virtual & virtual functions. Pure is defined by the derived class, so I guess the virtual would be...
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 376
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Need help w/ c++

 
0
  #8
Apr 12th, 2009
Well said! That's indeed the difference between them. Pure virtual functions are just there in the base class when you want to be able to dynamically look it up in derived classes, but don't have an implementation for it in the base: it MUST be defined in the derived classes! That's all I reckon.

Edit: Wiki has a good example:
Originally Posted by Wikipedia
As an example, an abstract base class "MathSymbol" may provide a pure virtual function doOperation(), and derived classes "Plus" and "Minus" implement doOperation() to provide concrete implementations. Implementing doOperation() would not make sense in the "MathSymbol" class as "MathSymbol" is an abstract concept whose behaviour is defined solely for each given kind (subclass) of "MathSymbol". Similarly, a given subclass of "MathSymbol" would not be complete without an implementation of doOperation().
So, split up those files, clean up the code a bit and restate your problem please.
Last edited by Clockowl; Apr 12th, 2009 at 9:10 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 7
Reputation: connoisseurodg is an unknown quantity at this point 
Solved Threads: 0
connoisseurodg connoisseurodg is offline Offline
Newbie Poster

Re: Need help w/ c++

 
0
  #9
Apr 13th, 2009
Originally Posted by Clockowl View Post
Well said! That's indeed the difference between them. Pure virtual functions are just there in the base class when you want to be able to dynamically look it up in derived classes, but don't have an implementation for it in the base: it MUST be defined in the derived classes! That's all I reckon.

Edit: Wiki has a good example:
So, split up those files, clean up the code a bit and restate your problem please.
Now my problem is with a library (I think). I'm getting 111 errors, and it starts with the ostream file. I don't know.
  1. // Week6iLab_RyanRodgers.cpp : main project file.
  2. #include "stdafx.h"
  3. #include <iostream>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. class Gun //base class
  9. {
  10. public:
  11. Gun();
  12. Gun(int); // overloaded constructor
  13. virtual ~Gun(); //virtual destructor
  14.  
  15. void shoot(int, double, int, int, int);
  16. virtual void print() = 0; //pure virtual function
  17. virtual void reload(); //virtual function
  18.  
  19. protected:
  20. int shotsFired;
  21. int hit;
  22. int miss;
  23. double hitPercent;
  24. string name;
  25. int ammo;
  26. };
  27.  
  28. // constructor w/ no parameters/define functions for class
  29. Gun::Gun()
  30. {
  31. shotsFired = 0;
  32. hit = 0;
  33. miss = 0;
  34. hitPercent = 0.0;
  35. name = " ";
  36. ammo = 0;
  37. }
  38.  
  39. Gun::Gun(int rounds)
  40. {
  41. if (rounds >= 0) {
  42. ammo = rounds;
  43. }
  44. }
  45.  
  46. void Gun::shoot(int ammo, double hitPercent, int hit, int miss, int shotsFired)
  47. {
  48. ammo--;
  49. shotsFired = hit + miss;
  50. hitPercent = hit / shotsFired;
  51. miss = shotsFired - hit;
  52. cout << "Shots: " << shotsFired << endl;
  53. cout << "Hit %: " << hitPercent << endl;
  54. }
  55.  
  56. Gun::~Gun() { } //destructor
  57.  
  58.  
  59.  
  60. class MachineGun : public Gun //example of inheritance & derived class
  61. {
  62. public:
  63. MachineGun();
  64. void print();
  65. };
  66.  
  67.  
  68. // MachineGun class constructor, passes 30 rounds to parents
  69. // overloaded constructor
  70. MachineGun::MachineGun() :Gun(30)
  71. {
  72. name = "Machine Gun";
  73. }
  74.  
  75. void MachineGun::print()
  76. {
  77. cout << "Your machine gun has " << ammo << " shots left." << endl;
  78. }
  79.  
  80. class Pistol : public Gun
  81. {
  82. public:
  83. Pistol();
  84. void print();
  85. };
  86.  
  87. Pistol::Pistol() :Gun(7)
  88. {
  89. name = "Pistol";
  90. }
  91.  
  92. void Pistol::print()
  93. {
  94. cout << "Your pistol has " << ammo << " shots left." << endl;
  95. }
  96.  
  97.  
  98. int main()
  99. {
  100. MachineGun *mGun = new MachineGun(); // dynamic allocation of pointer
  101. mGun->print(); // prints number of bullets left
  102.  
  103. Pistol *mPistol = new Pistol();
  104. mPistol->print();
  105.  
  106. // free these pointers' memory
  107. delete(mGun);
  108. delete(mPistol);
  109. return ammo;
  110. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 7
Reputation: connoisseurodg is an unknown quantity at this point 
Solved Threads: 0
connoisseurodg connoisseurodg is offline Offline
Newbie Poster

Re: Need help w/ c++

 
0
  #10
Apr 13th, 2009
Scratch that last code, here is the same project - a bit cleaned up (but isn't easy on the eyes). I'm still getting those 111 errors that refer me back to some tic-tac-toe project I worked on some while back.
  1. // Week6iLab_RyanRodgers.cpp : main project file
  2. #include "stdafx.h"
  3. #include <iostream>
  4. #include <string>
  5. #include <iomanip>
  6.  
  7. using namespace std;
  8.  
  9. class Gun //base class
  10. {
  11. public:
  12. Gun();
  13. Gun(int); // overloaded constructor
  14. virtual ~Gun(); //virtual destructor
  15. void shoot(int, double, int, int, int);
  16. virtual void print() = 0; //pure virtual function
  17. virtual void reload(); //virtual function
  18.  
  19. protected:
  20. int shotsFired;
  21. int hit;
  22. int miss;
  23. double hitPercent;
  24. string name;
  25. int ammo;
  26. };
  27.  
  28. // constructor w/ no parameters/define functions for class
  29. Gun::Gun()
  30. {
  31. shotsFired = 0;
  32. hit = 0;
  33. miss = 0;
  34. hitPercent = 0.0;
  35. name = " ";
  36. ammo = 0;
  37. }
  38.  
  39. Gun::Gun(int rounds)
  40. {
  41. if (rounds >= 0) {
  42. ammo = rounds;
  43. }
  44. }
  45.  
  46. void Gun::shoot(int ammo, double hitPercent, int hit, int miss, int shotsFired)
  47. {
  48. ammo--;
  49. shotsFired = hit + miss;
  50. hitPercent = hit / shotsFired;
  51. miss = shotsFired - hit;
  52. void print();
  53. cout << "Shots: " << shotsFired << endl;
  54. cout << "Hit %: " << hitPercent << endl;
  55. if(ammo==0){
  56. MachineGun::reload();
  57. Pistol::reload();
  58. }
  59. }
  60.  
  61. Gun::~Gun() { } //destructor
  62.  
  63.  
  64.  
  65. class MachineGun : public Gun //example of inheritance & derived class
  66. {
  67. public:
  68. MachineGun();
  69. if(ammo==0){
  70. void reload(30);
  71. }
  72. void print();
  73. };
  74.  
  75.  
  76. // MachineGun class constructor, passes 30 rounds to parents
  77. // overloaded constructor
  78. MachineGun::MachineGun() :Gun(30)
  79. {
  80. name = "Machine Gun";
  81. }
  82.  
  83. void MachineGun::print()
  84. {
  85. cout << "Your machine gun has " << ammo << " shots left." << endl;
  86. }
  87.  
  88. class Pistol : public Gun
  89. {
  90. public:
  91. Pistol();
  92. if(ammo==0){
  93. void reload(7);
  94. }
  95. void print();
  96. };
  97.  
  98. Pistol::Pistol() :Gun(7)
  99. {
  100. name = "Pistol";
  101. }
  102.  
  103. void Pistol::print()
  104. {
  105. cout << "Your pistol has " << ammo << " shots left." << endl;
  106. }
  107.  
  108.  
  109. int main()
  110. {
  111. MachineGun *mGun = new MachineGun(); // dynamic allocation of pointer
  112. mGun->print(); // prints number of bullets left
  113.  
  114. Pistol *mPistol = new Pistol();
  115. mPistol->print();
  116.  
  117. Gun shoot(5);
  118.  
  119. // free these pointers' memory
  120. delete(mGun);
  121. delete(mPistol);
  122. return 0;
  123. }
Reply With Quote Quick reply to this message  
Reply

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




Views: 592 | Replies: 10
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC