Assigning values from another class


Hello all, first time forum user so this is all abit intimidating and been learning C++ for a few weeks now.

Been trying to create a small console program that takes 2 values which are in separate class's and multiply them to get a total value. It then calculates these two numbers in a class and displays the total in the main.
I've been reading the tutorials and got a Sams book but can't seem to quite figure out how wrong im doing it.

I'm struggling with:
-Getting the values from the value class's into multiply class
-Constructing a way to calculate the total


//HEADER//
class FirstValue
{
protected:
int boxes;

public:
void setAmountOfBoxes(const int& aBoxes);
int getAmountOfBoxes();


};

//CPP//
void FirstValue::setAmountOfBoxes(const int& aBoxes)
{
boxes = aBoxes;
}

int FirstValue::getAmountOfBoxes()
{
return boxes;
}

First Value.


//HEADER//
class SecondValue
{
protected:
int eggs;
public:
void setAmountOfEggs(const int& aEggs);
int getAmountOfEggs();
};

//CPP//
void SecondValue::setAmountOfEggs(const int& aEggs)
{
eggs = aEggs;
}

int SecondValue::getAmountOfEggs()
{
return eggs;
}

Second Value


//HEADER//
class Total
{
protected:
int* tempBoxes;
int* tempEggs;
int totalEggs;

public:
int* setTempAmountBoxes(int* (aTempAmountBoxes));
int* setTempAmountEggs(int* (aTempAmountEggs));

int calculateTotalEggs(int tempBoxes, int tempEggs);

//This class will be used to calculate your total amount of eggs by using pointers
//to assign values to the tempBoxes & tempEggs, then use a method to take these 2
//values in its parameter to calculate how many eggs you have.

};

//CPP//
int* Total::setTempAmountBoxes(int* aTempAmountBoxes)
{
return tempBoxes;
}

int Total::calculateTotalEggs(int tempBoxes, int tempEggs)
{
totalEggs = tempBoxes * tempEggs;
return totalEggs;
}

The class which deals with multiplying


#include "stdafx.h"
#include "FirstValue.h"
#include "SecondValue.h"
#include "Total.h"



int _tmain(int argc, _TCHAR* argv[])
{
//Goal = To find how many egss there are in total by multiplying Boxes by Eggs

//How many boxes
FirstValue v1;
v1.setAmountOfBoxes(5);
cout << "There are "<<v1.getAmountOfBoxes()<< " boxes of eggs\n";

//How many eggs
SecondValue v2;
v2.setAmountOfEggs(6);
cout << "There is "<<v2.getAmountOfEggs()<<" eggs in each box\n";

//Total:
Total t1;
cout << "You have this many eggs: " << t1.getTotalEggs();

return 0;
}

Main

I hope i have provided enough information, if i'm meant to post something else as well then just say!

Any help on where i'm going wrong or useful reading thats friendly to a beginner would be truly appericated!

Many thanks for taking the time out to read.
Last edited on
I don't really know why you have another class to basically "join" the two other classes together...can't you just do:

int total = v1.getAmountOfBoxes() * v2.getAmountOfEggs();

EDIT: I would've just done it this way though:

1
2
3
4
5
6
7
8
9
10
11
12
13
class Data {
   int eggs, boxes;
public:
   void set_eggs(int new_value) {
      this->eggs = new_value;
   }
   void set_boxes(int new_value) {
      this->boxes = new_value;
   }
   int calc_total() {
      return this->boxes * this->eggs;
   }
}


And save the trouble of having 2 classes which are basically just wrappers for an int.
Last edited on
Topic archived. No new replies allowed.