I've looked all over and haven't found an answer on this that I can understand. I included my code (most of it, anyway.) Obviously, I'm trying to make class Square a subclass of class Rectangle. However, 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? What am I doing wrong? (Sorry to put so much code up. I trimmed it down but I wanted to make sure y'all had all the details you'd need.)

#include <iostream>
using namespace std;

class Rectangle		//class declaration
{
private:
    double base;
    double height;
public:
    Rectangle(double base = 5.00, double height = 4.00);    //constructor
    double area();
    void display();
};
//Rectangle methods
Rectangle::Rectangle(double b, double h)	
{
    base = b;
    height = h;
}
double Rectangle::area()
{
    double a;
    a = (base * height);
    return a;
}
void Rectangle::display()
{
    cout << "\nBase:      " << base;
    cout << "\nheight:    " << height;
    cout << "\narea:      " << area();
}

class Square : public Rectangle	//class declaration
{
private:
    double base;
    double height;
public:
    Square(double base = 3.00);	//constructor
    void display();
};
//Square methods
Square::Square(double b)
{
    base = b; 
    height = b;
}
void Square::display()
{
    cout << "\nBase:      " << base;
    cout << "\narea:      " << Square::area();  //tried a bunch of things here
}

//=================main function============================
int main()
{
    Rectangle rect1( 7.00000, 9.00000 );
    Rectangle rect2;		   //use default constructor
    Square squA;		            //use default constructor
    Square squB( 5.50 );

    //rect1 and 2 display fine
    cout << "Rectangle 1 Characteristics";             
    rect1.display();
    cout << "\n\nRectangle 2 Characteristics";
    rect2.display();

    //but these two return the wrong data. The base dimensions display
    //properly but the areas are the same as a default rectangle
    cout << "\n\nSquare A Characteristics";          
    squA.display();        
    cout << "\n\nSquare B Characteristics";
    squB.display();

    cin.get();
    return 0;
}

Hi,you wrote

Rectangle(double base = 5.00, double height = 4.00);

I woud write

Rectangle(double = 5.00, double= 4.00);

Anyway you have a two ways to define constructor.
Replay if it was helpfull.

Why would you change it?

Because that did slove his problem at me.
Dev-C++ at me.
And i have a few comment errors wich i repaired them.

> 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);
    [B]// really means a = Rectangle::base*Rectangle::height ;[/B]
    return a;
}
// ....
class Square : public Rectangle	//class declaration
{
private:
    double base;[B] // hides Rectangle::base[/B]
    double height;[B] // hides Rectangle::height[/B]
public:
    Square(double base = 3.00);	//constructor
    void display();
};
// ...
Square::Square(double b)
{
    base = b;  [B]// means Square::base = b [/B];
    height = b; [B]// means Square::height = b ;[/B]
   [B] // Rectangle::base, Rectangle::height are  initialized 
   // by the default constructor of Rectangle to 5.00, 4.00[/B]
}
// ....
void Square::display()
{
    cout << "\nBase:      " << base;
    cout << "\narea:      " << Square::area();  //tried a bunch of things here
    [B]// Square::area() is the inherited Recatngle::area()
   // which returns Rectangle::base*Rectangle::height ;
   // ie. 5.00 * 4.00[/B]
}

you should now be able to figure out what the problem is. and what you should do to address it.

you should now be able to figure out what the problem is. and what you should do to address it.

I can make more sense of the problem, yes. But I can't figure out how to resolve it. The only thing I can come up with is to create Square's own copy of display(), but I really don't want to do that. Isn't that the point of deriving? I've tried about ten different changes since I read everyone's comments and none of them changed any of the output. Most just created illegal errors. What do I try next. A big thanks, though to everyone who's posted. Keep 'em coming.

vijayan121, you're stringing me on. That's cool, though. I like to learn. I don't want anyone to just fix it all and hand it back. That's why I only posted part of my code. If I can figure out what's going wrong here I can fix the rest myself. I just haven't figured it out yet. Could you maybe uncover the next set of clues?

