Advertisement
Josif_tepe

Untitled

Nov 21st, 2021
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. class Shape{
  5. private:
  6.     int width, length;
  7. public:
  8.     Shape() {}
  9.     Shape(int _width, int _length) {
  10.         width = _width;
  11.         length = _length;
  12.     }
  13.     virtual int get_area() = 0; // only signature, the function will be written inside the derived classes
  14.     int get_widht() {
  15.         return width;
  16.     }
  17.     int get_length() {
  18.         return  length;
  19.     }
  20. };
  21. class Rectangle : public Shape {
  22. public:
  23.     Rectangle(int width, int length) : Shape(width, length) {
  24.        
  25.     }
  26.     int get_area() override {
  27.         return get_length() * get_widht();
  28.     }
  29. };
  30. class Square : public Shape {
  31. public:
  32.     Square(int width, int length) : Shape(width, length) {
  33.        
  34.     }
  35.     int get_area() override {
  36.         return get_length() * get_widht();
  37.     }
  38. };
  39. int main(){
  40.     Shape *rect = new Rectangle(4, 5);
  41.     Shape *square = new Square(5, 5);
  42.    
  43.     cout << rect->get_area() << endl;
  44.     cout << square -> get_area() << endl;
  45.     return 0;
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement