Function Overloading Q) Addition of Number Function Overloading With Different Types of Arguments

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Function Overloading Function overloading with different types

of arguments.
Q) Addition of number
#include<iostream>
#include <iostream> using namespace std;
using namespace std; int mul(int,int);
class Cal { float mul(float,int);
public:
static int add(int a,int b){
return a + b; int mul(int a,int b)
} {
static int add(int a, int b, int c) return a*b;
{ }
return a + b + c; float mul(double x, int y)
} {
}; return x*y;
int main(void) { }
Cal C; // class object dec int main()
laration. {
cout<<C.add(10, 20)<<endl; int r1 = mul(6,7);
cout<<C.add(12, 20, 23); float r2 = mul(0.2,3);
return 0; std::cout << "r1 is : " <<r1<< std::endl;
} std::cout <<"r2 is : " <<r2<< std::endl;
return 0;
Q) Function with different data types }
#include<iostream> Q) Runtime Polymorphism
using namespace std;
void fun(int); #include <iostream>
void fun(float); using namespace std;
void fun(int i) class A {
{ public:
std::cout << "Value of i is : " <<i<< std::e void disp(){
ndl; cout<<"Super Class Function"<<endl;
} }
void fun(float j) };
{ class B: public A{
std::cout << "Value of j is : " <<j<< std::e public:
ndl; void disp(){
} cout<<"Sub Class Function";
int main() }
{ };
fun(12); int main() {
fun(1.2); //Parent class object
return 0; A obj;
} obj.disp();
//Child class object }
B obj2; };
obj2.disp(); int main()
return 0; {
} A *a; //pointer of base class
B b; //object of derived class
Function overriding a = &b;
a->display(); //Late Binding occurs
#include <iostream> }
using namespace std;
class BaseClass { Data Abstraction
public:
void disp(){ #include <iostream>
cout<<"Function of Parent Class"; using namespace std;
} class Sum
}; {
class DerivedClass: public BaseClass{ private: int x, y, z; // private variables
public: public:
void disp() { void add()
cout<<"Function of Child Class"; {
} cout<<"Enter two numbers: ";
}; cin>>x>>y;
int main() { z= x+y;
DerivedClass obj; cout<<"Sum of two number is: "<<z<<endl;
obj.disp();
return 0; }
} };
int main()
Virtual Function {
Sum sm;
#include <iostream> sm.add();
using namespace std; return 0;
class A }
{
public:
virtual void display()
{
cout << "Base class is invoked"<<endl;
}
};
class B:public A
{
public:
void display()
{
cout << "Derived Class is invoked"<<endl;

You might also like