How to achieve compile time polymorphism explain in detail.

1 Answer

Answer :

The process of linking of function call to function definition at compile time is called as Compile Time Polymorphism. It can be through Function and operator overloading.

Function overloading: The process of defining the function with same name but with different number or type of argument is known as Function Overloading. In function Overloading, the function would perform different operations depending on the argument list in the function call. The correct function to invoked is determined at compile time by checking the number and type of the arguments but not on the function return type.

Example:

Function Overloading:

Swapping of two integers and swapping of two float values.

// Function definition1- swapping two integers

void swap(int*p, int*q)

{

int t;

t=*p;

*p=*q;

*q=t;

}

// Function definition2- swapping two floats

void swap(float*p,float*q)

{

float t;

t=*p;

*p=*q;

*q=t;

}


For the function call swap (&a, &b) where a and b are integers and function definition1 will be executed and function definition2 for the function call as swap (&a, &b) when a and b are floats.
OR
Operator Overloading: The Process of defining operator function to extend the use of existing operator to operate on User-defined data type such as „object of class‟ is known as Operator Overloading.
Example: Overloading + operator to concatenate two strings.
class string1
{
char str[20];
public:
void getdata()
{
cout<<"\n Enter String :";
cin>>str;
}
void display()
{
cout<<str;
}
void operator+(string1 x) //Concatenating String
{
strcat(str,x.str);
}
};
void main()
{
string1 str1, str2;
clrscr();
str1.getdata();
str2.getdata();
str1+str2;
cout<<"\n\n Concatenated String is:";
str1.display();
getch();
}
 

Related questions

Description : Compare run-time and compile-time polymorphism.

Last Answer : Compare run-time and compile-time polymorphism.

Description : Write any four differences between compile time and run time polymorphism.

Last Answer : Sr. No. Compile-time Polymorphism Run-time Polymorphism 1 Compile time polymorphism means that an object is bound to its function call at compile time. Run time polymorphism means that selection ... binding. 7 E.g. overloaded function call. E.g. virtual function.

Description : What is polymorphism? Enlist different types of polymorphism. What are the differences between them?

Last Answer : Polymorphism: It is a feature of object oriented programming which allows a programmer to have a more than one function having same name but different /same parameters but performs different/ ... Compile Time polymorphism o Virtual Function Difference between Types of Polymorphism:

Description : Polymorphism is implemented using function overloading. Justify the statement.

Last Answer : Polymorphism is a mechanism that allows a developer to have more than one function with same name but different signature. In function overloading, one can make use of more than one function with different signature as well. Hence ... main()  { int a[10],i,isum; float b[5],fsum; clrscr(); cout

Description : Define polymorphism. Enlist its types.

Last Answer : Definition: Polymorphism means ability to take more than one form that means a program can have more than one function with same name but different behavior. Types of polymorphism: 1) Compile time polymorphism 2) Runtime polymorphism 

Description : Define the following terms: (i) Data abstraction (ii) Class (iii) Dynamic binding (iv) Polymorphism

Last Answer : (i) Data abstraction: Abstraction refers to the act of representing essential features without including the background details or explanation. Data abstraction is the process of defining a data type, often ... more than one form at different instances depending on the type or number of arguments. 

Description : With suitable example, describe use of virtual function in polymorphism.

