Q1. How does c++ allows some functions to be used in a program even before the functions are defined ? Describe the mechanism that makes it possible and give examples.
Solution:
The mechanism is called : runtime
plymorphism or dynamic/late binding.
In C++ we can achieve runtime polymorphism using
virtual function.
A virtual member function is a function which
is declared within the base class
but gets defined in child class.
Functions are declared with virtual keywords in
base class.
By refering to a child class object using
pointer of parent class we can call
a virtual function which is declared in parent
class but defined in child class.
The resolving of function call is done at
runtime.
At runtime C++ compiler will find out which
child class is being referred by a parent class pointer
and will call the function which is defined in
child class.
e.g
#include<iostream>
using namespace std;
// Parent class with a virtual function.
class Parent
{
public:
// Virtual funuction
virtual void print ()
{
cout<< "print Parent class" <<endl;
}
void show ()
{
cout<< "show Parent class" <<endl;
}
};
// Child class extending parent class and
re-defining virtual function of parent class
class Child:public Parent
{
public:
//Virtual function declared in Parent class got redfined here
void print ()
{
cout<< "print child class" <<endl;
}
// Non-virtual function of child class itself.
void show ()
{
cout<< "show child class" <<endl;
}
};
int main()
{
// Pointer of parent
class.
Parent *bptr;
//Child class
object
Child d;
//Parent class
referring to child class object
bptr = &d;
/*
* virtual function
binded at runtime. Parent class pointer is pointing to child class
object hence
* print method from
child class will be called.
*/
bptr->print();
// Non-virtual
function, binded at compile time. Compiler will check whether show
function is present in Parent class or not.
// If not it will throw
error.
bptr->show();
}
Sample Output:
print derived
class
show base class
Q2. How does c++ implements virtual function ?
Solution:
Virtual functions are implemented using virtual table in
C++.
The virtual table is a lookup table of functions used to resolve
function calls in a dynamic/late binding manner.
Virtual table is also called as “vtable”, “virtual function table”,
“virtual method table”.
First, every class that uses virtual functions (or is derived
from a class that uses virtual functions) is
given its own virtual table. This table is simply a static array
that the compiler sets up at compile time.
A virtual table contains one entry for each virtual function that
can be called by objects of the class.
Each entry in this table is simply a function pointer that points
to the most-derived function accessible by that class.
Second, the compiler also adds a hidden pointer to the base
class, which we will call *__vptr. *__vptr is
set (automatically) when a class instance is created so that it
points to the virtual table for that class.
Unlike the *this pointer, which is actually a function parameter
used by the compiler to resolve self-references,
*__vptr is a real pointer. Consequently, it makes each class object
allocated bigger by the size of one pointer.
It also means that *__vptr is inherited by derived classes, which
is important.
Q3. Disadvantages of virtual function ?
Solution:
Slower:
The function call takes are slower because the
compiler doesnt know
which function to call at compile time. It
lookup every time in vtable for this.
Memory:
Additional 4 bytes for every instance of a class
that has at least one virtual function.
The code becomes also complex to understand
sometimes.
4. Consider virtual functions. a) How does C++ allow some functions to be used in a...
4.a)
4.b>
4.c)
C++ Programming Lab Exercise 09 Inheritance. Friend Functions, and Polymorphism (virtual functions) 4.a) Run the following code and observe the output. #include <iostream> #include <string> using namespace std; class Vehicle public: void print() { cout << "Print: I am a vehicle. \n"; } void display() { cout << "Display: I am a vehicle. \n"; } }; class Car: public Vehicle { public: void print() { cout << "Print: I am a car.\n"; } void display() { cout...
Hello
these are midterm study guide questions for my
C++ class. I need some clarification on how to
understand and write these functions.
Can someone write out examples and use definitions on how to go
about the stuff listed below?
again this is for an introductory C++ computer science class
Functions Predefined Functions Know basic predefined functions covered from <cmath> Know how to use the rand) function from <stdlib.h> Be prepared to convert arithmetic expressions to function calls. Programmer Defined...
C++ Programming Assignment S Mammal Lab This lab's goal is to give you some practice using inheritance, virtual functions, pointers, dynamic memory allocation, random numbers, and polymorphism. To complete the lab implement the following steps: Create a class called Mammal. All mammals have a weight and a name, so its data should be the mammal's weight and name. Provide a default constructor that sets the mammal's weight to 0 and name to null, and another constructor that allows the weight...
PROJECT 6 Functions You are to write a PYTHON program which: *Defines the following 4 functions: whoamI() This function prints out your name and lists any programming course you have taken prior to this course. isEven(number) This function accepts one parameter, a number, and prints out a message indicating if the number is even or odd. Keep in mind that a number is even if there is no remainder when the number is divided by two. printEven(number) This function accepts one...
This lab will exercise your understanding of some of the concepts covered in Chapter 12: virtual functions (think about compile-time and run-time binding) 1. We will be treating the PersonType object as a base class that may be inherited by multiple objects. We will treat the personType getAddress and setAddress as pure virtual because we wish to have all inherited objects to code these functions but do not need them in the base class. Using the code for person type...
C++ please no arrays! Create a program that will allow the user to enter up to 50 whole values and determine the number of values entered, how many of the values were odd and how many of the values were even. Your code should contain 2 functions, a main function and a function to enter the numbers enter and count the values. Your main function should be a driver program that will call another function to count the total number...
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...
Problem 5. Consider least squares polynomial approximation to f(x) = cos (nx) on x E [-1,1] using the inner product 1. In finding coefficients you will need to compute the integral By symmetry, an 0 for odd n, so we need only consider even n. (a) Make a change of variables and use appropriate identities to transform the integral for a to cos (Bcos 8)cos (ne) de (b) The Bessel function of even order, (x), can be defined by the...
You should implement several functions that the Phone Contacts program will need Each function with take in data through the parameters (i.e., variables names listed in parentheses) and make updates to data structure that holds the contact information, return, or print information for a particular contact. The parameters needed are listed in the table below and each function is described below that 1. Make an empty dictionary and call it 'contacts'. Where in your code would you implement this? Think...
CSC 430 GRAPH PROJECT Implement a graph class using an adjacency matrix or an adjacency list. The class should have a constructor, a copy constructor, a destructor and all the methods necessary to add/delete vertices, add/delete edges… Write a menu-driven program to THOROUGHLY check your class and all the functions included in your program. You can choose the data type. Allow the user to continue with operations as long as he/she wants to. Your program should check if an operation...