0% found this document useful (0 votes)
16 views18 pages

Polymorphism 1

Uploaded by

Bereket Worku
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views18 pages

Polymorphism 1

Uploaded by

Bereket Worku
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

1.

Polymorphism1

#include <iostream> // for cout, cin

using namespace std;

// base class

class Square

private:

int x;

public: // interface

Square(int _x) : x(_x) {}

void setX(int _x) { x = _x; }

int getX() const { return x; }

void print() const { cout << "Square!\n"; }

};

// 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 { cout << "Rectangle!\n" ; }


};

void foo(const Square& s)

cout <<s.getX() << "\n";

int main()

Square s(8);

Rectangle r(2, 3);

foo(s); // What is the output?

return 0;

The output is:

2. Polymorphism2

#include <iostream> // for cout, cin

using namespace std;

// base class

class Square

private:

int x;
public: // interface

Square(int _x) : x(_x) {}

void setX(int _x) { x = _x; }

int getX() const { return x; }

void print() const { cout << "Square!\n"; }

};

// 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 { cout << "Rectangle!\n" ; }

};

void foo(const Square& s)

cout <<s.getX() << "\n";

int main()

{
Square s(8);

Rectangle r(2, 3);

// permitted because of polymorphism

// a Rectangle is a square

foo(r); // What is the output?

return 0;

The output is:

3. Polymorphism3

#include <iostream> // for cout, cin

using namespace std;

// base class

class Square

private:

int x;

public: // interface

Square(int _x) : x(_x) {}

void setX(int _x) { x = _x; }

int getX() const { return x; }

void print() const { cout << "Square!\n"; }

};
// 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 { cout << "Rectangle!\n" ; } // Redefine

};

void foo(const Square& s)

s.print();

int main()

Square s(8);

Rectangle r(2, 3);

// permitted because of polymorphism

// a Rectangle is a square

foo(s); // What is the output?

return 0;
}

The output is:

Square!

4. Polymorphism4

#include <iostream> // for cout, cin

using namespace std;

// base class

class Square

private:

int x;

public: // interface

Square(int _x) : x(_x) {}

void setX(int _x) { x = _x; }

int getX() const { return x; }

void print() const { cout << "Square!\n"; }

};

// 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 { cout << "Rectangle!\n" ; } // Redefine

};

void foo(const Square& s)

s.print();

int main()

Square s(8);

Rectangle r(2, 3);

// permitted because of polymorphism

// a Rectangle is a square

foo(r); // What is the output?

return 0;

The output is:

Square!

5. Polymorphism5:

#include <iostream> // for cout, cin

using namespace std;


// base class

class Square

private:

int x;

public: // interface

Square(int _x) : x(_x) {}

void setX(int _x) { x = _x; }

int getX() const { return x; }

virtual print() const { cout << "Square!\n"; }

};

// 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);

// permitted because of polymorphism

// a Rectangle is a square

foo(r); // What is the output?

return 0;

The output is:

Rectangle!

6. Polymorphism6:

#include <iostream> // for cout, cin

using namespace std;

// base class

class Square

private:

int x;

public: // interface

Square(int _x) : x(_x) {}


void setX(int _x) { x = _x; }

int getX() const { return x; }

virtual print() const { cout << "Square!\n"; }

};

// 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);


// permitted because of polymorphism

// a Rectangle is a square

foo(r); // What is the output?

return 0;

The output is:

Square!

7. Polymorphism7:

#include <iostream> // for cout, cin

using namespace std;

// base class

class Square

private:

int x;

public: // interface

Square(int _x) : x(_x) {}

void setX(int _x) { x = _x; }

int getX() const { return x; }

virtual print() const { cout << "Square!\n"; }

};

// 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);

Square* sptr = &r;

sptr->print();

return 0;

The output is:

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.

 The cout in the main function should be commented.


Here is the correct code:
#include <iostream> // for cout, cin
using namespace std;

// base class
class Square
{
private:
int x;

public: // interface
Square(int _x) : x(_x) {}

void setX(int _x) { x = _x; }


int getX() const { return x; }
virtual print() const { cout << "Square!\n"; }
};

// 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);

Square* sptr = &r;

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) {}

void setX(int _x) { x = _x; }


int getX() const { return x; }
virtual print() const { cout << "Square!\n"; }
};

// 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);

Rectangle* rptr = &r;

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

void setX(int _x) { x = _x; }


int getX() const { return x; }
virtual void print() const { cout << "Square!\n"; } // Fix: Added void return type
};

// 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);

// Create an array of Square pointers


Square* squares[] = { new Square(3), new Rectangle(1, 2), new Foo(1) };

for (Square* s : squares) // Correct loop variable type


s->print();

for (Square* s : squares)


delete s;

return 0;
}
The output is:
Square!
Rectangle!
Foo!

You might also like