KEY: DSE001 DumanDOCS!

Press on this button to view the code on DumanDOCS!
Where you can find code examples to support the understanding.


Let’s say that we have 2 classes like shown below. Which triangle inherited it’s area property from the quadrilateral class.

class quadrilateral
{
public:
  quadrilateral() {}
  virtual ~quadrilateral() {}
  virtual int area(int width, int height) {
    cout << "Quadrilateral runs." << endl;
    return width*height;
  }
  int perimeter(int width, int height) {
    return (width+height)*2;
  }
};

class triangle : public quadrilateral
{
public:
  int area(int width, int height) {
    cout << "Triangle runs." << endl;
    return (width*height)/2;
  }
};

Now, let’s create the main function. So, everything will be clearer.

int main()
{
    quadrilateral *ptr;
    triangle object;
    ptr = &object;
    cout << ptr->area(2,3);
return 0;
}

As you can see we’ve created a pointer and an object for triangle. Then we’ve pointed to the object, last but not least, we requested the area for width = 2 and height = 3.
The program will output:

Triangle runs.
3

Everything seems normal, right? Let’s change only one line from the code. (6th line from the top box)

int area(int width, int height) {

Now let’s run the program and see what’s the result.

Quadrilateral runs.
6

That sucks! Why did that happen? It’s because we didn’t define it as virtual. Before coming to a conclusion let’s talk about one last thing. Even if there’s no virtual keyword defined at the base class if we run this through main, the code will still work fine.

quadrilateral test;
test.area(2,3);
triangle test2;
test2.area(2,3);

It will print:

Quadrilateral runs.
6
Triangle runs.
3

So… why don’t we just directly call everything? You can, but if you want to use a pointer to call the function, and you’re using inheritance… you will need the virtual keyword only in the base class function. This way, pointer will know what’s up and will return the correct value. Otherwise as we demonstrated before, you will meet with an unwanted result.

So… pretty much that’s it. Also as a side note see what I’ve done here:

virtual ~quadrilateral() {}

There’s no regular destructor in the entire class definition. Only a virtual destructor. Why?
I’d recommend reading the answers provided here: When to use virtual destructors? [stackoverflow]

Bonus: PURE Virtual Functions!

Pure virtual methods in C++ are basically a way to define interfaces without requiring them to be implemented.

Example code:

class Example {
    // regular, old, virtual destructor
    virtual ~Example();

    // pure virtual, must be implemented by subclasses
    virtual void myMethod() = 0;

    // normal method, will be available to all subclasses,
    // but *can* be overridden
    virtual void myOtherMethod();
};

Making a pure virtual function also turns that class into an abstract class.

About what an abstract class is: Anstract Classes – (Interfaces) [tutorialspoint]

Have a nice day!

Categories: C++CODE

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

C++

Operator overloading! (C++)

Press on this button to view the code on DumanDOCS! Where you can find code examples to support the understanding. Why do we need operator overloading? To make operators work in a way that we want, obviously. Read more…

CODE

Get a 100/100 PageSpeed Score!

As you can see, currently my blog is getting a whopping 100/100! Also it looks nice, well, that’s in my opinion. But, still… First things first, you need to enable gzip. You can either use a Read more…