I scrapped the other code about the student because it didn't make any sense. I was actually modeling it after another program I did last semester that was a Plasma gun program.
Anyway I started over with the same requirements but new program. I am having similar issues with this one. Here are the requirements:
STEP 1: Create a New Project
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 as covered this week in class
at least one example of dynamic memory allocation as covered this week in class
STEP 2: Construct Main Method
Construct the main method so that it will test each and every member function of the parent and the child classes
Here's my code, it compiles ok but gives garbage data. I want to have an abstract class of Drinks with two derived classes on of Soda and one of Juice. The Soda class should include the pure virtual function while the Juice class should include the other virtual function. I want to be able to hard code the values of ounces, ounces consumed and name into the main function when the other functions are called. Does this all make sense?
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
class Drink //base class
{
public:
Drink();
Drink(int); // Overloaded constructor
virtual ~Drink(); //virtual destructor
string getName(string);
int getOunces(int);
int getOzCon(int);
virtual void print() = 0; //pure virtual function
virtual void consume(); //virtual function (no = 0 part)
protected:
string name;
int ounces;
int ozConsumed;
double percentConsumed;
};
Drink::Drink() {
}
Drink::Drink(int oz) {
while (oz > 0) {
oz--;
ounces = oz;
}
}
void Drink::consume()
{
}
string Drink::getName(string nam)
{
return nam;
}
int Drink::getOunces(int oz)
{
return oz;
}
int Drink::getOzCon(int ozC)
{
return ozC;
}
Drink::~Drink()
{
}
class Soda : public Drink //example of inheritance & derived class
{
public:
Soda();
string getName(string);
int getOunces(int);
int getOzCon(int);
double setPercent(double, double, double);
void consume(string,int, int,double);
void print();
};
Soda::Soda() :Drink(20)
{
}
string Soda::getName(string nam)
{
return nam;
}
int Soda::getOunces(int oz)
{
return oz;
}
int Soda::getOzCon(int ozC)
{
return ozC;
}
void Soda::consume(string nam, int oz, int ozC, double ozPer)
{
name = nam;
ounces = oz;
ozConsumed = oz-ozC;
percentConsumed = ozPer;
}
double Soda::setPercent(double oz, double ozC, double ozPer)
{
ozPer = ozC/oz;
return ozPer;
}
void Soda::print() {
cout << "Your soda has " << ounces << " ounces left." << endl;
cout << "The percentage of your soda consumed is " << percentConsumed<<endl;
}
class Juice : public Drink
{
public:
Juice();
string getName(string);
int getOunces(int);
int getOzCon(int);
double setPercent(double, double, double);
void consume(string,int, int, double);
void print();
};
Juice::Juice()
{
}
string Juice::getName(string nam)
{
return nam;
}
int Juice::getOunces(int oz)
{
return oz;
}
int Juice::getOzCon(int ozC)
{
return ozC;
}
void Juice::consume(string nam, int oz, int ozC, double ozPer)
{
name = nam;
ounces = oz;
ozConsumed = oz - ozC;
percentConsumed = ozPer;
}
double Juice::setPercent(double oz, double ozC, double ozPer)
{
ozPer = ozC/oz;
return ozPer;
}
void Juice::print()
{
cout << "Your juice has " << ounces << " ounces left." << endl;
cout << "The percentage of your juice consumed is " << percentConsumed << endl;
}
int main()
{
Soda soda;
soda.consume("Soda",20,3,0);
Soda *sDrink = new Soda(); // Dynamic allocation of pointer
sDrink->print(); // Prints number of bullets left
Juice juice;
juice.consume("Juice", 10,3,0);
Juice *aJuice = new Juice();
aJuice->print();
// Free these pointers memory
delete(sDrink);
delete(aJuice);
}