Skip to main content

C++: 8. Polymorphism

Polymorphism is an important concept of object-oriented programming. It simply means more than one form. That is, the same entity (function or operator) behaves differently in different scenarios.

Function Overloading in C++
In C++, two or more functions can have the same name if the number and/or type of parameters are different, this is called function overloading.

Example:- 
#include <iostream>
using namespace std;
int add(int a, int b) {
    return a+b;
}
int add(int a, int b, int c) {
    return a+b+c;
}
double add(double a, double b) {
    return a+b;
}
int main() {
  int x = 3, y = 7, z = 12;
  double n1 = 4.56, n2 = 13.479;
  cout<<"x+y = "<<add(x,y)<<endl;
  cout<<"x+y+z = "<<add(x,y,z)<<endl;
  cout<<"n1+n2 = "<<add(n1,n2);
  return 0;
}
Output:- 
x+y = 10
x+y+z = 22
n1+n2 = 18.039


Function Overriding in C++
When a member function of a base class is redefined in its derived class with the same parameters and return type, it is called function overriding in C++. The base class function is said to be overridden.

Example:- 
#include <iostream>
using namespace std;
class base {
    public:
    virtual void display() {
        cout<<"Function of base class"<<endl;
    }
};
class derived : public base {
    public:
    void display() {
        cout<<"Function of derived class"<<endl;
    }
};
int main() {
  derived d1;
  d1.display();
  return 0;
}
Output:- 
Function of derived class

Virtual Function in C++
A virtual function is a function defined in the base class with virtual keyword. The purpose of virtual function is to ensure that the function is overridden.

Example:- 
#include <iostream>
using namespace std;
class base {
    public:
    void display() {
        cout<<"Function of base class"<<endl;
    }
};
class derived : public base {
    public:
    void display() {
        cout<<"Function of derived class"<<endl;
    }
};
int main() {
  derived d;
  base *b = &d;   //base class pointer
  b->display();   //base class function executes
  return 0;
}
Output:- 
Function of base class

Comments

Popular posts from this blog

WP: 8. Dynamically show post taxonomies and their data in tab format [Solved]

    Today I ran into a problem, I need to dynamically show post taxonomies and their data in tab format and found this solution. And nonetheless, It's better to fix the thing with some bunch of code rather than uploading one more bulky plugin on your website that will end up making your site heavy. Just follow the steps below and avoid heading up into any malicious or bulky plugin for this particular problem from now on forever. Step 1: Get the taxonomies terms first <?php     $terms = get_terms( array(       'taxonomy' => 'off_plan_categories',       'hide_empty' => true,     ) );     // echo '<pre>';     // print_r($terms);     // die; ?> Step 2:  Execute loop first for tab navigation <nav id="offPlanPropertyTabs" class="offplan-property-type-tabs nav nav-tabs"> <?php foreach($terms as $key => $term): ?>   <button class="nav-item nav-link <?php if($key==0): ?> active <?php en

JS: Cheatsheet

   Datatypes Difference Between Var, Let and Const Functions and its types Asynchronous operations in JS

C++: 9. File Handling

Files are used to store data in a storage device permanently. File handling provides a mechanism to store the output of a program in a file and perform various operations on it. Example of opening/creating a file    #include<iostream> #include<fstream> using namespace std; int main() { fstream new_file; new_file.open("new_file",ios::out); if(!new_file) { cout<<"File creation failed"; } else { cout<<"New file created"; new_file.close(); // Step 4: Closing file } return 0; } Writing to a File #include <iostream> #include <fstream> using namespace std; int main() { fstream new_file; new_file.open("new_file_write.txt",ios::out); if(!new_file) { cout<<"File creation failed"; } else { cout<<"New file created"; new_file<<"Learning File handling"; //Writing to file new_file.close();