C++ Function Overloading:

C++ Function Overloading:

Share It With Your Firends:

In some programming languages, like c++ function overloading or method overloading isย the ability to create multiple functions of the same name with different implementations.

Rules of Function Overloading in C++

  • These functions have different parameter type. sum(int a, int b) sum(double a, double b)
  • These functions have a different number of parameters. sum(int a, int b) sum(int a, int b, int c)
  • These functions have a different sequence of parameters. sum(int a, double b) sum(double a, int b)
#include<iostream>
using namespace std;

int sum(float a, int b){
    cout<<"Using function with 2 arguments"<<endl;
    return a+b;
}

int sum(int a, int b, int c){
    cout<<"Using function with 3 arguments"<<endl;
    return a+b+c;
}

// Calculate the volume of a cylinder
int volume(double r, int h){
    return(3.14 * r *r *h);
}

// Calculate the volume of a cube
int volume(int a){
    return (a * a * a);
}

// Rectangular box
int volume (int l, int b, int h){
    return (l*b*h);
}

int main(){
    cout<<"The sum of 3 and 6 is "<<sum(2,4)<<endl;
    cout<<"The sum of 3, 7 and 6 is "<<sum(3, 7, 8)<<endl;
    cout<<"The volume of cuboid of 3, 7 and 6 is "<<volume(3, 7, 5)<<endl;
    cout<<"The volume of cylinder of radius 3 and height 6 is "<<volume(2, 6)<<endl;
    cout<<"The volume of cube of side 3 is "<<volume(3)<<endl;
    return 0;
}

Output:

The sum of 3 and 6 is Using function with 2 arguments
6
The sum of 3, 7 and 6 is Using function with 3 arguments
18
The volume of cuboid of 3, 7 and 6 is 105
The volume of cylinder of radius 3 and height 6 is 75
The volume of cube of side 3 is 27

Share It With Your Friends

Leave a Reply

Recent Posts

  • All Post
  • A.I.
  • AI
  • c
  • c++
  • computer
  • cryptocurrency
  • database
  • digital marketing
  • finance
  • hacking
  • HTML
  • java
  • Marketing
  • network
  • other
  • programming
  • python
  • react
  • social
  • Tools
  • Uncategorized
  • web devlopment
    •   Back
    • drawing In Python
DIY AI Voice Assistant In Python

September 8, 2023

Build your own AI assistant with Python! Learn to recognize voice commands, generate responses, and more. Create your virtual companion…

share it

Recent Posts

  • All Post
  • A.I.
  • AI
  • c
  • c++
  • computer
  • cryptocurrency
  • database
  • digital marketing
  • finance
  • hacking
  • HTML
  • java
  • Marketing
  • network
  • other
  • programming
  • python
  • react
  • social
  • Tools
  • Uncategorized
  • web devlopment
    •   Back
    • drawing In Python

Additional Content

CodeWithAM

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus luctus.

Products

Automated Chatbot

Data Security

Virtual Reality

Communication

Support

Services

FAQ's

Privacy Policy

Terms & Condition

Team

Contact Us

Company

About Us

Services

Features

Our Pricing

Latest News

ยฉ 2023 Codewitham