![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
This is one way to do it, although I consider the requirements to be strained. If I were going to be dealing in carton batches and couldn't use containers (such as vectors), I'd define a carton batch object and use a dynamically allocated array, as shown. However, I'd pass back a batch object that contained a pointer to the array as well as the length of the array. I wouldn't specify a fixed batch size (such as 4), but would accept lwh entries until ENTER was input on a blank line. I'd then allocate however many cartons that was, fill in the batch object, and return it. The example shown here prints out all volumes. You can see how you'd just replace that with code to pluck out the max. I forgot to put in "delete pCartons []"; I leave that as an exercise.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
class Carton
{
private:
double length;
double width;
double height;
public:
Carton ();
Carton (double l, double w, double h);
double getVolume () {return length * width * height;}
void setLength (double l) {length = l;}
void setWidth (double w) {width = w;}
void setHeight (double h) {height = h;}
};
Carton::Carton()
{
length = 1.0;
width = 1.0;
height = 1.0;
}
Carton::Carton (double l, double w, double h = 1.0)
{
length = l;
width = w;
height = h;
}
Carton *inputCartons ();
int main ()
{
Carton *pCartons = inputCartons ();
Carton *pTemp = pCartons;
for (int i = 0; i < 4; ++i)
{
cout << "Carton " << i << " volume = ";
cout << pTemp->getVolume ();
cout << endl;
++pTemp;
}
return 0;
}
Carton *inputCartons ()
{
double Dimensions [4][3];
int i;
for (i = 0; i < 4; i++)
{
cout << "Package " << i+1 << endl;
cout << "Enter the dimension for the length (enter 1 for default): ";
cin >> Dimensions [i][0];
cout << "Enter the dimension for the width (enter 1 for default): ";
cin >> Dimensions [i][1];
cout << "Enter the dimension for the height (enter 1 for default): ";
cin >> Dimensions [i][2];
}
Carton *pCartons = new Carton [4];
Carton *pTemp = pCartons;
for (i = 0; i < 4; ++i)
{
pTemp->setLength (Dimensions [i][0]);
pTemp->setWidth (Dimensions [i][1]);
pTemp->setHeight (Dimensions [i][2]);
++pTemp;
}
return pCartons;
}Quote:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
|
#12 | |
|
Hobbyist Programmer
Join Date: Mar 2006
Posts: 120
Rep Power: 3
![]() |
Quote:
I start now the code to search for the max volume |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to invoke static method from a Class object? | smith.norton | Java | 1 | Oct 5th, 2006 4:00 AM |
| Wierd compile Error. Need help please. | Keiyentai | Java | 7 | Aug 19th, 2006 2:35 AM |
| Syntax Error in defining class Object | Mack1982 | C++ | 14 | May 3rd, 2006 6:06 PM |
| C++ Object class? | titaniumdecoy | C++ | 2 | Mar 23rd, 2006 12:36 AM |
| Problem assigning a string to a class object. | omdown | C++ | 3 | Oct 4th, 2005 1:48 AM |