Question

In: Computer Science

c++ Define polymorphism. What is the benefit of using pointers with polymorphic functions?

c++
Define polymorphism. What is the benefit of using pointers with polymorphic functions?

Solutions

Expert Solution

Polymorphism can be defined as a code that is processed or run multiple times to achieve the desired output. There are two types of polymorphism run-time(Method overriding) or compile-time(Method overloading).

Benefits of Polymorphism

  • Code can be used multiple times
  • A single variable can be used to store multiple data types
  • Assist smooth compilation

E.g

#include <iostream> 
Using namespace std: 

class student {  
   public: 
   void show() {    
      cout << "Hello Welcome to class"; 
   } 
}; 

class School:public student { 
   public: 
   void show() {   
      cout << "Please Study well"; 
   } 
};  

int main() {   
   student x;        // Base class object 
   school y;        // Derived class object 
   x.show();   // student class method is called 
   y.show();   // school class method is called 
   return 0; 
} 

Output

Hello welcome to class

Please study well


Related Solutions

Write a C++ program (using pointers and dynamic memory allocation only) to implement the following functions...
Write a C++ program (using pointers and dynamic memory allocation only) to implement the following functions and call it from the main function. (1)Write a function whose signature looks like (char*, char) which returns true if the 1st parameter cstring contains the 2nd parameter char, or false otherwise. (2)Create an array of Planets. Populate the array and print the contents of the array using the pointer notation instead of the subscripts.
what is DNA polymorphism and define SNP what does it meangenetically
what is DNA polymorphism and define SNP what does it mean genetically
Using C#, modify the codes below to do the following: Develop a polymorphic banking application using...
Using C#, modify the codes below to do the following: Develop a polymorphic banking application using the Account hierarchy created in the codes below. Create an array of Account references to SavingsAccount and CheckingAccount objects. For each Account in the array, allow the user to specify an amount of money to withdraw from the Account using method Debit and an amount of money to deposit into the Account using method Credit. As you process each Account, determine its type. If...
Java programming year 2 Polymorphism superclass variables and subclass objects polymorphic code -Classwork Part A ☑...
Java programming year 2 Polymorphism superclass variables and subclass objects polymorphic code -Classwork Part A ☑ Create a class Employee. Employees have a name. Also give Employee a method paycheck() which returns a double. For basic employees, paycheck is always 0 (they just get health insurance). Give the class a parameterized constructor that takes the name; Add a method reportDeposit. This method prints a report for the employee with the original amount of the paycheck, the amount taken out for...
this program is to be done in c language. Using Pointers Create a program pointerTester.c to...
this program is to be done in c language. Using Pointers Create a program pointerTester.c to experiment with pointers. Implement the following steps one by one in your program: YOU NEED TO ANSWER QUESTION Use printf to print your answers at the end(after 12). 1. Declare three integer variables a, b and c. Initialize them to 0, 100 and 225, respectively. 2. Print the value of each variable and its address. 3. Add the following declaration to your code: int...
This class models people moving in together in real life using pointers in C++. What test(s)...
This class models people moving in together in real life using pointers in C++. What test(s) could be added with the code below? At the end of this task, add them! (Answer this.) class Person { public: Person(const string& name) : name(name) {} void movesInWith(Person& newRoomate) { roomie = &newRoomate; // now I have a new roomie newRoomate.roomie = this; // and now they do too } const string& getName() const { return name; } // Don't need to use...
The concept of pointers in C++ is inherited from the C language, which relies extensively on the use of pointers.
  Topic: The concept of pointers in C++ is inherited from the C language, which relies extensively on the use of pointers. What are the advantages and disadvantages of having the functionality of pointers in a programming language?
C++ programming test 2, chapters 6,7,& 9 on Functions, Arrays, & Pointers 1. Create a one...
C++ programming test 2, chapters 6,7,& 9 on Functions, Arrays, & Pointers 1. Create a one dimensional array, Ages, which will hold 4 ages. Each age is an int.        b. Write a for loop and print, in reverse order the 4 values stored in memory assuming that the ages in the previous question have already been entered with a space between each value. Use subscript notation.                                     short cnt; c. Do the same as above, but use pointer...
Inheritance - Polymorphism One advantage of using subclasses is the ability to use polymorphism. The idea...
Inheritance - Polymorphism One advantage of using subclasses is the ability to use polymorphism. The idea behind polymorphism is that several different types of objects can have the same methods, and be treated in the same way. For example, have a look at the code we’ve included for this problem. We’ve defined Shape as an abstract base class. It doesn’t provide any functionality by itself, but it does supply an interface (in the form of .area() and .vertices() methods) which...
Don't use vectors use pointers ,classes & objects, functions and loop etc only C++ PROGRAM Following...
Don't use vectors use pointers ,classes & objects, functions and loop etc only C++ PROGRAM Following is a partial implementation of Set class. You are required to enhance and implement the following missing functions from the implementation: A) UNION B) reset C) intersection D) difference A SAMPLE driver program : int a1[] = {10,5,7,3,9}; Set s1(5); s1.insert(a1,5); s1.print("s1"); int a2[] = {2,9,6}; Set s2(3); s2.insert(a2,3); s2.print("s2"); Set s3 = s1.unionset(s2); Set s4 = s1.intersection(s2); Set s5 = s1.difference(s2); s3.print("s3"); s4.print("s4");...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT