Error in subclass!!

Help!!!
This code doesn't work and I don't see the error! What should I change to make this work????


#include <iostream>
using namespace std;

class Rectangle
{
public:
Rectangle(double = 1.0, double = 1.0);
void setdata(double len, double wid); //mutator
void showdata(); //accessor
double perimeter();
double area();

protected:
double length, width; //declare length and width as double variable

};

Rectangle::Rectangle(double len, double wid)
{
length = len;
width = wid;
}

void Rectangle::setdata(double len, double wid)
{
length = len;
width = wid;
}

//Implementation
void Rectangle::showdata() //accessor
{
cout << "The length is: " << length << " and the width is: " << width << endl;
}

double Rectangle::perimeter()
{
double c = 2 * length + 2 * width;
return c;
}

double Rectangle::area()
{
double a = length*width;
return a;
}

//subclass
class Box : public Rectangle
{
protected:
double depth,volume;
public:
Box(double= 1.0,double=1.0,double = 1.0);
void setdata(double dep); //mutator
void showdata();
double calcvolume();

//implementation section
Box::Box(double len,double wid,double dep)
{
depth = dep;
}

void Box::setdata(double dep)
{
depth = dep;
}

void Box::showdata() //accessor
{
cout << "The length is: " << length << " ,the width is: " << width << "and the depth is: "<<depth<<endl;
}
double Box::calcvolume()
{
volume = length*width*depth;
return volume;
}
};

int main()
{
//declare 2 rectangles
Rectangle rec1(4.6, 5.3);
Rectangle rec2(3.2, 6.1);

rec1.showdata();
cout << "The perimeter is: " << rec1.perimeter() << endl;
cout << "The area is: " << rec1.area() << endl << endl;

rec2.showdata();
cout << "The perimeter is: " << rec2.perimeter() << endl;
cout << "The area is: " << rec2.area() << endl << endl;

Box bx1(3.5, 5.3, 1.4);

bx1.showdata();
cout << "The volume is: " << bx1.calcvolume() << endl;

system("PAUSE");
return 0;

}

[/code]
listen to you compiler:

you have:
void showdata();
in Box, and then further down (still in Box) you have:

1
2
3
4
void Box::showdata() //accessor
	{
		cout << "The length is: " << length << " ,the width is: " << width << "and the depth is: " << depth << endl;
	}


if you're prepending the class name before the method, then do this outside of your class.

You have this issue with 3 methods and also your Box constructor.

You may want to read about the virtual keyword if you are going down the polymorphism route at some point as well.
Last edited on
The closing brace of Box is at the wrong place:

1
2
3
4
5
6
double Box::calcvolume()
{
volume = length*width*depth;
return volume;
}
}; // <---- Wrong place 


Move it before //implementation section
The closing brace of Box is at the wrong place:
weirdly i didn't get this issue when i copied and pasted OP's code & compiled.
weirdly i didn't get this issue when i copied and pasted OP's code & compiled.
Hm? I suggested to move the closing brace of the class and you suggested to move the functions outside of the class, which is basically the same?
ah right sorry. thought you were suggesting adding another brace.
I'm a tad tired today :/
Hey guys thanks. The program works now. :)
But now I have another problem. for the box, it gives me the default values of 1, 1 and 1.4. But my setter is to set the values to Box bx1(3.5, 5.3, 1.4);
Do you see what is wrong?
you call this:
Box bx1(3.5, 5.3, 1.4);

But your constructor does sod all with your len and wid variables:

1
2
3
4
	Box::Box(double len, double wid, double dep)
	{
		depth = dep;
	}


it's only setting dep.
That means I have to set for all 3? Is there anyway that I can use the setters from the inheritance class or is this the only way?
Topic archived. No new replies allowed.