// 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; }
void shoot(int ammo, double hitPercent, int hit, int miss, int shotsFired)
void gun::shoot(int ammo, double hitPercent, int hit, int miss, int shotsFired)
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.![]()
// 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); }
class monkey { public: protected: private: };
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; }
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)
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.
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().
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.
// 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; }
// 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; }
| DaniWeb Message | |
| Cancel Changes | |