 Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works # Classes

### #1  goldfish04 Posted 13 December 2005 - 12:03 PM

goldfish04

Member

• • Member
•  • 11 posts
Well I got this far. I just need a little guidance to see where I am going. My objective is commented at the end.

#include <iostream>
using namespace std;

class boat {
public:
boat();//Constructor
boat2();//Constructor
boat(double,double,double,int);
boat2(double,double,double,int);
void rowing(double);
void display();
private:
double length,
width,
depth;
int oars;
bool rowing;
};

void main()
{
boat b;
b.rowing(2);
b2.rowing(5);
}

boat::boat()
{
length = 10;
width = depth = 5;
oars = 2;
}
boat2::boat()
{
length = 25;
width = 6;
depth = 3;
oars = 8;
}
boat::boat(double l,double w,double d,int o)
{
length = l;
width = w;
depth = d;
oars = o;
}
void boat::rowing(double s)
{
rowing = true;
cout << "The boat's rowing speed is " << s << " knots." << endl;
}

void boat::display()
{
cout << "This boat is " << length << " long and "
<< width << " wide and " << depth << " deep with "
<< oars << " oars." << endl;
}
void boat2::rowing(double s)
{
rowing = true;
cout << "The boat's rowing speed is " << s << " knots." << endl;
}

void boat2::display()
{
cout << "This boat is " << length << " long and "
<< width << " wide and " << depth << " deep with "
<< oars << " oars." << endl;
}
/*

1.declare a second boat with
length 25
width 6
depth 3
8 oars
2.write the definition for the class method
rowing.
3.have the boat go at 5 knots
4.add a class method that will print out
the dimensions of a boat
5.have the dimensions of each boat displayed

place your new sample output here:

*/
• 0

### #2  gust0208 Posted 13 December 2005 - 12:49 PM

gust0208

Member

• • Member
•   • 311 posts
Hello,

I am mainly confused by your main statement and some of your "boat2" declarations. First, lets start with the main statment. It seems as if you have all the needed functions written to do the objectives listed. You just need to declare a boat2 object and have it start rowing, so the main that I would write would be:
void main()
{
boat b;
boat b2;
boat b = new boat();
boat b2 = new boat2();
b2.rowing(5);
b.rowing(2);
b.display();
b2.display();
}

I have been working mainly in Java lately, so please excuse any typos. First, you only have one class, called "boat", so the first function declarations you have are right, e.g. "boat::boat() { ... }", but then you define some functions for a class "boat2" which does not exist as far as I can tell, the first declaration like this I see is:
boat2::boat()
{
length = 25;
width = 6;
depth = 3;
oars = 8;
}
You only have one boat class, but want to have a couple of different constructors for different starting boats, e.g. boat() and boat2(). I think what you are trying to do is declare the boat2() constructor, but referred to it as a different class, class boat 2. I would re-write this function to define the boat2() constructor in the boat class, example below:
boat::boat2()
{
length = 25;
width = 6;
depth = 3;
oars = 8;
}

Now that we are referring to boat2 as just a different constructor under the boat class, you don't need two Display functions and two Rowing functions, you just need the "boat::rowing" and "boat::display" functions and can delete the "boat2::..." functions. So, the final program would look like: (also added "row = false" to the constructors)

#include <iostream>
using namespace std;
class boat {
public:
boat();//Constructor
boat2();//Constructor
boat(double,double,double,int);
boat2(double,double,double,int);
void rowing(double);
void display();
private:
double length,
width,
depth;
int oars;
bool rowing;
};
void main()
{
boat b;
boat b2;
boat b = new boat();
boat b2 = new boat2();
b2.rowing(5);
b.rowing(2);
b.display();
b2.display();
}
boat::boat()
{
length = 10;
width = depth = 5;
oars = 2;
rowing = false;
}
boat::boat2()
{
length = 25;
width = 6;
depth = 3;
oars = 8;
rowing = false;
}
boat::boat(double l,double w,double d,int o)
{
length = l;
width = w;
depth = d;
oars = o;
}
void boat::rowing(double s)
{
rowing = true;
cout << "The boat's rowing speed is " << s << " knots." << endl;
}

void boat::display()
{
cout << "This boat is " << length << " long and "
<< width << " wide and " << depth << " deep with "
<< oars << " oars." << endl;
}

Cheers,
Tom

1.declare a second boat with
length 25
width 6
depth 3
8 oars
2.write the definition for the class method
rowing.
3.have the boat go at 5 knots
4.add a class method that will print out
the dimensions of a boat
5.have the dimensions of each boat displayed
place your new sample output here:

• 0

### Similar Topics

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

### As Featured On:       