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?

// 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)

should be

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. ;)

void shoot(int ammo, double hitPercent, int hit, int miss, int shotsFired)

should be

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:

// 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);
}

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?

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..

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:

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...

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?

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.

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...

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:

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.

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.

// 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;
}

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.

// 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;
}

Errr, that's probably because you included stdafx.. *-) You're working in MSVC++ I presume? If so, put your includes inside that include (stdafx.h), if they aren't there already. ;)

This article has been dead for over six months. Start a new discussion instead.