Last Answer : In order to achieve polymorphism, objects belonging to different classes should be able to respond to the same message at different instances which initiates the use of single pointer variable to refer to objects of ... void accept() { coutroll; coutname; } virtual void display() { cout

Description : What is inheritance and explain visibility modes in detail.

Last Answer : Inheritance: The mechanism of deriving new class from an old (existing) class is called as inheritance. With inheritance, one class acquires the properties of objects of other classes. ... members of derived class and public members of base class becomes public members of derived class.

Description : Define what is the difference between compile-time polymorphism and run-time polymorphism?

Last Answer : Compile time Polymorphism Compile time Polymorphism also known as method overloading. Method overloading means having two or more methods with the same name but with different signatures. Run time ... or more methods with the same name, same signature but with a different implementation.

Description : Differentiate between run time and compile time polymorphism.

Last Answer : Sr. No. Compile time polymorphism Runtime polymorphism 1 In this polymorphism, an object is bound to its function call at compile time. In this polymorphism, ... is implemented with operator overloading or function overloading It is implemented with virtual function.

Description : What is constructor? How user can declared constructor in derived class? Explain with example.

Last Answer : Constructor:- Constructor is a special member function which has same name as a class name and is used to initialize object during compile time of program. Declaring constructor in derived class If a base class contains a ... derived d(2,5); d.displaybase(); d.display(); getch(); }

Description : What is copy constructor? Explain with example.

Last Answer : Copy constructor: The copy constructor is a constructor which creates an object by initializing it with an object of the same class, which has been created previously. The copy constructor is used to: ... ; // Copy constructor is called here // Let us access values assigned by constructors cout

Description : Explain how to pass object as function argument.

Last Answer : An object may be used as function arguments in two methods:- i) A copy of the entire object is passed to the function. ii) Only the address of the object is transferred to the function. 1) Pass-by-value Since a copy ... (int h, int m) { hours = h; minutes = m; } void puttime(void)  { cout

Description : Explain the derived class access by pointer.

Last Answer : When base class and its derived class both contains same function name then the function in base class is declared as virtual using keyword virtual preceding its normal declaration. When a function is made virtual, C ... . Example: #include class Base { public: virtual void show( ) { cout

Description : Explain different operator used in C++.

Last Answer : :: Scope resolution operator: This operator allows access to the global version of a variable. Scope resolution operator is also used in classes to identify the class to which a member function belongs. :: ... and assigns it to the variable on its right. It is used with cin statement to input data.

Description : Explain the need of static member function with example.

Last Answer : A static member function can have access to only other static members (functions or variables) declared in the same class. A static member function can be called using the class name as follows: class_name:: ... test::showcount(); -------------Call to static member function. }

Description : What is parametrized constructor? Explain with example.

Last Answer : A constructor that can take arguments is known as parameterized constructor. In some applications, it may be necessary to initialize the various data members of different objects with different values when they ... object is created. Member function put displays the value of data member m . 

Description : What is the need of virtual function? Explain with example.

Last Answer : When base class and its derived class both contain same name and prototype member function then derived class function overrides base class function. Base class pointer is used to refer member functions of its ... Program/Example: #include class Base { public: virtual void show( ) { cout

Description : State characteristic of static data member. Explain why static data member must be defined outside the class. 

Last Answer : Characteristics of static data members: 1. It is initialized to zero when the first object of its class is created. No other initialization is permitted. 2. Only one copy of that member is created ... member(s) it is necessary to make static members global and re-declared outside of the class.

Description : Explain the following with syntactic rules: (i) public inheritance (ii) protected inheritance.

Last Answer : (i) public inheritance: i) When the visibility-mode is public the base class is publicly inherited. ii) In public inheritance, the public members of the base class become public members of the derived class and ... variables; Member function; }; class B : protected A { Members of class B };

Description : Explain the concept of memory allocation for object.

Last Answer : The memory space for object is allocated when it is declared & not when the class is specified. The member functions are created & placed in memory space only once when they are defined as a ... (object 1, object 2, object 3) has its own separate memory space for its member variables.

Description : Explain data encapsulation and data abstraction.

Last Answer : Data encapsulation: The wrapping up of data and function into a single unit (called class) is known as encapsulation. The data is not accessible to the outside world, and only those functions ... as they hold information. The functions that operate on these data are called as member functions.

Description : Explain concept of function overriding with example.

Last Answer : Function Overriding:- When derived class defines same name function, as defined in its base class then it is called as function overriding. In this a function in the derived class overrides the inherited function. Example : #include #include class Base { public: void Display() { cout

