Question

c++ 2. What is a virtual function? 3. How do you create a pure virtual function?...

c++

2. What is a virtual function?
3. How do you create a pure virtual function?
4. Know what is the difference between overloading a function and redefining a function.
5. Know the difference between static binding and dynamic binding.
6. Should you make a destructor virtual? Why or why not?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

`Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries

A virtual function a member function which is declared within a base class and is re-defined(Overriden) by a derived class. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class’s version of the function.

  • Virtual functions ensure that the correct function is called for an object, regardless of the type of reference (or pointer) used for function call.
  • They are mainly used to achieve Runtime polymorphism
  • Functions are declared with a virtual keyword in base class.
  • The resolving of function call is done at Run-time.

A pure virtual function (or abstract function) in C++ is a virtual function for which we don’t have implementation, we only declare it. A pure virtual function is declared by assigning 0 in declaration. See the following example.

class Test

{   

    // Data members of class

public:

    // Pure Virtual Function

    virtual void show() = 0;

    

   /* Other members */

};

An overloaded function is a function that shares its name with one or more other functions, but which has a different parameter list. The compiler chooses which function is desired based upon the arguments used.

A redefined function is a method in a descendant class that has a different definition than a non-virtual function in an ancestor class. Don't do this. Since the method is not virtual, the compiler chooses which function to call based upon the static type of the object reference rather than the actual type of the object.

  • Static type checking means that type checking occurs at compile time. No type information is used at runtime in that case.

  • Dynamic type checking occurs when type information is used at runtime. C++ uses a mechanism called RTTI (runtime type information) to implement this. The most common example where RTTI is used is the dynamic_cast operator which allows downcasting of polymorphic types:

By default, C++ matches a function call with the correct function definition at compile time. This is called static binding. You can specify that the compiler match a function call with the correct function definition at runtime; this is called dynamic binding. You declare a function with the keyword virtual if you want the compiler to use dynamic binding for that specific function.

Virtual destructors are useful when you might potentially delete an instance of a derived class through a pointer to base class:

class Base 
{
    // some virtual methods
};

class Derived : public Base
{
    ~Derived()
    {
        // Do some important cleanup
    }
};

Here, you'll notice that I didn't declare Base's destructor to be virtual. Now, let's have a look at the following snippet:

Base *b = new Derived();
// use b
delete b; // Here's the problem!

Since Base's destructor is not virtual and b is a Base* pointing to a Derived object, delete bhas undefined behaviour:

[In delete b], if the static type of the object to be deleted is different from its dynamic type, the static type shall be a base class of the dynamic type of the object to be deleted and the static type shall have a virtual destructor or the behavior is undefined.

In most implementations, the call to the destructor will be resolved like any non-virtual code, meaning that the destructor of the base class will be called but not the one of the derived class, resulting in a resources leak.

To sum up, always make base classes' destructors virtual when they're meant to be manipulated polymorphically.

If you want to prevent the deletion of an instance through a base class pointer, you can make the base class destructor protected and nonvirtual; by doing so, the compiler won't let you call deleteon a base class pointer.

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
c++ 2. What is a virtual function? 3. How do you create a pure virtual function?...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A.3 - 10 pts It is legal to have a pure virtual destructor in a C++...

    A.3 - 10 pts It is legal to have a pure virtual destructor in a C++ class as far as that destructor has a function body. Please explain the logic behind this requirement. int fu int fu Pure virtual functions in C++ can have function body implementation. Please explain a scenario when and how this implementation is used. Then please provide a sample code to access/invoke that original function code if it was overridden by a child class

  • write C++ program Create a class named Bicycle with one public pure virtual function showFeatures(). Create...

    write C++ program Create a class named Bicycle with one public pure virtual function showFeatures(). Create the two classes MountainBike and BeachCruiser that both publicly inherit from Bicycle. Please add necessary member variables and functions to each class. The showFeatures() function shows the details about a bicycle. should have main.cpp Bicycle.h Bicycle.cpp  MountainBike .h  MountainBike.cpp  BeachCruiser.h  BeachCruiser.cpp

  • c++ Why would one want to create a pure virtual function? For a subclass that should...

    c++ Why would one want to create a pure virtual function? For a subclass that should never be constructed directly, and force the superclasses to override the function. For a superclass that should never be constructed directly, and force the subclasses to overload the function. None of the other answers is correct. For a subclass that should never be constructed directly, and force the superclasses to overload the function. For a superclass that should never be constructed directly, and force...

  • 1. How does polymorphism contribute to the design of object-oriented systems? 2. Explain the role of...

    1. How does polymorphism contribute to the design of object-oriented systems? 2. Explain the role of the vptr and vtable in the implementation of virtual functions. 3. What is a pure virtual function and why would you ever use one? 4. What is the purpose of a virtual destructor? 5. Describe a situation where it makes sense for a class to have a destructor, but no constructor.

  • This is In C++ only Thank you 2. Employee is a base class and Hourly Worker...

    This is In C++ only Thank you 2. Employee is a base class and Hourly Worker is a derived class. Which statement about the destructors of both classes is true? al They should be declared virtual u Declaring them virtual or not does not make any difference c) They should be implemented as friend functions d) The destructor of HourlyWorker must be virtual whereas the destructor of Employee should 3. If a base class has a non-virtual member function called...

  • C++ please 27. How do you differentiate a destructor from the rest? 28. Can a static...

    C++ please 27. How do you differentiate a destructor from the rest? 28. Can a static function call an instance one? 29. Can an instance function call a static one? 30. How many objects are created? class Account { public: string firstName; string lastName; double balance; Account* p; 31. Discuss the differences among the following access modifiers, public, private, and protected. 32. When do you use the following notation? . (dot notation) :: (scope resolution operator) -> (arrow notation) 33....

  • 1. What is a virtual world? 2. How do you see virtual worlds being used in...

    1. What is a virtual world? 2. How do you see virtual worlds being used in the future? Do you think that the technology is just a trend, or something that is here to stay? Explain. 3. What are some difficulties in using a virtual world for business or professional purposes? 4. Do you seen any major benefits (or not) in using virtual worlds for marketing a business or its products in a virtual space compared to a web page?...

  • C++ 1. How do functions facilitate modular programming? 2. What does a function prototype do? 3....

    C++ 1. How do functions facilitate modular programming? 2. What does a function prototype do? 3. What does "calling" a function mean? 4. If you have two or more functions with the same name, what has to be true of them? 5. How would you explain the difference between pass-by-value and pass-by-reference?

  • please write the code in C++ 2 Base class File 3 Derived class PDF 3.1 Class...

    please write the code in C++ 2 Base class File 3 Derived class PDF 3.1 Class declaration • The class PDF inherits from File and is a non-abstract class 1. Hence objects of the class PDF can be instantiated. 2. To do so, we must override the pure virtual function clone) in the base class • The class declaration is given below. • The complete class declaration is given below, copy and paste it into your file. . It is...

  • In c++ What does operator overloading allow us to do ? Why must the << and...

    In c++ What does operator overloading allow us to do ? Why must the << and the >> operators be overloaded as friend functions instead of member functions, for a user defined class ? What is the difference between an instance member variable and a static member variable ? How are call to static member functions made ? Describe the difference between reading a file using the >> operator and with the getline function What is inheritance What is a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT