Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

#include <iostream>

// Base class

class Shape {

public:

virtual void draw() const {

std::cout << "Drawing a generic shape." << std::endl;

};

// Derived class 1 (inherits from Shape)

class Circle : public Shape {

private:

double radius;

public:

Circle(double r) : radius(r) {}

void draw() const override {

std::cout << "Drawing a circle with radius " << radius << std::endl;

};

// Derived class 2 (inherits from Shape)

class Rectangle : public Shape {

private:

double length;

double width;

public:

Rectangle(double l, double w) : length(l), width(w) {}


void draw() const override {

std::cout << "Drawing a rectangle with length " << length << " and width " << width << std::endl;

};

int main() {

// Creating instances of the derived classes

Circle myCircle(5.0);

Rectangle myRectangle(4.0, 6.0);

// Accessing methods from the base class (Shape)

myCircle.draw();

myRectangle.draw();

return 0;

You might also like