Why do we need virtual functions in C

0 votes
I'm learning C++ and am just starting to understand about virtual functions.

Virtual functions, according to what I've read (both in the book and online), are base class functions that can be overridden in derived classes.

However, I was able to override base functions in derived classes without using virtual earlier in the book when learning about fundamental inheritance.

So, what exactly am I missing? I know there's more to virtual functions than meets the eye, and it appears to be significant, so I'd like to be clear on what it is. I just can't seem to find a straight answer on the internet.
May 26, 2022 in Others by Nicholas
• 7,760 points
1,217 views

1 answer to this question.

0 votes

A virtual function is a base class member function that we expect to redefine in derived classes.

In the base class, a virtual function is used to ensure that the function is overridden. 

This is especially true when a pointer from a base class points to an object from a derived class.

For example, consider the code below:

class Base {
   public:
    void print() {
        // code
    }
};

class Derived : public Base {
   public:
    void print() {
        // code
    }
};
answered May 27, 2022 by Damon
• 4,960 points

Related Questions In Others

0 votes
1 answer

Why do we use Body-parser in Node.js?

For understanding this first you need to ...READ MORE

answered May 20, 2019 in Others by sunshine
• 1,300 points
8,020 views
0 votes
0 answers

How do I determine the size of my array in C?

How do I determine the size of ...READ MORE

May 1, 2022 in Others by Kichu
• 19,040 points
456 views
0 votes
1 answer

Avoiding memory leaks in C++

If you use smart pointers at all ...READ MORE

answered Nov 5, 2018 in Others by nirvana
• 3,130 points
1,500 views
0 votes
2 answers
0 votes
1 answer

Cases of static and dynamic binding in C++

When an object's static type is used to associate it with a member function, this is known as static binding (understand the type of its class). When a pointer or reference is associated with a member function based on the dynamic type of the object, this is known as dynamic binding (understand the instance of the variable at runtime). Before continuing, keep in mind that dynamic binding only works with pointers, references, and virtual functions for the base class. Because everything needed to call the function is known at compile time, the first call is a static binding (also known as early binding). Derived1 d1(1, 10); d1.display_data(); You already know that the d1 instance is a Derived1 automatic variable, and that it will call the Derived1::display data method (). The first condition is incorrect: d1 is neither a pointer nor a reference. The second condition isn't acceptable:  There is no virtual Derived1::display data. The second call is for ...READ MORE

answered Jun 7, 2022 in C++ by Damon
• 4,960 points
689 views
0 votes
1 answer

When to use virtual destructors?

When you want to delete an instance of a derived class using a pointer to the base class, virtual destructors come in handy: class Base { // ...READ MORE

answered Jun 7, 2022 in C++ by Damon
• 4,960 points
592 views
0 votes
1 answer

Are virtual functions the only way to achieve Runtime Polymorphism in C++?

fprintf is a polymorphism function in the C programming language. It can print to a file, stdout, a printer, a socket, or whatever else the system can represent as a stream if you supply it different handles. FILE* file = fopen("output.txt", "w"); ...READ MORE

answered Jun 21, 2022 in C++ by Damon
• 4,960 points
475 views
0 votes
1 answer

Why we actually need runtime polymorphism?

One of the most significant elements of ...READ MORE

answered Jun 10, 2022 in C++ by Damon
• 4,960 points
1,266 views
0 votes
1 answer

How to find out if an item is present in an std::vector?

The most straightforward solution is to count the total number of elements in the vector that have the specified value.  If the count is greater than zero, we've found our element.  This is simple to accomplish with the std::count function. #include <iostream> #include <vector> #include <algorithm> int main() { ...READ MORE

answered May 27, 2022 in Others by Damon
• 4,960 points
14,116 views
0 votes
1 answer

What is this weird colon-member (" : ") syntax in the constructor?

Foo(int num): bar(num) In C++, this is known as a Member Initializer List. Simply put, it sets the value of your member bar to num. There is a significant difference between initializing a member with the Member initializer list and assigning a value to it within the function Object() { [native code] } body. When you use the Member initializer list to initialise fields, the constructors are only called once, and the object is constructed and initialised in a single operation. If you use assignment, the fields will be initialised with default constructors and then reassigned with actual values (via the assignment operator). As you can see, there is an extra overhead of creation and assignment in the latter, which may be significant for user defined classes. Cost of Member Initialization =Object ...READ MORE

answered May 27, 2022 in Others by Damon
• 4,960 points
1,530 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP