Derived classes' attributes are empty - c++

I am new to C++ and I am currently playing with inheritance. I am creating a base Polygon class that is inherited by Rectangle and Triangle classes respectively. From there I want to print out the area as defined in calcArea. However, the output of my derived class instances seem to be null.
From what I understand the Polygon:(name, width, height) can help to initialize variables that already exist in the base class. Thanks for all the help!
Here's my code:
#include <iostream>
#include <string>
using namespace std;
enum Polytype {POLY_PLAIN, POLY_RECT, POLY_TRIANG};
class Polygon
{
public:
Polygon(string name, double width, double height){
_name = name;
_width = width;
_height = height;
_polytype = POLY_PLAIN;
}
virtual ~Polygon()
{
cout << "Destroying polygon" << endl;
}
virtual Polytype getPolytype(){
return _polytype;
}
virtual void setPolytype(Polytype polytype){
_polytype = polytype;
}
virtual string getName(){
return _name;
}
virtual double calcArea(){
return _width * _height;
}
private:
string _name;
double _width;
double _height;
Polytype _polytype;
};
class Rectangle: public Polygon
{
public:
Rectangle(string name, double width, double height) : Polygon(name, width, height){
_polytype = POLY_RECT;
};
~Rectangle()
{
cout << "Destroying rectangle" << endl;
}
Polytype getPolytype(){
return _polytype;
}
void setPolytype(Polytype polytype){
_polytype = polytype;
}
double calcArea(){
return _width * _height;
}
string getName(){
return _name;
}
private:
string _name;
double _width;
double _height;
Polytype _polytype = POLY_RECT;
};
class Triangle: public Polygon
{
public:
Triangle(string name, double width, double height) : Polygon(name, width, height){
_polytype = POLY_TRIANG;
};
~Triangle()
{
cout << "Destroying triangle" << endl;
}
Polytype getPolytype(){
return _polytype;
}
void setPolytype(Polytype polytype){
_polytype = polytype;
}
string getName(){
return _name;
}
double calcArea(){
return 0.5 * _width * _height;
}
private:
string _name;
double _width;
double _height;
Polytype _polytype;
};
int main(){
//Initialize rectangle and triangle and store them onto the stack
Rectangle rect("RectA", 10.0, 20.0);
Triangle triang("TriangB", 10.0, 20.0);
cout << "Name is " << rect.getName() << endl;
cout << "Name is "<< triang.getName() << endl;
string rectArea = to_string(rect.calcArea());
string triangArea = to_string(triang.calcArea());
cout << "RectA's area is " << rectArea << endl;
cout << "TriangB's area is " << triangArea << endl;
return 0;
}
And here's my output:
Name is
Name is
RectA's area is 0.000000
TriangB's area is 0.000000
Destroying triangle
Destroying polygon
Destroying rectangle
Destroying polygon

The main problem is that you have variables in the sub classes shadowing the names in the base class - so you assign values to the variables in the base class, but you later print the values of the default initialized variables in the sub classes.
You actually mostly need to remove code.
I would rethink the name of the base class though. Polygon is not a good name for a class with only width and height. I'll leave that up to you.
I've replaced all endl with \n. They do the same thing, but endl flushes the output, which is usually not needed - but it is usually also expensive.
Example:
#include <iostream>
#include <string>
enum Polytype { POLY_PLAIN, POLY_RECT, POLY_TRIANG };
class Polygon {
public:
Polygon(std::string name, double width, double height)
: Polygon(name, width, height, POLY_PLAIN) {}
virtual ~Polygon() { std::cout << "Destroying polygon\n"; }
// make member functions that does not change the object `const`:
virtual Polytype getPolytype() const { return _polytype; }
virtual void setPolytype(Polytype polytype) { _polytype = polytype; }
virtual const std::string& getName() const { return _name; }
// in your case, the implementation could actually be in the base class - but
// I've made it into a pure virtual here.
virtual double calcArea() const = 0; // no instances can be made of Polygon
protected:
// only derived classes can access this constructor:
Polygon(std::string name, double width, double height, Polytype ptype)
: _name(name), _width(width), _height(height), _polytype(ptype) {}
std::string _name;
double _width;
double _height;
Polytype _polytype;
};
class Rectangle : public Polygon {
public:
Rectangle(std::string name, double width, double height)
//use the protected base class ctor:
: Polygon(name, width, height, POLY_RECT) {};
~Rectangle() { std::cout << "Destroying rectangle\n"; }
// the only implementation needed in this sub class:
double calcArea() const override { return _width * _height; }
};
class Triangle : public Polygon {
public:
Triangle(std::string name, double width, double height)
: Polygon(name, width, height, POLY_TRIANG) {};
~Triangle() { std::cout << "Destroying triangle\n"; }
// the only implementation needed in this sub class:
double calcArea() const override { return 0.5 * _width * _height; }
};
int main() {
// Initialize rectangle and triangle and store them onto the stack
Rectangle rect("RectA", 10.0, 20.0);
Triangle triang("TriangB", 10.0, 20.0);
std::cout << "Name is " << rect.getName() << '\n';
std::cout << "Name is " << triang.getName() << '\n';
std::cout << "RectA's area is " << rect.calcArea() << '\n';
std::cout << "TriangB's area is " << triang.calcArea() << '\n';
}

I realized that I did not have to redeclare the private variables in my subclasses. That is probably the reason why it returned null.
#include <iostream>
#include <string>
using namespace std;
enum Polytype {POLY_PLAIN, POLY_RECT, POLY_TRIANG};
class Polygon
{
public:
Polygon(string name, double width, double height){
_name = name;
_width = width;
_height = height;
_polytype = POLY_PLAIN;
}
~Polygon()
{
cout << "Destroying polygon" << endl;
}
virtual Polytype getPolytype(){
return _polytype;
}
virtual void setPolytype(Polytype polytype){
_polytype = polytype;
}
virtual string getName(){
return _name;
}
virtual double calcArea(){
return _width * _height;
}
protected:
string _name;
double _width;
double _height;
Polytype _polytype;
};
class Rectangle: public Polygon
{
public:
Rectangle(string name, double width, double height) : Polygon(name, width, height){
_polytype = POLY_RECT;
};
~Rectangle()
{
cout << "Destroying rectangle" << endl;
}
Polytype getPolytype(){
return _polytype;
}
void setPolytype(Polytype polytype){
_polytype = polytype;
}
double calcArea(){
return _width * _height;
}
string getName(){
return _name;
}
};
class Triangle: public Polygon
{
public:
Triangle(string name, double width, double height) : Polygon(name, width, height){
_polytype = POLY_TRIANG;
};
~Triangle()
{
cout << "Destroying triangle" << endl;
}
Polytype getPolytype(){
return _polytype;
}
void setPolytype(Polytype polytype){
_polytype = polytype;
}
string getName(){
return _name;
}
double calcArea(){
return 0.5 * _width * _height;
}
};
int main(){
//Initialize rectangle and triangle and store them onto the stack
Rectangle rect("RectA", 10.0, 20.0);
Triangle triang("TriangB", 10.0, 20.0);
cout << "Name is " << rect.getName() << endl;
cout << "Name is "<< triang.getName() << endl;
string rectArea = to_string(rect.calcArea());
string triangArea = to_string(triang.calcArea());
cout << "RectA's area is " << rectArea << endl;
cout << "TriangB's area is " << triangArea << endl;
return 0;
}

Related

Error when using super-class objects in an array made for the sub-class of that super-class [duplicate]

This question already has answers here:
What is object slicing?
(18 answers)
Closed 5 years ago.
Hey guys here is some code I am going to run, issue is it doesn't work the way I intend to. I am unable to figure out what's wrong with it. I am c++ noob please help.
#include <iostream>
#include <cmath>
#include <stdexcept>
using namespace std;
/**
* Super class
*/
class Shape
{
protected:
int _dimensions;
public:
Shape() : _dimensions{0}
{};
Shape(int dims) : _dimensions{dims}
{};
virtual double getArea() {};
// Because some shapes have no volume.
virtual double getVolume() {};
void setDimensions(int dim)
{
this->_dimensions = dim;
};
int getDimensions()
{
return this->_dimensions;
};
};
/**
* Extended classes
*/
class TwoDimensionalShape : public Shape
{
public:
TwoDimensionalShape() : Shape{2}
{};
// This should throw an error
double getVolume() {
throw logic_error("This shape ain't got area!");
};
};
class ThreeDimensionalShape : public Shape
{
public:
ThreeDimensionalShape() : Shape{3} {};
};
/**
* Final Concrete classes extending extended classes
*/
class Circle : public TwoDimensionalShape
{
protected:
double _radius;
public:
Circle(double r) : _radius{r}
{};
double getArea()
{
// pi*r^2
return M_PI * pow(_radius, 2);
}
};
class Square : public TwoDimensionalShape
{
protected:
double _side;
public:
Square(double s) : _side{s}
{}
double getArea()
{
// s^2
return pow(_side, 2);
}
};
class Triangle : public TwoDimensionalShape
{
protected:
double _base, _height;
public:
Triangle(double b, double h) : _base{b}, _height{h}
{};
double getArea()
{
// b*h/2
return _base * _height / 2;
}
};
class Sphere : public ThreeDimensionalShape
{
protected:
double _radius;
public:
Sphere(double r) : _radius{r}
{}
double getArea()
{
cout << 4 * M_PI * pow(_radius, 2) << endl;
return 4 * M_PI * pow(_radius, 2);
}
double getVolume()
{
return (4/3) * M_PI * pow(_radius, 3);
}
};
class Cube : public ThreeDimensionalShape
{
protected:
double _side;
public:
Cube(double s) : _side{s}
{};
double getArea()
{
// surface area = 6*a^2
return 6 * pow(_side, 2);
}
double getVolume()
{
// a^3
return pow(_side, 3);
}
};
class Tetrahedron : public ThreeDimensionalShape
{
protected:
double _side;
public:
Tetrahedron(double s) : _side{s}
{};
double getArea()
{
// sqrt(3)*a^2
return sqrt(3) * pow(_side, 2);
}
double getVolume()
{
// a^3/6sqrt(2)
return pow(_side, 3) / (6 * sqrt(2));
}
};
int main()
{
Shape arr[2];
arr[0] = Circle{10};
arr[1] = Sphere{10};
// This one is accessing the right method.
cout << "Area of circle: " << arr[0].getArea() << endl;
// This one should access the parent, but accesses the grand parent!
// even if it is overridden in parent.
cout << "Volume of circle: " << arr[0].getVolume() << endl;
// Both of these are accessing methods on grand parent rather than their own!!
cout << "Area of sphere: " << arr[1].getArea() << endl;
cout << "Volume of sphere: " << arr[1].getVolume() << endl;
return 0;
}
I don't know why the array methods keep accessing the grand parent functions in the last three lines, but the right method on the first.
You are experiencing Object Slicing. The portion of your code below does that:
Shape arr[2];
arr[0] = Circle{10};
arr[1] = Sphere{10};
Each of the assignments above invokes the copy assignment operator of Shape and slices off objects of the subclass. You can achieve your intention by using references or pointers:
std::unique_ptr<Shape> arr[2];
arr[0] = std::make_unique<Circle>(10);
arr[1] = std::make_unique<Sphere>(10);
This is a case of object slicing. You need to put your objects as pointers, preferably as std::unique_ptr<> into your array - or again preferably a std::vector<>
Try this:
#include <memory>
#include <vector>
// ...
int main()
{
std::vector<std::unique_ptr<Shape>> vec(2);
vec[0] = std::make_unique<Circle>(10);
vec[1] = std::make_unique<Sphere>(10);
// This one is accessing the right method.
cout << "Area of circle: " << vec[0]->getArea() << endl;
// This one should access the parent, but accesses the grand parent!
// even if it is overridden in parent.
cout << "Volume of circle: " << vec[0]->getVolume() << endl;
// Both of these are accessing methods on grand parent rather than their own!!
cout << "Area of sphere: " << vec[1]->getArea() << endl;
cout << "Volume of sphere: " << vec[1]->getVolume() << endl;
return 0;
}

Windows.h causes a problem with C++ class

I'm using Visual Studio 2019, and when I made my first class in C++, an error occurred. It dissappeared when I deleted #include <Windows.h>. My question is, why does Windows.h collide with C++ classes, and is it possible to use both (I'm almost sure it is).
#include <iostream>
#include <locale>
using std::cout;
using std::cin;
class Rectangle {
public:
Rectangle() = default;
Rectangle(double width, double height)
: width_{ width }, height_{ height }
{}
double Width() const { return width_; }
double Height() const { return height_; }
double Area() const {
return width_ * height_;
}
double Perimeter() const {
return 2 * (width_ + height_);
}
void Scale(double scaleFactor) {
width_ *= scaleFactor;
height_ *= scaleFactor;
}
private:
double width_{};
double height_{};
};
void printInfo(const Rectangle & r) {
cout << "Width" <<r.Width() << '\n';
cout << "Height" << r.Height() << '\n';
cout << "Area" << r.Area() << '\n';
cout << "Per" << r.Perimeter() << '\n';
}
int main() {
setlocale(LC_ALL, "pl_PL.UTF8");
Rectangle rect;
}
Windows.h defines Rectangle as a free function, see here.
Solution: change the name of your class or put it in its own namespace.

C++ Inheritance is not working properly

#include <iostream>
using namespace std;
class Item {
private:
int _code;
int _color;
int _brand;
double _height;
double _length;
double _width;
double _weight;
double _price;
int _type;
bool _doesItHaveThis;
public:
Item();
Item(int code, int color, int brand, double height, double length, double width,
double weight, double price, int type, bool doesItHaveThis);
void setCode(int code);
void setColor(int color);
void setBrand(int brand);
void setHeight(double height);
void setLength(double length);
void setWidth(double width);
void setWeight(double weight);
void setPrice(double price);
void setType(int type);
void setDoesItHaveThis(bool doesItHaveThis);
int getCode();
int getColor();
int getBrand();
double getHeight();
double getLength();
double getWidth();
double getWeight();
double getPrice();
int getType();
bool getDoesItHaveThis();
virtual ~Item();
void display();
};
//----------------------------------------------------------
Item::Item()
{
_code = 0;
_color = 0;
_brand = 0;
_height = 0;
_length = 0;
_width = 0;
_weight = 0;
_price = 0;
_type = 0;
_doesItHaveThis = 0;
}
//----------------------------------------------------------
Item::Item(int code, int color, int brand, double height, double length, double width,
double weight, double price, int type, bool doesItHaveThis)
{
_code = code;
_color = color;
_brand = brand;
_height = height;
_length = length;
_width = width;
_weight = weight;
_price = price;
_type = type;
_doesItHaveThis = doesItHaveThis;
}
//----------------------------------------------------------
void Item::setCode(int code)
{
_code = code;
}
//----------------------------------------------------------
void Item::setColor(int color)
{
_color = color;
}
//----------------------------------------------------------
void Item::setBrand(int brand)
{
_brand = brand;
}
//----------------------------------------------------------
void Item::setHeight(double height)
{
_height = height;
}
//----------------------------------------------------------
void Item::setLength(double length)
{
_length = length;
}
//----------------------------------------------------------
void Item::setWidth(double width)
{
_width = width;
}
//----------------------------------------------------------
void Item::setWeight(double weight)
{
_weight = weight;
}
//----------------------------------------------------------
void Item::setPrice(double price)
{
_price = price;
}
//----------------------------------------------------------
void Item::setType(int type)
{
_type = type;
}
//----------------------------------------------------------
void Item::setDoesItHaveThis(bool doesItHaveThis)
{
_doesItHaveThis = doesItHaveThis;
}
//----------------------------------------------------------
int Item::getCode()
{
return _code;
}
//----------------------------------------------------------
int Item::getColor()
{
return _color;
}
//----------------------------------------------------------
int Item::getBrand()
{
return _brand;
}
//----------------------------------------------------------
double Item::getHeight()
{
return _height;
}
//----------------------------------------------------------
double Item::getLength()
{
return _length;
}
//----------------------------------------------------------
double Item::getWidth()
{
return _width;
}
//----------------------------------------------------------
double Item::getWeight()
{
return _weight;
}
//----------------------------------------------------------
double Item::getPrice()
{
return _price;
}
//----------------------------------------------------------
int Item::getType()
{
return _type;
}
//----------------------------------------------------------
bool Item::getDoesItHaveThis()
{
return _doesItHaveThis;
}
//----------------------------------------------------------
Item::~Item()
{
cout << "ITEM ELIMINATED" << endl;
}
//----------------------------------------------------------
void Item::display()
{
cout << "code = " << _code << ", color = " << _color << ", brand = "
<< _brand << ", height = " << _height << ", length = " << _length
<< ", width = " << _width << ", weight = " << _weight << ", price = "
<< _price << ", type = " << _type << ", doesItHaveThis = "
<< _doesItHaveThis << endl;
}
//----------------------------------------------------------
class Pens : public Item {
private:
int _code;
int _color;
int _brand;
double _height;
double _length;
double _width;
double _weight;
double _price;
int _type;
bool _doesItHaveThis;
int _packetSize;
public:
Pens();
Pens(int code, int color, int brand, double height, double length, double width,
double weight, double price, int type, bool doesItHaveThis, int packetSize);
void setPacketSize(int packetSize);
int getPacketSize();
virtual ~Pens();
void display();
};
//----------------------------------------------------------
Pens::Pens()
{
_code = 0;
_color = 0;
_brand = 0;
_height = 0;
_length = 0;
_width = 0;
_weight = 0;
_price = 0;
_type = 0;
_doesItHaveThis = 0;
_packetSize = 0;
}
//----------------------------------------------------------
Pens::Pens(int code, int color, int brand, double height, double length, double width,
double weight, double price, int type, bool doesItHaveThis, int packetSize)
{
_code = code;
_color = color;
_brand = brand;
_height = height;
_length = length;
_width = width;
_weight = weight;
_price = price;
_type = type;
_doesItHaveThis = doesItHaveThis;
_packetSize = packetSize;
}
//----------------------------------------------------------
void Pens::setPacketSize(int packetSize)
{
_packetSize = packetSize;
}
//----------------------------------------------------------
int Pens::getPacketSize()
{
return _packetSize;
}
//----------------------------------------------------------
Pens::~Pens()
{
cout << "PEN ELIMINATED" << endl;
}
//----------------------------------------------------------
void Pens::display()
{
cout << "code = " << _code << ", color = " << _color << ", brand = "
<< _brand << ", height = " << _height << ", length = " << _length
<< ", width = " << _width << ", weight = " << _weight << ", price = "
<< _price << ", type = " << _type << ", doesItHaveThis = "
<< _doesItHaveThis << ", packetSize = " << _packetSize << endl;
}
//----------------------------------------------------------
void main()
{
Pens I1(1, 2, 3, 4.1, 2.0, 3.4, 3.3, 3.2, 5, 1, 0);
I1.setBrand(999);
I1.setDoesItHaveThis(0);
I1.setHeight(34.62);
I1.display();
}
So this is my code, and I'm wondering why the Pens class is not properly inheriting the public methods of the Item class. When I run this code, the setBrand(999), setDoesItHaveThis, and setHeight don't work from what I can tell from the output. Can anybody tell what I did wrong?
The way to use polymorphism is to inherit the members of the base class. If you repeat them instead, then this will declare another member of the same name.
struct base
{
int member1 = 0; // the =0 means that these members
int member2 = 0; // will be default initialised to 0
base() = default;
explicit base(int i, int j=0)
: member1(i), member2(j) {}
};
struct derived
: base
{
int member1 = 1; // not to be confused with base::member1
int member3 = 4;
derived() = default;
explicit derived(int i, int j=0, int k=1, int m=4)
: base(i,j), member1(k), member3(m) {}
int foo() const
{ return member1 * base::member1; }
};
Here, derived has two members member1: one inherited from base and another not inherited. These two can be easily confused by a programmer (but not by the compiler). So such constructs should be avoided (and good compilers will warn you).
In the derived class call the base class constructor with all the arguments you need. And like pointed out in the comments do not redeclare all the base class variables as new members of the derived class. To set/get the base class member variables use the Setters and Getters that you have made public in your base class (these get inherited by the derived class). (Although I would make the setters protected if I were you). Also look in to initializer lists in constructors instead of initializing each member in the body - Read this

Polymorphism vs Inheritance. Diffrence?

I dont understand the diffrence between Polymorphism and Inheritance... They Litterarly do the same thing...
Simple Example Of Polymorphism:
class shape {
public:
void setValues(int height_, int width_) {
height = height_, width = width_;
}
protected:
int height, width;
private:
};
class rectangle :public shape, public ThreeDView{
public:
int area() {
return(shape::height*shape::width);
}
float threeDArea() {
return(((shape::height*shape::width)/2)*(std::cos(Z_LENGTH)));
}
};
class ThreeDView{
public:
void setZLength(int value) {
Z_LENGTH = value;
}
int setCompact(bool ans) {
compact = ans;
}
float getZLength() {
return Z_LENGTH;
}
bool getCOMPACT() {
return compact;
}
protected:
float Z_LENGTH;
bool compact;
private:
unsigned char ZCHAR = 'Z';
};
class triangle :public shape {
public:
int area() {
return((shape::height * shape::width) / 2);
}
};
int main(){
rectangle rect2;
triangle trng2;
shape *poly = &rect2;
shape *poly2 = &trng2;
poly->setValues(2,3);
poly2->setValues(5,4);
std::cout << "AREA : " << trng1.area() << "AREA RECT : \n" <<rect1.area() << std::endl;
}
Above example translated to Inheritance:
class shape {
public:
void setValues(int height_, int width_) {
height = height_, width = width_;
}
protected:
int height, width;
private:
};
class rectangle :public shape, public ThreeDView{
public:
int area() {
return(shape::height*shape::width);
}
float threeDArea() {
return(((shape::height*shape::width)/2)*(std::cos(Z_LENGTH)));
}
};
class triangle :public shape {
public:
int area() {
return((shape::height * shape::width) / 2);
}
};
int main(){
rectangle rect2;
triangle trng2;
rect2.setValues(2,3);
trng2.setValues(5,4);
std::cout << "AREA : " << trng1.area() << "AREA RECT : \n" <<rect1.area() << std::endl;
}
Please tell me diffrence. Honestly i dont even see the use of Polymorphism! Thanks for helping!
Here's a version of your first example, that actually uses polymorphism:
#include <iostream>
#include <string>
class shape
{
public:
void setValues(int height_, int width_)
{
height = height_;
width = width_;
}
virtual int area() = 0; // This is needed for polymorphism to work
virtual std::string name() = 0;
protected:
int height;
int width;
};
class rectangle : public shape
{
public:
int area()
{
return height * width;
}
std::string name()
{
return "Rectangle";
}
};
class triangle :public shape
{
public:
int area()
{
return height * width / 2;
}
std::string name()
{
return "Triangle";
}
};
void print_area(shape& poly)
{
std::cout << poly.name() << ' ' << poly.area() << '\n';
}
int main()
{
rectangle rect;
triangle trng;
rect.setValues(2, 3);
trng.setValues(5, 4);
print_area(rect);
print_area(trng);
}
The first big change is that I declare the virtual function area in the shape class. For polymorphism to work, the functions must be declared in the base class as virtual. The "assignment" to 0 is simply telling the compiler that it's an abstract function, and the child-classes must override that function.
The second big change is that I use a function to print the area, one that only takes a reference to the base shape class. You must use references or pointers to the base class for polymrphism to work, not use the actual objects directly like in your example.
This works as expected.

Implementing operator<< for a derived class

So I have a base class (Shape) and three derived classes, Circle, Rectangle and Square (Square is derived from Rectangle) I'm attempting to implement operator<< which just calls the correct display function for what called it. However, I don't think I have the syntax correct. Here's a snippet--where have I gone wrong?
class Shape
{
public:
Shape(double w = 0, double h = 0, double r = 0)
{
width = w;
height = h;
radius = r;
}
virtual double area() = 0;
virtual void display() = 0;
protected:
double width;
double height;
double radius;
};
ostream & operator<<(ostream & out, const Shape & s)
{
s.display(out);
return out;
}
class Rectangle : public Shape
{
public:
Rectangle(double w, double h) : Shape(w, h)
{
}
virtual double area() { return width * height; }
virtual void display()
{
cout << "Width of rectangle: " << width << endl;
cout << "Height of rectangle: " << height << endl;
cout << "Area of rectangle: " << this->area() << endl;
}
};
You're calling display like this:
s.display( out );
But display is defined as:
vritual void display() = 0;
The function was declared and defined taking no parameters. It should take a reference to std::ostream as a parameter:
virtual void display(std::ostream &) = 0;
It should also be a const method as you're passing in a const object through the operator << overload:
virtual void display(std::ostream &) const = 0;
Don't forget that in the definition of display you should be writing to the ostream object, not specifically std::cout.
Here is a compiling program on Ideone.
You have a number of problems here. Firstly, let's deal with the printing problem:
ostream & operator<<(ostream & out, const Shape & s)
Here, you're passing a const Shape. This means you can only call const methods on the s you pass in. However, you haven't marked any of the methods in the base (or derived) class as const. Neither area nor display should change the state of the object. Secondly, you're trying to call s.display(out), that is, passing an ostream& to display. The function signature you have doesn't reflect this. So putting that all together we get:
virtual double area() const = 0;
virtual void display(ostream& out) const = 0;
You've also got some other problems - a base class that doesn't declare a virtual destructor. If you're planning on using a class polymorphically, it must have a virtual destructor:
virtual ~Shape() { }
You also need to amend your methods in the derived class:
double area() const { return width * height; }
void display(ostream& out) const
{
out << "Width of rectangle: " << width << endl;
out << "Height of rectangle: " << height << endl;
out << "Area of rectangle: " << area() << endl;
}
Note that display in Rectangle was always printing to cout beforehand.
You almost got it right, here is the working solution:
#include <iostream>
using std::cout;
using std::endl;
using std::ostream;
class Shape
{
public:
Shape(double w = 0, double h = 0, double r = 0)
{
width = w;
height = h;
radius = r;
}
virtual ~Shape() {} // Recommended!
virtual double area() const = 0;
virtual void display(ostream & out) const = 0;
protected:
double width;
double height;
double radius;
};
ostream & operator<<(ostream & out, const Shape & s)
{
// Since `s` is `const`, then `display` method should be `const` too.
s.display(out);
return out;
}
class Rectangle : public Shape
{
public:
Rectangle(double w, double h) : Shape(w, h)
{
}
virtual double area() const { return width * height; }
virtual void display(ostream & out) const
{
// Since `display` method is `const`, then `area` method should be
// `const` too.
out << "Width of rectangle: " << width << endl;
out << "Height of rectangle: " << height << endl;
out << "Area of rectangle: " << this->area() << endl;
}
};
void main() {
Rectangle r(1, 2);
cout << r << endl;
}
Please, pay attention to the const qualifiers which enforce const-correctness of class methods. I've added some useful comments so that you can follow the logic smoothly. Take it as a rule of thumb, if the method does not modify class members, then you should declare it const.