function overriding in c++ code example

Example 1: c++ function overload

/*A function over load in c++ is when you take a function with the same definition but change the input variables.
  This can include multiple functions with the same name see below
  */
#include <iostream>
using namespace std;

int function1(int var1){//example of single variable
	//do somthing
}
int function1(int var1,int var2){//of overload
	//do somthing	
}
int function1(int var1,string var3){//of overload
	//do somthing	
}

int main(){
 
  cout << "Hello World" << endl;
  function1(4);
  function1(3,-90);
  function1(34,"it works");//these should all work even tho they have different input variables
  return 0;
}

Example 2: function overriding in oop c++

#include<iostream>
using namespace std;
class Base
{
 public:
 virtual void show() // virtual function
 {
  cout << "Base class";
 }
};
class Derived:public Base
{
 public:
 void show()
 {
  cout << "Derived Class";
 }
};

int main()
{
 Base* b;       //Base class pointer
 Derived d;     //Derived class object
 b = &d;	// passing derived class address into base class pointer	
 b->show();     //Late Binding Occurs
}

Tags:

Cpp Example