Description : Explain any four concept of OOP.

Last Answer : Basic Concepts of Object Oriented Programming:  1. Objects Objects are the basic run time entities in an object-oriented system. They may represent a person, a place, a bank ... objects that communicate with each other. Objects communicate with one another by sending and receiving information.

Description : Explain structure with syntax and example.

Last Answer : Structure: The Structure is a user defined data supported by object oriented programming. It has almost similar properties that any other user defined data type possess except all members are public by default.  One can ... cout<<"\nThe Value is "<<d.a; getch();  }

Description : Explain hybrid inheritance with example.

Last Answer : Hybrid inheritance is also referred as mixed inheritances. As the name suggests it is a combination of all the kinds of inheritance mechanisms, namely single inheritance, multiple inheritance, multilevel inheritance and hierarchical inheritance. ... { D d; d.getdata(); d.putdata(); }  

Description : Explain pointer to derived class with example.

Last Answer : Pointers can be used to point to the base class objects and objects of derived class. Pointers to objects of base class are compatible with pointers to objects of a derived class. Single pointer variable can be made to point ... { int a; public:  void get() { couta; }  void put() { cout

Description : Explain memory management operator with example.

Last Answer : There are two memory management operators in C++: 1. new 2. delete These two memory management operators are used for allocating and de-allocating memory blocks. C++ allow dynamic allocation ... for release of memory. Syntax: delete pointer_variable; Example: Delete p; 

Description : Explain different visibility modes and its effect in inheritance.

Last Answer : Different visibility modes are: 1) Private 2) Protected 3) Public Effect in inheritance: Private members of base class are not inherited directly in any ... become protected members of derived class and public members of base class become public members of derived class

Description : Explain the concept of overloaded constructor in a class with suitable example.

Last Answer : Overloaded constructor: When more than one constructor function is defined in a same class then it is called as overloaded constructor. All constructors are defined with the same name as ... first constructor does not accept any argument and the second constructor accepts two integer arguments.

Description : Explain the concept of this pointer.

Last Answer : Concept of this pointer: C++ use a unique keyword called “this” to represent an object that invokes a member function. This unique pointer is automatically passed to a member function when it is invoked. “this” is a pointer that always points to the object for which the member function is called.

Description : Explain constructor in derived class with suitable example.

Last Answer : When a class is declared, a constructor can be declared inside the class to initialize data members. When a base class contains a constructor with one or more arguments then it is ... one argument. Derived class constructor accepts two values and passes one value to base class constructor.

Description : Explain object as a function argument using following points with suitable example: (i) Pass by value (ii) Pass by reference

Last Answer : (i) Pass by Value: When an object is passed by value to a function, a copy of that object is created and changes are reflected on the copy object not on original object. Example: #include #include class Example { int x; public: Example(int a)  {  x=a;  }  void print()  { cout

Description : Explain pointer arithmetic with example.

Last Answer : C++ allows pointers to perform the following arithmetic operations: a. A pointer can be incremented (++) or decremented (--) b. Any integer can be added or subtracted from a pointer. c. One pointer can be subtracted ... -: 56 value of ptr +2: 22 value of ptr-1: 75 value of ptr+3: 90

Description : Explain searching elements in array using pointers.

Last Answer : Consider an array of five elements as shown below: A[5]={ 10,20,30,40,50}; Search element(SE)=30 Pointer variable is declare as *ptr; Before starting search process ... If search element is not present in an array, then after comparing all elements stop the search process.

Description : Explain single inheritance with suitable example.

Last Answer : When a single derived class is derived from only one base class then it is called as single inheritance. In a single inheritance, derived class can inherit some or all members of base class. It is implemented by specifying ... s; clrscr(); s.getdata(); s.putdata(); getch(); }

Description : Explain static member function.

