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

0 votes

"How is Runtime Polymorphism achieved in C++?" one of my friends inquired. 

"By Inheritance," I replied.

"No, it can only be done with virtual functions," he said.

As a result, I gave him the following code as an example:-

#include<iostream>
using namespace std;

class A
{
public:
    int i;
    A(){i=100;}
};

class B : public A
{
public:
    int j;
    B(){i = -1; j = 99;}
};

void func(A& myA)
{
    cout<<myA.i << endl;
}

int main()
{
    B b;
    A* a = new B();
    func(*a);
    func(b);
    delete a;
    return 0;
}

We can print the value of public member I using the function func(), which takes a reference to A but accepts an object from B. 

Compile time polymorphism, he explained.

My inquiries are as follows:

1) Is virtual function polymorphism the only way to achieve runtime polymorphism?

2) Is the above example polymorphic at runtime or at compile time?

3) In the event that I have the following code:

void func2(A& myA)
{
    cout << myA.i << endl;
    // dynamic/static cast myA to myB
    cout<<myB.j << endl;
}

Is it a polymorphism of some sort? 

Is it polymorphism, or something else?

Jun 6, 2022 in C++ by Nicholas
• 7,760 points
435 views

1 answer to this question.

0 votes

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");                    // a file
FILE* file = stdout;                                      // standard output
FILE* file = fopen("/dev/usb/lp0", "w");                  // a usb printer
FILE* file = popen("/usr/bin/lpr -P PDF", "w");           // a PDF file
FILE* file = fdopen(socket(AF_INET,SOCK_STREAM,0), "r+"); // network socket

fprintf(file, "Hello World.\n");
answered Jun 21, 2022 by Damon
• 4,960 points

Related Questions In C++

0 votes
0 answers

What is the fastest way to transpose a matrix in C++?

I have a reasonably large matrix that I need to transpose.  Assume, for example, that my matrix is a b c d e f g h ...READ MORE

Jul 15, 2022 in C++ by Nicholas
• 7,760 points
656 views
0 votes
1 answer

Easiest way to convert int to string in C++

std::stoi (and variations for each numeric type) ...READ MORE

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

What is compile-time polymorphism and why does it only apply to functions?

"Compile time polymorphism" used to signify function overloading.  It only applies to functions because that's all you can overload. Templates in modern C++ modify this.  One example has previously been provided by Neil Butterworth.  Another technique is template specialisation.  As an example: #include <iostream> #include <string> template <class T> struct my_template { ...READ MORE

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

how could I use the power function in c/c++ without pow(), functions, or recursion

It is part of a series.  Replace pow() with the previous iteration's value. There is no need for code to call pow ().  Pow(x, 5 * I - 1) and pow(-1, I - 1) may be formed since both have an int exponent dependent on the iterator I from the previous loop iteration. Example: Let f(x, i) = pow(x, 5 * i ...READ MORE

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

Why do we need virtual functions in C++?

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 ...READ MORE

answered May 27, 2022 in Others by Damon
• 4,960 points
1,087 views
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
654 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
562 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,212 views
0 votes
1 answer

What is the best way to use a HashMap in C++?

The ordered and unordered map containers (std::map and std::unordered map) are included in the standard library.  The items in an ordered map are sorted by key, and insert and access are in O (log n).  For ordered maps, the standard library often use red black trees.  However, this is only an implementation detail.  Insert and access are in O in an unordered map (1).  It is simply another term for a hashtable. An illustration using (ordered) std::map: #include <map> #include <iostream> #include <cassert> int main(int argc, char ...READ MORE

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

Easiest way to convert int to string in C++

C++ adds std::stoi (and variants for each numeric type) and std::to string, which are the C equivalents of atoi and itoa but expressed in terms of std::string #include <string> std::string s = std::to_string(42); Is therefore ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
1,311 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