Skip to main content

Posts

Showing posts with the label function overloading

List and explain different types of operators in C++. Explain function overloading with a suitable program.

 In C++, operators are special symbols used to perform operations on variables and values. There are different types of operators in C++, which are as follows: Arithmetic operators: These operators are used to perform arithmetic operations such as addition, subtraction, multiplication, division, and modulus. For example, +, -, *, /, %. Assignment operators: These operators are used to assign a value to a variable. For example, =, +=, -=, *=, /=, %=. Comparison operators: These operators are used to compare two values and return true or false. For example, ==, !=, <, >, <=, >=. Logical operators: These operators are used to perform logical operations such as AND, OR, and NOT. For example, &&, ||, !. Bitwise operators: These operators are used to perform bitwise operations such as AND, OR, XOR, left shift, and right shift. For example, &, |, ^, <<, >>. Unary operators: These operators are used to perform operations on a single operand. For example,...

Discuss function overloading with example.

 Function overloading is a feature in C++ that allows us to have multiple functions with the same name, but with different parameters. The compiler decides which function to call based on the number, type, and order of the parameters passed. Here's an example to demonstrate function overloading: #include <iostream> using namespace std; // Function to calculate area of a square int area(int side) {     return side * side; } // Function to calculate area of a rectangle int area(int length, int breadth) {     return length * breadth; } // Function to calculate area of a circle float area(float radius) {     return 3.14 * radius * radius; } int main() {     int s = 5, l = 10, b = 5;     float r = 2.5;     cout << "Area of square with side " << s << " is " << area(s) << endl;     cout << "Area of rectangle with length " << l << " and breadth " << b << "...

Define function prototype. How function overloading is achieved? Mention advantages of using inline function.

 A function prototype is a declaration of a function that specifies the function's name, return type, and parameter types. It is used to inform the compiler about the existence of a function before it is called in the program. Function overloading is achieved by defining multiple functions with the same name but different parameter lists. When a function call is made with a particular name, the compiler determines which function to call based on the number, types, and order of the arguments passed. This allows a programmer to define functions that perform similar operations but with different data types or number of arguments. The compiler distinguishes between the different functions based on their signatures. For example, consider the following functions: void add(int a, int b); void add(double a, double b); Both functions are named "add", but one takes two integers as arguments while the other takes two doubles. When the program calls the "add" function, the ...