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

Recommended Answers

All 3 Replies

The Soda class should include the pure virtual function while the Juice class should include the other virtual function.

My stuff is a bit rusty so bear with me if I'm missing something obvious but aside from varying the access specifiers (pub,priv,prot) of the two methods and using a different {public, protected,private} inheritance model for each derived class I believe they are both going to be there regardless.

My stuff is a bit rusty so bear with me if I'm missing something obvious but aside from varying the access specifiers (pub,priv,prot) of the two methods and using a different {public, protected,private} inheritance model for each derived class I believe they are both going to be there regardless.

Thanks, that helps but my code is a mess andreturns garbage data.....any ideas how to fix it?

You never call setPercent for any of the objects so that value is always zero in the output.

Also, you don't have a constructor for juice to pass in the value for the ounces.
In your drink constructor you just take the value and reduce it to zero again so it doesn't show up either.

So make sure you're getting the values to their proper spot and calling everything that needs to be called. I would definitely change that drink constructor and perhaps integrate that into another method called empty or something.

A couple of general things. If your methods (e.g., getName or consume) are virtually (no pun intended) identical to each other why not just put them in the parent class as such. You've defeated the purpose of having a class to inherit from as you did twice as much work.
Also, they'll probably push you into this eventually but you should think about dividing your code up into headers for your declarations, implementation files (.cpp) for your method definitions and perhaps a separate .cpp for your main file (that way you include all the headers in main() and in their (respective) .cpp files and compile the whole shebang at once). It's more convenient to have it in one file now but in terms of maintenance and keeping it organized (and particularly for reuse) you'll benefit from doing it in the long run.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.