Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 19th, 2006, 8:36 PM   #1
codylee270
Unverified User
 
Join Date: Sep 2005
Posts: 209
Rep Power: 0 codylee270 is an unknown quantity at this point
Inheritance and polymorphism exercise problems, plus other questions

Hi all,

I actually thought I was gunna get this one on my own, but the text I was reading from is a little confusing int the inheritance deptartment and what to do with constructors of subclasses. Anyways, this is a program that is supposed to create and initialize a Rectangle object, and use its properties to create a Box object.

#include <iostream>
#include <iomanip>

using namespace std;     

class Rectangle
{
      protected:
                float length;
                float width;
      public:
                Rectangle(float, float);
                virtual float area(){return length*width;}
};

Rectangle::Rectangle(float l, float w)
{
      length = l;
      width = w;
}

class Box : public Rectangle
{
      protected:
                float depth;
      public:
                Box( float l, float w, float d):Rectangle(l), Rectangle(w), depth(d) {}
                virtual float area();
                float volume(){return length*width*depth;}
}
float Box::area() //overide rect. area, compute surface area
{
      return(4*(length*width)+2*(width*depth));
}
    

int main()
{
    Rectangle r1(2, 4);
    Box b1(2,4,6);
    cout << "The area of the Rectangle is : " << r1.area() << "." << endl;
    cout << "The surface area of the Box is : " << b1.area() << "." << endl;
    cout << "The volume of the Box is : " << b1.volume() << ".\n" << endl;
   
    system("PAUSE");
    return 0;

}

Even though i'm going to post the log of the errors from the compiler, I do believe all of the problems stem from the contructor of the Box object.

Box( float l, float w, float d):Rectangle(l), Rectangle(w), depth(d) {}

I tried to do something similar to the book, but of course the compiler doesn't like it, and it doesn't give an example of making a constructor that ISN'T inline. So, 1) Could someone tell me how to make this work with the inline constructor? 2 ) How can you do this without having to do it inline.? 3) Why doesn't the subclass constructor have to have a semicolon after its declaration?

Errors
In constructor `Box::Box(float, float, float)':
multiple initializations given for base `Rectangle'
no matching function for call to `Rectangle::Rectangle(float&)'
note: candidates are: Rectangle::Rectangle(const Rectangle&)
note: Rectangle::Rectangle(float, float)

At global scope:
new types may not be defined in a return type
two or more data types in declaration of `area'
prototype for `Box Box::area()' does not match any in class `Box'
candidate is: virtual float Box::area()
`Box Box::area()' and `virtual float Box::area()' cannot be overloaded

In member function `Box Box::area()':
conversion from `float' to non-scalar type `Box' requested
codylee270 is offline   Reply With Quote
Old Apr 19th, 2006, 11:29 PM   #2
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 266
Rep Power: 4 Cache is on a distinguished road
Pass the base class' constructor arguments exactly as they appear in the base class. And the derived class should have a semicolon at the end.


Inline constructor:
class Box : public Rectangle
{
protected:
	float depth;
public:
	Box( float l, float w, float d) : Rectangle(l, w), depth(d) {}
	float area();
	float volume(){return length*width*depth;}
};

Non-inline constructor (just take the initializer list with you):
class Box : public Rectangle
{
protected:
	float depth;
public:
	Box( float l, float w, float d);
	float area();
	float volume(){return length*width*depth;}
};

Box::Box( float l, float w, float d) : Rectangle(l, w), depth(d)
{
	// Blah!					
}

I imagine the base class constructor arguments need to be passed in the initializer list because of the order of instantiation -- the bass class constructor being called before the derived class.
Cache is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:59 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC