Polymorphism 1
Polymorphism 1
Polymorphism1
// base class
class Square
private:
int x;
public: // interface
};
// rectangle is a square
private:
int y;
public: // Interface
int main()
Square s(8);
return 0;
2. Polymorphism2
// base class
class Square
private:
int x;
public: // interface
};
// rectangle is a square
private:
int y;
public: // Interface
};
int main()
{
Square s(8);
// a Rectangle is a square
return 0;
3. Polymorphism3
// base class
class Square
private:
int x;
public: // interface
};
// rectangle is a square
private:
int y;
public: // Interface
};
s.print();
int main()
Square s(8);
// a Rectangle is a square
return 0;
}
Square!
4. Polymorphism4
// base class
class Square
private:
int x;
public: // interface
};
// rectangle is a square
private:
int y;
public: // Interface
Rectangle(int _x, int _y) : Square(_x), y(_y) {}
};
s.print();
int main()
Square s(8);
// a Rectangle is a square
return 0;
Square!
5. Polymorphism5:
class Square
private:
int x;
public: // interface
};
// rectangle is a square
private:
int y;
public: // Interface
};
{
s.print();
int main()
Square s(8);
// a Rectangle is a square
return 0;
Rectangle!
6. Polymorphism6:
// base class
class Square
private:
int x;
public: // interface
};
// rectangle is a square
private:
int y;
public: // Interface
};
s.print();
int main()
Square s(8);
// a Rectangle is a square
return 0;
Square!
7. Polymorphism7:
// base class
class Square
private:
int x;
public: // interface
};
// rectangle is a square
private:
int y;
public: // Interface
};
s.print();
int main()
Square s(8);
sptr->print();
return 0;
Rectangle!
8. Polymorphism8:
The print function in the Square class lacks a return type and should be virtual void
print() const.
The getY() function is not part of the Square class, so attempting to call getY() on a
Square pointer will cause a compilation error.
// base class
class Square
{
private:
int x;
public: // interface
Square(int _x) : x(_x) {}
// rectangle is a square
class Rectangle : public Square
{
private:
int y;
public: // Interface
Rectangle(int _x, int _y) : Square(_x) { y = _y;}
void setY(int _y){ y = _y;}
int getY() const { return y;}
virtual print() const { cout << "Rectangle!\n" ; } // overriding
};
int main()
{
Square s(8);
Rectangle r(2, 3);
sptr->print();
// cout << sptr->getY();
return 0;
}
And the output is:
Rectangle!
9. Polymorphism9:
The print function in the Square class is corrected to virtual void print() const.
You are attempting to assign a Square object address to a Rectangle pointer (Rectangle*
rptr = &s;). This is not allowed because a Square is not necessarily a Rectangle.
Here is the correct version of the code:
#include <iostream> // for cout, cin
using namespace std;
// base class
class Square
{
private:
int x;
public: // interface
Square(int _x) : x(_x) {}
// rectangle is a square
class Rectangle : public Square
{
private:
int y;
public: // Interface
Rectangle(int _x, int _y) : Square(_x) { y = _y;}
void setY(int _y){ y = _y;}
int getY() const { return y;}
virtual print() const { cout << "Rectangle!\n" ; } // overriding
};
void foo(const Square& s)
{
s.print();
}
int main()
{
Square s(8);
Rectangle r(2, 3);
return 0;
}
There will be no output.
10. Polymorphism10:
#include <iostream> // for cout, cin
using namespace std;
// base class
class Square
{
private:
int x;
public: // interface
Square(int _x) : x(_x) {}
virtual ~Square() {}
// rectangle is a square
class Rectangle : public Square
{
private:
int y;
public: // Interface
Rectangle(int _x, int _y) : Square(_x), y(_y) {}
void setY(int _y) { y = _y; }
int getY() const { return y; }
void print() const override { cout << "Rectangle!\n"; } // Fix: Added void return type and
override keyword
};
// Foo is a square
class Foo : public Square
{
public:
Foo(int _x) : Square(_x) {} // Fix: Correct constructor definition
void print() const override { cout << "Foo!\n"; } // Fix: Added void return type and override
keyword
virtual ~Foo() {}
};
void foo(const Square& s)
{
s.print();
cout << s.getX() << "\n";
}
int main()
{
Square s(8);
Rectangle r(2, 3);
return 0;
}
The output is:
Square!
Rectangle!
Foo!