Object Oriented Programming · Unit 8
Static and Dynamic Polymorphism
Exam-focused notes for Static and Dynamic Polymorphism (Object Oriented Programming, BIT153): what the TU syllabus asks and how it has actually been tested, with 12 solved past questions from this unit.
What this unit covers
- Static polymorphism: function overloading
- Static polymorphism: operator overloading
- Dynamic polymorphism definition
- Virtual functions
- Pure virtual functions
- Abstract classes and concrete classes
- Early binding versus late binding
- Function overriding
Early binding versus late binding
Differentiate between early binding and late binding. Write a program to show dynamic polymorphism using virtual function. [2+3]
--- Feature Early Binding (Static Binding) Late Binding (Dynamic Binding) --------- Time of Resolution Resolved at compile time Resolved at run time Mechanism Uses normal functions / function overloading Uses virtual functions Flexibility Less flexible More...
Full solved answer →How-late binding differs from early binding? How can you achieve dynamic polymorphism? Explain with example. [5]
Early binding means the compiler resolves the function call at compile time. The decision of which function to call is made before the program runs. - Also called compile-time binding or static polymorphism - Examples: function overloading, operator overloa...
Full solved answer →How late binding differs from early binding? How can you achieve dynamic polymorphism? Explain with example. [5]
Early binding means the compiler resolves the function call at compile time. The decision of which function to call is made before the program runs. - Also called compile-time binding or static polymorphism - Examples: function overloading, operator overloa...
Full solved answer →Abstract classes and concrete classes
What is the difference between abstract class and concrete class? Explain with example. [5]
C++ has no abstract keyword. A class becomes abstract the moment it declares at least one pure virtual function, written by putting = 0 after a virtual member function instead of a body: An abstract class cannot be instantiated. The compiler refuses Shape s...
Full solved answer →Distinguish between concrete class and abstract class. Describe the roles of polymorphism. [5]
Feature Concrete class Abstract class --------- Definition Every member function it declares or inherits has a body It declares at least one pure virtual function, written virtual T f() = 0; Instantiation Objects can be created directly No object can be cre...
Full solved answer →Differentiate between abstract class and concrete class.Write a program to create a class named 'Quadratic' that represents a function of the form f(x) = ax + bx +C, where x is a real variable and a, b, c are real constants. The class must satisfy the following requirements: a. A constructor should be provided that takes the values of a, b and c as arguments. All three of these arguments should default to zero. b. A function that takes a single argument x return the value of f(x). c. All data members should be private.[2+8]
(a) Difference Between Abstract Class and Concrete Class Feature Abstract Class Concrete Class --------- Definition A class that cannot be instantiated directly and may contain pure virtual functions A class that can be instantiated directly and provides im...
Full solved answer →Virtual functions
What is Polymorphism in Object Oriented Programming? Implement a class Employee with a virtual function CalculateSalary() that returns the salary of the employee. Create two derived classes HourlyEmployee and SalariedEmployee. Override the CalculateSalary() function in both derived classes to calculate the salary of an hourly employee and a salaried employee respectively. Write a program that creates objects of both derived classes and calls the CalculateSalary() function for each.[10]
Polymorphism is one of the four fundamental pillars of Object Oriented Programming (along with Encapsulation, Inheritance, and Abstraction). The word polymorphism comes from Greek meaning "many forms". In OOP, polymorphism means the ability of a single inte...
Full solved answer →Differentiate between virtual function and pure virtual function with an example. [5]
A virtual function is a member function declared in a base class using the virtual keyword that can be overridden in derived classes. It has a definition (body) in the base class, so objects of the base class can still be instantiated and the function can b...
Full solved answer →Function overriding
Differentiate between function overloading and function overriding in C++. [5]
--- Function overloading means defining multiple functions with the same name but with different parameter lists (different number, type, or order of parameters) within the same class or scope. - Resolved at compile time (Static/Early Binding) - Occurs in t...
Full solved answer →Pure virtual functions
Explain the use of pure virtual function with example. [5]
A pure virtual function is a virtual function that has no implementation in the base class and is declared by assigning = 0 in its declaration. A class containing at least one pure virtual function is called an Abstract Class. --- Feature Description ------...
Full solved answer →Explain pure virtual functions with example. [5]
A pure virtual function is a virtual function that has no implementation in the base class and is declared by assigning = 0 in its declaration. It forces every derived class to provide its own implementation of that function. Syntax: --- Feature Description...
Full solved answer →Static polymorphism
What are the different ways of achieving static polymorphism?Discuss overloading of pre-increment and post-increment operators with suitable examples.[5+5]
(a) Ways of Achieving Static Polymorphism Static Polymorphism (also called compile-time polymorphism) is the mechanism where the decision about which function or operator to call is made at compile time rather than at runtime. It is achieved through the fol...
Full solved answer →Make Unit 8 stick
Practice BIT153 with flashcards & quizzes