![]() |
derived classes compile error
I cannot seem to understand the errors I am getting when trying to compile a program that I have written. Any help appreciated..the problem states
Create a base class named Rectangle that contains length and width data members (doubles). From this class derive a class named Box having an additional date member named depth (double) and will have access to derived variables length and width. The base Rectangle class should have a constructor function prototype (initialize: double=1.0 and double=1.0) and an area() function that returns the area of the rectangle. The derived Box class should have a constructor function prototype (initialize: double=10.0, double=20.0,double=30.0), a volume function that returns the volume of the box, and an override function named area() that returns the surface area of the box. In function main(), declare an object of Rectangle class r and declare an object of Box class called b(3,4,5). Then display r.area(), b.area(), and b.volume(). Code:
|
Re: derived classes compile error
sorry the errors are::
39 prototype for `Box::Box(double, double, double)' does not match any in class `Box' 27 candidates are: Box::Box(const Box&) 39 declaration of `Box::Box(double, double, double)' outside of class is not definition |
Re: derived classes compile error
Change the line
Box::Box(double 10.0,double 20.0,double 30.0); to Box(double l,double w,double d); and remove the semicolons off the Box::Box, Box::volume and Box::area function bodies. (You didn't put one after main, which is correct, so it's not clear why you think you should put one after other functions.) In a function prototype you specify variable names, not values. Function bodies should not include a semicolon between the close bracket and open brace. |
Re: derived classes compile error
Quote:
Code:
Box(double l,double w,double d)The functions give the answers 1 6000 6000 so I am assuming this would work. |
Re: derived classes compile error
The idea of a function is that you pass in values that you want the function to operate on. If length, width and depth are always going to be initialised to 10,20 and 30 then you don't need to pass anything into Box(). But if you want to set length to l etc, then you need to do length=l; instead of length=20.0;, and when you call Box, give it those values, e.g. Box(10,20,30).
I suggest you get the Rectangle class working first, then you can start work on Box. Take it a step at a time. "Rectangle::Rectangle(double = 1.0,double = 1.0);" - hmm, you seem to be taking "initialize: double=1.0 and double=1.0" rather too literally. The Rectangle constructor should take no parameters if you're always going to be using fixed values, or it should take two named parameters (e.g. Rectangle(double l,double w), then you can assign those values to length and width, i.e. length=1.0 if you're using fixed values, or length=l if you're using parameters. In a way it's similar to maths; if a function is not dependent on specific variables then it's pointless passing them in. If f() is dependent on x but not y, then you would define f(x) rather than f(x,y); you could set f(x,y)=x^2, but then everyone would ask "why y?". Correct results don't always mean the code is correct. Does the code give the correct results for different size boxes and rectangles? You haven't specified the Rectangle::Rectangle function body anywhere, just a prototype. I'm not sure why the code works at all; Rectangle::length and Rectangle::width haven't been initialised anywhere. What compiler are you using? |
Re: derived classes compile error
Quote:
So the Rectangle::Rectangle(double=1.0,double=1.0) should be more like Rectangle::Rectangle(double l,double w)? How would I initialize length and width to 1.0 using Rectangle::length Rectangle::width since neither of these are functions? sorry for so many questions as I am a beginner. Let me repost the code so you can see the update and see if what I did was what you meant Code:
#include<iostream> |
Re: derived classes compile error
Here's what I think it should be:
Code:
class Rectangle |
Re: derived classes compile error
Quote:
" In function main(), declare an object of Rectangle class r and declare an object of Box class called b(3,4,5). Then display r.area(), b.area(), and b.volume()." so this means that he is expecting just two objects to be declared. Rectangle r; Box b(3,4,5) When I change the code to what you suggested above it gives me compiler errors similar to the ones I posted in my first/second post. In constructor `Box::Box(double)': no matching function for call to `Rectangle::Rectangle()' candidates are: Rectangle::Rectangle(const Rectangle&) note C:\Users\Chris\Documents\C++\cemHW13.cpp:11 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. |
Re: derived classes compile error
Would this be the correct formula to calculate the surface area of Box b?
Code:
double Box::area() |
Re: derived classes compile error
> 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)); |
| All times are GMT +5.5. The time now is 15:44. |