28 Lecture

CS304

Midterm & Final Term Short Notes

VIRTUAL FUNCTIONS

Virtual functions in C++ are a mechanism for achieving dynamic polymorphism, which allows a function to be called based on the actual type of an object, rather than the declared type. Virtual functions are defined in a base class and are over


Important Mcq's
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is the purpose of virtual functions in C++? A. To achieve static polymorphism B. To achieve dynamic polymorphism C. To improve code readability D. To increase program performance Answer: B Which keyword is used to declare a function as virtual in C++? A. static B. virtual C. dynamic D. polymorphic Answer: B In which class are virtual functions declared in C++? A. Base class B. Derived class C. Abstract class D. Static class Answer: A Which function is called when a virtual function is invoked through a base class pointer? A. Base class function B. Derived class function C. Default function D. Static function Answer: B What is a virtual function table (vtable) in C++? A. A table that stores the addresses of all virtual functions in a class hierarchy B. A table that stores the names of all virtual functions in a class hierarchy C. A table that stores the values of all virtual functions in a class hierarchy D. A table that stores the types of all virtual functions in a class hierarchy Answer: A Can a derived class override a non-virtual function of its base class in C++? A. Yes B. No Answer: A What is the syntax for providing a default implementation of a virtual function in C++? A. virtual void functionName() { ... } B. virtual void functionName() = 0; C. virtual void functionName() default; D. virtual void functionName() { ... } default; Answer: D What is the difference between a pure virtual function and a virtual function with a default implementation in C++? A. A pure virtual function has no implementation, while a virtual function with a default implementation does B. A pure virtual function cannot be called, while a virtual function with a default implementation can be C. A pure virtual function is declared with the = 0 syntax, while a virtual function with a default implementation is declared with the = default syntax D. There is no difference between the two Answer: A Can virtual functions be defined as private in a C++ class? A. Yes B. No Answer: A What is the purpose of a virtual destructor in C++? A. To improve program performance B. To allow objects to be destroyed properly in a class hierarchy C. To prevent memory leaks D. To allow objects to be cloned easily Answer: B



Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is a virtual function in C++ and how is it declared? Answer: A virtual function is a function that can be overridden by a derived class. It is declared using the virtual keyword before the function prototype in the base class. What is the difference between a virtual function and a non-virtual function in C++? Answer: A virtual function can be overridden by a derived class, while a non-virtual function cannot be overridden. Can a virtual function be static or friend in C++? Answer: No, a virtual function cannot be static or friend in C++. What is the purpose of a pure virtual function in C++ and how is it declared? Answer: A pure virtual function is a virtual function that has no implementation in the base class and must be overridden by the derived class. It is declared using the syntax virtual void functionName() = 0; Can a virtual function be defined outside of its class in C++? Answer: Yes, a virtual function can be defined outside of its class in C++. Can a constructor or destructor be virtual in C++? Answer: Yes, a constructor or destructor can be virtual in C++. What is a virtual function table (vtable) in C++? Answer: A virtual function table is a table that stores the addresses of all virtual functions in a class hierarchy. What is the difference between early binding and late binding in C++? Answer: Early binding is when the function call is resolved at compile-time based on the type of the object pointer, while late binding is when the function call is resolved at runtime based on the type of the object pointed to. What is the difference between function overloading and function overriding in C++? Answer: Function overloading is when multiple functions have the same name but different parameters, while function overriding is when a derived class provides its own implementation of a virtual function in the base class. What is the purpose of a virtual destructor in C++? Answer: The purpose of a virtual destructor is to ensure that the destructor of the derived class is called when a derived class object is deleted through a pointer to the base class.

Virtual functions are an important concept in object-oriented programming and are commonly used in C++. A virtual function is a member function of a class that can be overridden by a derived class. When a derived class overrides a virtual function, it provides its own implementation of the function. This allows the derived class to customize the behavior of the function without modifying the base class. Virtual functions are declared using the virtual keyword before the function prototype in the base class. When a virtual function is called, the function that is executed depends on the type of the object pointed to, not the type of the pointer itself. This is known as dynamic or late binding. In addition to virtual functions, C++ also supports pure virtual functions, which are virtual functions that have no implementation in the base class and must be overridden by the derived class. Pure virtual functions are declared using the syntax virtual void functionName() = 0; C++ also uses a virtual function table (vtable) to store the addresses of all virtual functions in a class hierarchy. When a virtual function is called, the vtable is used to determine which function to execute. One important use of virtual functions is to implement polymorphism, which allows objects of different types to be treated as if they are of the same type. This is done by defining a common interface using virtual functions and allowing derived classes to override the functions as needed. Polymorphism makes it possible to write generic code that can work with objects of different types without knowing the specific type at compile-time. Overall, virtual functions are a powerful feature of C++ that allow for flexible and extensible object-oriented programming. They are widely used in many applications and are an important tool for software developers to master.