zandiago
Nearly a Posting Maven
2,480 posts since Jun 2007
Reputation Points: 129
Solved Threads: 26
> I can't get my display() method to work properly in the derived class. The function calls within it keep reading the dimensions from the original Rectangle class. Isn't there a way I can "virtualize" something and get the inner functions to read the right data?
class Rectangle //class declaration
{
private:
double base;
double height;
public:
Rectangle(double base = 5.00, double height = 4.00); //constructor
double area();
void display();
};
// ....
double Rectangle::area()
{
double a;
a = (base * height);
<strong>// really means a = Rectangle::base*Rectangle::height ;</strong>
return a;
}
// ....
class Square : public Rectangle //class declaration
{
private:
double base;<strong> // hides Rectangle::base</strong>
double height;<strong> // hides Rectangle::height</strong>
public:
Square(double base = 3.00); //constructor
void display();
};
// ...
Square::Square(double b)
{
base = b; <strong>// means Square::base = b </strong>;
height = b; <strong>// means Square::height = b ;</strong>
<strong> // Rectangle::base, Rectangle::height are initialized
// by the default constructor of Rectangle to 5.00, 4.00</strong>
}
// ....
void Square::display()
{
cout << "\nBase: " << base;
cout << "\narea: " << Square::area(); //tried a bunch of things here
<strong>// Square::area() is the inherited Recatngle::area()
// which returns Rectangle::base*Rectangle::height ;
// ie. 5.00 * 4.00</strong>
}
you should now be able to figure out what the problem is. and what you should do to address it.
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
consider not hiding Rectangle::base, Rectangle::height in Square.
hint: like public and private, you also have a protected access specifier.
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
class Rectangle //class declaration
{
protected: // look up <strong>protected</strong> in your text book
double base;
double height;
public:
Rectangle(double base = 5.00, double height = 4.00); //constructor
double area();
void display();
};
> How do Inot hide Rectangle::base, Rectangle::height in Square?
by not declaring members with the same name in Square
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
using protected access specifier
#include <iostream>
class Rectangle
{
protected:
double base; // derived class members can access
double height; // inherited base, height
public:
Rectangle( double base = 5.00, double height = 4.00 );
// added const specifiers for selectors
double Area() const ;
void Display() const ;
};
Rectangle::Rectangle( double b, double h ) { base=b ; height=h ; }
double Rectangle::Area() const { return base * height ; }
void Rectangle::Display() const
{
std::cout << "\nBase: " << base ;
std::cout << "\nHeight: " << height;
std::cout << "\nArea: " << Area() << '\n' ;
}
class Square : public Rectangle // use inherited base, height
{
public:
Square( double base = 3.00 );
void Display() const ;
};
Square::Square( double b ) { base=b ; height=b ; }
void Square::Display() const
{
std::cout << "\nBase: " << base ;
std::cout << "\nArea: " << Area() << '\n' ;
}
int main()
{
Rectangle rect1( 7.00000, 9.00000 );
Rectangle rect2;
Square squA;
Square squB( 5.50 );
std::cout << std::fixed << std::showpoint ;
std::cout << "\nrect1\n-----" ; rect1.Display();
std::cout << "\nrect2\n-----" ; rect2.Display();
std::cout << "\nsquA\n-----" ; squA.Display();
std::cout << "\nsquB\n-----" ; squB.Display();
}
> Is there a way to use the same display() for both classes?
there is.
#include <iostream>
#include <cmath>
class Rectangle
{
private:
double base;
double height;
public:
Rectangle( double base = 5.00, double height = 4.00 );
double Area() const ;
void Display() const ;
};
// using initializer list to initialize base, height
Rectangle::Rectangle( double b, double h ) : base(b), height(h) {}
double Rectangle::Area() const { return base * height ; }
void Rectangle::Display() const
{
std::cout << "\nBase: " << base ;
if( std::fabs(base-height) > 0.000001 ) // if height != base
std::cout << "\nHeight: " << height;
std::cout << "\nArea: " << Area() << '\n' ;
}
class Square : public Rectangle
{
public:
Square( double base = 3.00 );
};
// initialize base class (Rectangle) with base=b, height=b
Square::Square( double b ) : Rectangle(b,b) {}
int main()
{
Rectangle rect1( 7.00000, 9.00000 );
Rectangle rect2;
Square squA;
Square squB( 5.50 );
std::cout << std::fixed << std::showpoint ;
std::cout << "\nrect1\n-----" ; rect1.Display();
std::cout << "\nrect2\n-----" ; rect2.Display();
std::cout << "\nsquA\n-----" ; squA.Display();
std::cout << "\nsquB\n-----" ; squB.Display();
}
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287