Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- class Shape{
- private:
- int width, length;
- public:
- Shape() {}
- Shape(int _width, int _length) {
- width = _width;
- length = _length;
- }
- virtual int get_area() = 0; // only signature, the function will be written inside the derived classes
- int get_widht() {
- return width;
- }
- int get_length() {
- return length;
- }
- };
- class Rectangle : public Shape {
- public:
- Rectangle(int width, int length) : Shape(width, length) {
- }
- int get_area() override {
- return get_length() * get_widht();
- }
- };
- class Square : public Shape {
- public:
- Square(int width, int length) : Shape(width, length) {
- }
- int get_area() override {
- return get_length() * get_widht();
- }
- };
- int main(){
- Shape *rect = new Rectangle(4, 5);
- Shape *square = new Square(5, 5);
- cout << rect->get_area() << endl;
- cout << square -> get_area() << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement