I'm writing my first program with classes, and I'm having a hard time. I'm a bit shaky with functions, and this is probably where a lot of my problems root from.
I'm instructed to create a program that prompts the user to enter the amount of brown and white eggs in a carton. I'm suppose to return false if any of the users eggs is equal to zero, but be able to display the particular color of egg that's false to output 0. I'm also suppose to get the total of all eggs.
Below is my header file, class definition file, and my main file.
#include <iostream>
using namespace std;
class EggCarton //Class definition.
{
public:
EggCarton(int brownEggs, int whiteEggs); //Default Constructor.
bool addBrownEggs(int n); //Mutator function
bool addWhiteEggs(int n); //Mutator function
int getTotalEggs(); //Accessor function
private:
int brownEggs; // Variable
int whiteEggs; // Variable
};
--------------------------------------------------------------------------------
#include <iostream>
#include "EggCarton.h"
using namespace std;
EggCarton::EggCarton(int brownEggs = 0, int whiteEggs = 0)
{
cout << "Please enter the amount of brown eggs in the carton: ";
cin >> brownEggs;
cout << "Please enter the amount of white eggs in the carton: ";
cin >> whiteEggs;
}
bool addBrownEggs(int n)
{
addBrownEggs = brownEggs;
if(browneggs == 0)
{
return false;
}
}
bool addWhiteEggs(int n)
{
addWhiteEggs = whiteEggs;
if(whiteEggs == 0)
{
return false;
}
}
int getTotalEggs(brownEggs, whiteEggs)
--------------------------------------------------------------------------------
#include <iostream>
#include "EggCarton.h"
using namespace std;
int main ()
{
int call;
int Total;
call = getTotalEggs(int n, int m);
Total = n + m;
cout << "The amount of brown eggs in the carton is " << brown_eggs << " and the amount of white eggs in the carton is "
<< white_eggs << ". The total amount of eggs in the carton is " << Total << "\n";
At what point in your main function do you create an EggCarton object? I see you creating an int, like this: int call;
but I don't see you ever creating an EggCarton. You need to create an EggCarton object before you can use it.
By defining a class you just defining a plan or blueprint for new data type. So by defining EggCarton you telling the compiler there is another data type and how it works.
If you define EggCarton class correctly (which is not) you have default data type (int, bool, double, etc) + new data type (EggCarton). To use your new data type, you should make one or more instance from it. By coding "int i = 0" you making a new variable named i from int type.
You should do same for your class. (like EggCarton ec(5,6);) now ec is new variable or instance from EggCarton type.
Also all class defination is wrong.
instead of:
1 2
bool addBrownEggs(int n); //Mutator function
bool addWhiteEggs(int n); //Mutator function
do:
1 2
void addBrownEggs(int n); //Mutator function
void addWhiteEggs(int n); //Mutator function
so above functions gets number of new brown or while eggs as arguments
what is this?!!
1 2 3 4 5 6 7 8 9
EggCarton::EggCarton(int brownEggs = 0, int whiteEggs = 0)
{
cout << "Please enter the amount of brown eggs in the carton: ";
cin >> brownEggs;
cout << "Please enter the amount of white eggs in the carton: ";
cin >> whiteEggs;
}
the arguments are where class recives new eggs. so
1 2 3 4 5 6
EggCarton::EggCarton(int inBrownEggs, int inWhiteEggs)
{
brownEggs=inBrownEggs;
whiteEggs = inWhileEggs;
}
seems you do not know what to do (start learning and do practices of your choice book):
void EggCarton::addBrownEggs(int n) //it just add Brown Eggs and nothing else.
{
brownEggs = n; //just do adding eggs nothing else. do same for while eggs in its adding function
}
This is worst function ever: int getTotalEggs(brownEggs, whiteEggs)
it has no semicolon nor defination and not right;
so do:
1 2 3 4
int EggCarton::getTotalEggs()//no argument. it should return the total eggs
{
return brownEggs + whiteEggs;
}
now in main function:
1 2 3
EggCarton ec(2,3);
ec.addBrownEggs(4); //4 more brown eggs
int total = ec.getTotalEggs();