| | |
Need help w/ c++
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2009
Posts: 7
Reputation:
Solved Threads: 0
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?
* 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?
C++ Syntax (Toggle Plain Text)
// Week6iLab_RyanRodgers.cpp : main project file. #include "stdafx.h" #include <iostream> #include <string> #include <iomanip> #include <fstream> using namespace std; class Gun //abstract class { public: Gun() { }; string name; void shoot(int, double, int, int, int); virtual void reload() = 0; virtual void print() = 0; //pure virtual function virtual void damage() = 0; //virtual function virtual ~Gun(); //virtual destructor static int ammo; }; void shoot(int ammo, double hitPercent, int hit, int miss, int shotsFired) { ammo-- ; shotsFired = hit + miss; hitPercent = hit / shotsFired; miss = shotsFired - hit; void print(); cout << "Shots: " << shotsFired << endl; cout << "Hit %: " << hitPercent << endl; } class MachineGun : public Gun //example of inheritance & derived class { public: void setGunData(string name, void(*shoot)(void); void shoot(); void print(); }; void class Pistol : public Gun { public: int ammo = 7; }; int main() { Gun myGun; myGun.shotsFired=shoot(); Gun *myGun(); myGun = new Gun(); myGun->ammo = hitPercent; return 0; }
cpp Syntax (Toggle Plain Text)
void shoot(int ammo, double hitPercent, int hit, int miss, int shotsFired)
should be
cpp Syntax (Toggle Plain Text)
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.
•
•
Join Date: Mar 2009
Posts: 7
Reputation:
Solved Threads: 0
•
•
•
•
cpp Syntax (Toggle Plain Text)
void shoot(int ammo, double hitPercent, int hit, int miss, int shotsFired)
should be
cpp Syntax (Toggle Plain Text)
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.
C++ Syntax (Toggle Plain Text)
// Week6iLab_RyanRodgers.cpp : main project file. #include "stdafx.h" #include <iostream> #include <string> #include <iomanip> #include <fstream> using namespace std; class Gun //base class { protected: int shotsFired; int hit; int miss; double hitPercent; string name; int ammo; public: Gun(); Gun(int); // overloaded constructor Gun(int, double); virtual ~Gun(); //virtual destructor void shoot(int, double, int, int, int); void reload(); virtual void print() = 0; //pure virtual function virtual void damage(); //virtual function }; // Define functions for class Gun::Gun() { shotsFired = 0; hit = 0; miss = 0; hitPercent = 0.0; name = " "; ammo = 0; } Gun::Gun(int rounds) { if (rounds >= 0) { ammo = rounds; } } void Gun::shoot(int ammo, double hitPercent, int hit, int miss, int shotsFired) { ammo--; shotsFired = hit + miss; hitPercent = hit / shotsFired; miss = shotsFired - hit; } void Gun::Gun(int, double) { cout << "Shots: " << shotsFired << endl; cout << "Hit %: " << hitPercent << endl; } void Gun::shoot(int, double, int, int, int) Gun::~Gun() { } //destructor class MachineGun : public Gun //example of inheritance & derived class { public: MachineGun(); void print(); }; // MachineGun class constructor, passes 30 rounds to parents // overloaded constructor MachineGun::MachineGun() :Gun(30) { name = "Machine Gun"; } void MachineGun::print() { cout << "Your machine gun has " << ammo << " shots left." << endl; } class Pistol : public Gun { public: Pistol(); void print(); }; Pistol::Pistol() :Gun(7) { name = "Pistol"; } void Pistol::print() { cout << "Your pistol has " << ammo << " shots left." << endl; } int main() { MachineGun *mGun = new MachineGun(); // dynamic allocation of pointer mGun->print(); // prints number of bullets left Pistol *mPistol = new Pistol(); mPistol->print(); // free these pointers' memory delete(mGun); delete(mPistol); }
Last edited by connoisseurodg; Apr 12th, 2009 at 7:55 pm. Reason: quick question
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..
In that order, functions first, variables last. But again no biggy.
You seem to have an aweful lot of constructors:
And some double functions...
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?
Oh, and normally people code classes like this..
cpp Syntax (Toggle Plain Text)
class monkey { public: protected: private: };
In that order, functions first, variables last. But again no biggy.
You seem to have an aweful lot of constructors:
cpp Syntax (Toggle Plain Text)
Gun::Gun() { shotsFired = 0; hit = 0; miss = 0; hitPercent = 0.0; name = " "; ammo = 0; } Gun::Gun(int rounds) { if (rounds >= 0) { ammo = rounds; } } void Gun::Gun(int, double) { cout << "Shots: " << shotsFired << endl; cout << "Hit %: " << hitPercent << endl; }
And some double functions...
cpp Syntax (Toggle Plain Text)
void Gun::shoot(int ammo, double hitPercent, int hit, int miss, int shotsFired) { ammo--; shotsFired = hit + miss; hitPercent = hit / shotsFired; miss = shotsFired - hit; } 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?
•
•
Join Date: Mar 2009
Posts: 7
Reputation:
Solved Threads: 0
•
•
•
•
Decide that for yourself, do you know the difference between a virtual function and a non-virtual function? It's quite an advanced subject.
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...
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.
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().
Last edited by Clockowl; Apr 12th, 2009 at 9:10 pm.
•
•
Join Date: Mar 2009
Posts: 7
Reputation:
Solved Threads: 0
•
•
•
•
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.
C++ Syntax (Toggle Plain Text)
// Week6iLab_RyanRodgers.cpp : main project file. #include "stdafx.h" #include <iostream> #include <string> using namespace std; class Gun //base class { public: Gun(); Gun(int); // overloaded constructor virtual ~Gun(); //virtual destructor void shoot(int, double, int, int, int); virtual void print() = 0; //pure virtual function virtual void reload(); //virtual function protected: int shotsFired; int hit; int miss; double hitPercent; string name; int ammo; }; // constructor w/ no parameters/define functions for class Gun::Gun() { shotsFired = 0; hit = 0; miss = 0; hitPercent = 0.0; name = " "; ammo = 0; } Gun::Gun(int rounds) { if (rounds >= 0) { ammo = rounds; } } void Gun::shoot(int ammo, double hitPercent, int hit, int miss, int shotsFired) { ammo--; shotsFired = hit + miss; hitPercent = hit / shotsFired; miss = shotsFired - hit; cout << "Shots: " << shotsFired << endl; cout << "Hit %: " << hitPercent << endl; } Gun::~Gun() { } //destructor class MachineGun : public Gun //example of inheritance & derived class { public: MachineGun(); void print(); }; // MachineGun class constructor, passes 30 rounds to parents // overloaded constructor MachineGun::MachineGun() :Gun(30) { name = "Machine Gun"; } void MachineGun::print() { cout << "Your machine gun has " << ammo << " shots left." << endl; } class Pistol : public Gun { public: Pistol(); void print(); }; Pistol::Pistol() :Gun(7) { name = "Pistol"; } void Pistol::print() { cout << "Your pistol has " << ammo << " shots left." << endl; } int main() { MachineGun *mGun = new MachineGun(); // dynamic allocation of pointer mGun->print(); // prints number of bullets left Pistol *mPistol = new Pistol(); mPistol->print(); // free these pointers' memory delete(mGun); delete(mPistol); return ammo; }
•
•
Join Date: Mar 2009
Posts: 7
Reputation:
Solved Threads: 0
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.
C++ Syntax (Toggle Plain Text)
// Week6iLab_RyanRodgers.cpp : main project file #include "stdafx.h" #include <iostream> #include <string> #include <iomanip> using namespace std; class Gun //base class { public: Gun(); Gun(int); // overloaded constructor virtual ~Gun(); //virtual destructor void shoot(int, double, int, int, int); virtual void print() = 0; //pure virtual function virtual void reload(); //virtual function protected: int shotsFired; int hit; int miss; double hitPercent; string name; int ammo; }; // constructor w/ no parameters/define functions for class Gun::Gun() { shotsFired = 0; hit = 0; miss = 0; hitPercent = 0.0; name = " "; ammo = 0; } Gun::Gun(int rounds) { if (rounds >= 0) { ammo = rounds; } } void Gun::shoot(int ammo, double hitPercent, int hit, int miss, int shotsFired) { ammo--; shotsFired = hit + miss; hitPercent = hit / shotsFired; miss = shotsFired - hit; void print(); cout << "Shots: " << shotsFired << endl; cout << "Hit %: " << hitPercent << endl; if(ammo==0){ MachineGun::reload(); Pistol::reload(); } } Gun::~Gun() { } //destructor class MachineGun : public Gun //example of inheritance & derived class { public: MachineGun(); if(ammo==0){ void reload(30); } void print(); }; // MachineGun class constructor, passes 30 rounds to parents // overloaded constructor MachineGun::MachineGun() :Gun(30) { name = "Machine Gun"; } void MachineGun::print() { cout << "Your machine gun has " << ammo << " shots left." << endl; } class Pistol : public Gun { public: Pistol(); if(ammo==0){ void reload(7); } void print(); }; Pistol::Pistol() :Gun(7) { name = "Pistol"; } void Pistol::print() { cout << "Your pistol has " << ammo << " shots left." << endl; } int main() { MachineGun *mGun = new MachineGun(); // dynamic allocation of pointer mGun->print(); // prints number of bullets left Pistol *mPistol = new Pistol(); mPistol->print(); Gun shoot(5); // free these pointers' memory delete(mGun); delete(mPistol); return 0; }
![]() |
Other Threads in the C++ Forum
- Previous Thread: Passing FFT data to an Editbox in an MFC App
- Next Thread: Cannot modify values through vector itertor
Views: 592 | Replies: 10
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll dynamic encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort stream string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