consider not hiding Rectangle::base, Rectangle::height in Square.
hint: like public and private, you also have a protected access specifier.

consider not hiding Rectangle::base, Rectangle::height in Square.
hint: like public and private, you also have a protected access specifier.

I thought that having Rectangle::base, Rectangle::height in Private meant they wouldn't get accessed outside of class Rectangle. Can I make them any more "private" than... private?

I tried "protecting" everything but my sanity here (no change - to the problem anyways. My sanity is experiencing an odd decline, however.) What should I protect.

In trying some changes I thought your comments suggested, I encountered a number of error messages that directly backed up your comments. Unfortunately, they didn't help me fix it, they just verified that you know what your talking about.:confused: How do I not hide Rectangle::base, Rectangle::height in Square?

Another clue??? Anyone? (Maybe a couple clues?)

I wrote you code on Visual C++ 6.0 and it works. Use this Compiler ))

I wrote you code on Visual C++ 6.0 and it works. Use this Compiler ))

Oh, it runs. But is the output correct?
I'm using Visual c++ 2005. I don't really want to download C++ to fix one problem. Isn't here a way to fix it in 2005? I tried it also in DevC++ I happen to have installed. Runs there, too, but same problem.

class Rectangle  //class declaration
{
  protected: // look up [B]protected[/B] 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 I not hide Rectangle::base, Rectangle::height in Square?
by not declaring members with the same name in Square

All right, I think I got it.

I changed class Square's variables so thier unique. I also changed Square::area() and display(). Area() accepts two variables (unique to the current class) when called (instead of using Rectangle::base and height for its internal calculations) and display() sends area() two variables when it calls it.

How's my code look now? Is there a way to use the same display() for both classes? Or would I have to do the same thing to it?

(I tried protecting the elements suggested but there was no changes to the program's output. I don't know enough about it to mess with it.)

Huge thanks, BTW, to anyone who contributed to this thread! The efforts you took to enlighten me have not gone unappreciated.

class Rectangle
{
private:
	double base;
	double height;
public:
	Rectangle(double base = 5.00, double height = 4.00);
	double area(double,double);
	virtual void display();
};
//... skipped a few lines here
double Rectangle::area(double x, double y)
{
	double a;
	a = (x * y);
	return a;
}
void Rectangle::display()
{
	cout << "\nBase:      " << base;
	cout << "\nheight:    " << height;
	cout << "\narea:      " << area( base, height);
}

class Square : public Rectangle
{
private:
	double bottom;
	double side;

public:
	Square(double = 3.00);
	void display();
};
//... again, skipped a few
void Square::display()
{
	cout << "\nBase:      " << bottom;
	cout << "\narea:      " << area( bottom, side);
}

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();
}

how do you derive a class triangle from class rectangle in this code.


Next How do you derive rectangle from a square and then derive a triangle from square.

The problem with your implementation of Square::display() is that as follows:

First of all the Square object has 4 data members (Rect::base, Rect::height, Square::base, Square::height)

Now when you call Square Squa1 in main() this is what happens
--> call to default constructor of Rect
--> call to default constructor of Square
so
Rect::base = 5.00
Rect::height = 4.00
Square::base = 3.0
Square::height = 3.0

Next thing to note is that area() is a function of the Rectangle class and hence it only has access to Rect::base, Rect::height
In other words
your area() function means the following

double Rectangle::area()
{
    double a;
    a = (Rectangle::base * Rectangle::height);
    return a;
}

Now when you call display() for Squa1 it calls the area function of the Rectangle class which uses the Rect::base and Rect::height.


Hope you got the problem. The solution is to rewrite a area function in the Square class separately OR

redefine your area() as follows

//In class Rectangle
declare 

friend double area(Rectangle&);

//Outside the class define it as follows:
double area(Rectangle& R1)
{
  return(R1.base * R1.height);
}

int main()
{Rectanlge Rect1;
 Square Squa1;
 area(Rect1);
 area(Squa1);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.