Last Answer : Static member function: - a static member function can have access to only other static variables or functions declared in the same class. It can be called using the class name instead of its object. It can be declared ... ; } static void showcount()----------------static member function { cout

Description : Explain data types in C++.

Last Answer : Primitive Built-in Types: C++ offer the programmer a rich assortment of built-in as well as user defined data types. Following table lists down seven basic C++ data types: Following table lists ... task. Pointer: It is a variable that stores address of another variable of similar data type.

Description : Explain class with suitable example.

Last Answer : A class is a user defined data type which binds data and its associated functions together. It allows the data and functions to be hidden, if necessary from external use. Generally, a class ... for displaying their values. These functions provide the only access to data members of the class. 

Description : Explain parameterized constructors with example.

Last Answer : A constructor that can take arguments is known as parameterized constructor. In some applications, it may be necessary to initialize the various data members of different objects with different values when they ... as an argument. Member function put displays the value of data member m .

Description : What is dynamic memory allocation? Explain with example.

Last Answer : Allocating memory at run time (when program is in execution) is called as dynamic memory allocation. Dynamic memory allocation use malloc ( ) and calloc ( ) functions to allocate memory at run time. C++ supports these ...  sample *ptr=new sample;  ptr->getdata();  ptr->putdata(); }

Description : Explain the concept of pointer to derived classes. 

Last Answer : Pointer to derived class: Pointers can be used to point to the base class objects and objects of derived class. Pointers to objects of base class are type-compatible with pointers to objects of a derived ... of B,then any reference to that member by cptr will always access the base class member. 

Description : Explain friend function. Give example.

Last Answer : Friend function: The private members of a class cannot be accessed from outside the class but in some situations two classes may need access of each other s private data. So a common function can be declared which can be made friend of ... xyz x1; a1.get1(); x1.get1(); add(a1,x1); }

Description : Explain the concept of virtual function with example.

Last Answer : Virtual Function: A virtual function is a member function that is declared within a base class and redefined by its derived class. When base class and its derived class both contain same name member function then ... pointer. Example: #include class Base { public: virtual void show( ) { cout

Description : Explain destructor with suitable example. 

Last Answer : Destructor: 1. A destructor is a special member function whose task is to destroy the objects that have been created by constructor. 2. It does not construct the values for the data members of the class. 3. It is invoked ... main() { time t(2,43,56); t.display(); getch(); }

Description : Explain multiple constructors in a class with suitable example. 

Last Answer : Multiple constructors in a class means a class can contain more than one constructor. This is also known as constructor overloading. All constructors are defined with the same name as the class they belong to. All the ... default constructor integer(int a, int b) { m = a; n = b; cout

Description : Which among the following definitions are correct: a) Object - Any entity that has state and behavior is known as an object. b) Class - Collection of objects is called class. It is logical entity. c) ... re-usability and is used to achieve run time polymorphism. d) All of the above e) None of These

Last Answer : d) All of the above

Description : Write a program which concate and reverse string by using pointer to string.

Last Answer : #include #include void main()  { char str1[20], str2[20], rev[20],*p1,*p2; int cnt=0; clrscr(); coutstr2;  p1=&str1[0];  p2=&str2[0]; while(*p1!='\0')  { p1++;  } while(*p2!='\0')  { *p1=*p2; p1++; p2++;  }  *p1='\0'; cout

Description : What is function? What is call by value? What is call by reference? What is the difference between them?

Last Answer : Function: A function is a group/set of statements/instruction that performs a specific task. It can be system defined i.e. clrscr(), getch(), or user defined i.e. add(), sub(). ... reflected inside as well as outside the function. Difference between Call by Value and Call by Reference:

Description : Write a program which perform arithmetic operation using pointer.

Last Answer : #include<iostream.h> #include<conio.h> void main() { int num[5]={56,75,22,18,90}; int ptr; int i; cout<< array elements are:: ; for(i=0;i<5;i++) ptr=num; cout<< ... 3; cout<< value of ptr+=3:: <<*ptr; cout<< \n ; getch(); }