Skip to content

Commit

Permalink
updated dynamic polymorphism note
Browse files Browse the repository at this point in the history
  • Loading branch information
Rishabh672003 committed Dec 25, 2023
1 parent 33b7016 commit 1b0ca09
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions src/28-dynamic-polymorphism.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,20 @@ Here's an example in C++ demonstrating dynamic polymorphism.

// Base class
class Shape {
public:
virtual void draw() {
std::cout << "Drawing a shape" << std::endl;
}
public:
virtual void draw() { std::cout << "Drawing a shape" << std::endl; }
};

// Derived class 1
class Circle : public Shape {
public:
void draw() override {
std::cout << "Drawing a circle" << std::endl;
}
public:
void draw() override { std::cout << "Drawing a circle" << std::endl; }
};

// Derived class 2
class Rectangle : public Shape {
public:
void draw() override {
std::cout << "Drawing a rectangle" << std::endl;
}
public:
void draw() override { std::cout << "Drawing a rectangle" << std::endl; }
};

int main() {
Expand Down Expand Up @@ -64,3 +58,24 @@ Drawing a rectangle
```
As you can see, using dynamic polymorphism, we can determine at runtime which `draw` method should be called based on the type of object being used.
## **Never use `malloc` in C++**
In this code calling of `f()` with the _a2_ object will cause a segmentation fault as _a2_ is instantiated using `malloc`
So don't use `malloc` use `new` keyword instead in C++
```cpp
class A {
public:
int x = 3;
virtual void f() { cout << "abc"; }
};
A* a1 = new A;
A* a2 = (A*)malloc(sizeof(A));
std::cout << a1->x; // print "3"
cout << a2->x; // undefined value!!
a1->f(); // print "abc"
a2->f(); // segmentation fault
```

0 comments on commit 1b0ca09

Please sign in to comment.