0
xyzabc's Avatar, Join Date: Apr 2009
Go4Expert Member
Quote:
Originally Posted by xpi0t0s View Post
> When I change the code to what you suggested above it gives me compiler errors...

Yes, you will need to change the Box usage of Rectangle to reflect the constructor. The error occurs because Rectangle::Rectangle() doesn't exist (you've changed it to Rectangle::Rectangle(double,double)).

> I am feeling the way he is wanting us to do this problem is making things more difficult then they should be.

Depends. I don't know the context of the course, i.e. what you've done in the past. Maybe Rectangle was only ever meant to operate for 1x1 squares and we're now looking at class derivation. The Box class will work for boxes of any size so I don't understand why Rectangle is so limited. Possibly the point is that even though Rectangle only works for 1x1 squares due to there only being a parameterless constructor that initialises length and width to 1,1, this doesn't limit Box to 1x1xN cuboids.

> Would this be the correct formula to calculate the surface area of Box b?

Yes. For extra smartie points you could show you understand how to access overloaded functions from classes lower down the object hierarchy by changing that to
Code:
return((2*Rectangle::area())+(2*width * depth)+(2*length*depth));
and if he makes a comment about lower performance ask him about code profiling and what he thinks about premature optimisation <grin>
Thanks for the "smartie points" code help . I will see what he has to say about that.

I still am confused though on one thing. The 10.0, 20.0,30.0 isn't even going to be used at all in my program since Box b(3,4,5) has been declared in main, correct? Considering I am calling b.volume() and b.area(). So I don't understand why he even wants us to declare the 10,20,30 values. The Box b(3,4,5) will override those values everytime wont they?

Code:
#include<iostream>
using namespace std;
class Rectangle
{
      protected:
             double length;
             double width;
      public:
             Rectangle::Rectangle(double = 1.0,double =  1.0);
             double area();
             
};

Rectangle::Rectangle(double l,double w)
{
             length = l;
             width = w;
}
double Rectangle::area()
{
     return(length * width);
}

class Box : public Rectangle
{
    protected:
             double depth;
                
    public:
             Box::Box(double = 10.0,double = 20.0,double = 30.0);
             double volume();
             double area();
              
};
Box::Box(double l,double w, double d)
{
                length=l;
                width=w;
                depth=d;
                
}
                
 
double Box::volume()
{
       return(length * width * depth);
}
double Box::area()
{
       return((2*Rectangle::area())+(2*width * depth)+(2*length*depth));
}
int main()
{
    Rectangle r;
    Box b(3.0,4.0,5.0);
    
    cout<<"The area of the rectangle is: "<<r.area()<<endl;
    cout<<"\nThe volume of box b is: " <<b.volume()<<endl;
    cout<<"\nThe surface area of box b is: "<<b.area()<<endl;
    cout<<endl;
    
    system("pause");
    return 0;
}
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
They could be default values. That means that you can miss off those parameters when you declare a Box and you will get the default. Example: Box a, b(1), c(1,2), d(1,2,3); will define 4 boxes of dimensions a=10,20,30; b=1,20,30; c=1,2,30; d=1,2,3 and all four will invoke the Box::Box(double l=10, double w=20, double d=30) constructor.

nitpicks:
- include the variable names in the constructor prototype. e.g double l=30.0, rather than double=30.0. It's clearer to read that way.
- you don't need the Box:: prefix on function names when the function is written within the class definition, i.e. public: Box::Box(...) should be shortened to public: Box(...). Again this is for clearer reading; you would only expect to see Box(...) and the addition of Box:: will make you wonder why that's been added.
The Box:: prefix is needed for stuff written outside the class definition, i.e.:
Code:
class foo {
  void func1(); // no prefix needed
  void func2() { /* ... */ } // no prefix needed
};
void foo::func1() { /* ... */ } // prefix is needed here to distinguish between foo::func1() and a global function func1().
void func1() { /* ... */ } // this declares a global function func1() that has nothing to do with the one in class foo
0
xyzabc's Avatar, Join Date: Apr 2009
Go4Expert Member
Quote:
Originally Posted by xpi0t0s View Post
They could be default values. That means that you can miss off those parameters when you declare a Box and you will get the default. Example: Box a, b(1), c(1,2), d(1,2,3); will define 4 boxes of dimensions a=10,20,30; b=1,20,30; c=1,2,30; d=1,2,3 and all four will invoke the Box::Box(double l=10, double w=20, double d=30) constructor.

nitpicks:
- include the variable names in the constructor prototype. e.g double l=30.0, rather than double=30.0. It's clearer to read that way.
- you don't need the Box:: prefix on function names when the function is written within the class definition, i.e. public: Box::Box(...) should be shortened to public: Box(...). Again this is for clearer reading; you would only expect to see Box(...) and the addition of Box:: will make you wonder why that's been added.
The Box:: prefix is needed for stuff written outside the class definition, i.e.:
Code:
class foo {
  void func1(); // no prefix needed
  void func2() { /* ... */ } // no prefix needed
};
void foo::func1() { /* ... */ } // prefix is needed here to distinguish between foo::func1() and a global function func1().
void func1() { /* ... */ } // this declares a global function func1() that has nothing to do with the one in class foo
Ok thanks again for all your help with this problem. Have a great day.