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...
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...
C++ help This assignment gives you practice with inheritance and pure virtual functions. You will implement some code that could be part of a program to play a popular card game called “Crazy 8’s”, which is the basis for the game “Uno”. You will start to implement the rules, as described at: https://www.pagat.com/eights/crazy8s.html . Note that the inheritance relationship below is also based on the organization of card games described on that website. Requirements: your work will be split into...
2 Utility Functions (2 Points) Consider the utility function u(c) where c denotes consumption of some arbitrary good and ơ (the Greek letter "sigma") is known as the "curvature parameter" because its value governs how curved the utility function is and is treated as a constant. In the following, restrict your attention to the region c > 0 (because "negative consumption" is an ill-defined concept) a. (0.50 Points) Plot the utility function for σ 0, Does this utility function display...
Are the Resulting Functions Linear? At this point you should wonder whether these combinations of functions are linear or not Problem I Show that if S and T are linear functions from V to V and c is a real number, then the functions S+T and c.T defined above are indeed linear functions from V ta V Because of Problem 1 we know that the set Linear(V, W) (all linear functions from V to W.) has rules for addition and...