Jan 20, 2015 at 3:14pm UTC
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]
Jan 20, 2015 at 3:18pm UTC
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 Jan 20, 2015 at 3:20pm UTC
Jan 20, 2015 at 3:24pm UTC
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.
Jan 20, 2015 at 4:14pm UTC
ah right sorry. thought you were suggesting adding another brace.
I'm a tad tired today :/
Jan 21, 2015 at 1:19pm UTC
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?
Jan 21, 2015 at 1:57pm UTC
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?