meena
meena
meena
Functional overloading:
Example:
#include <iostream>
int main(void) {
Cal C; // class object declaration.
cout<<C.add(10, 20)<<endl;
cout<<C.add(12, 20, 23);
return 0;
}
o The values passed in the default arguments are not constant. These
values can be overwritten if the value is passed to the function. If
not, the previously declared value retains.
o During the calling of function, the values are copied from left to
right.
o All the values that will be given default value will be on the right.
Example:
#include<iostream>
int sum(int x, int y, int z=0, int w=0) // Here there are two values in
the default arguments
{ // Both z and w are initialised to zero
return (x + y + z + w); // return sum of all parameter values
}
int main()
{
cout << sum(10, 15) << endl; // x = 10, y = 15, z = 0, w = 0
cout << sum(10, 15, 25) << endl; // x = 10, y = 15, z = 25, w =
0
cout << sum(10, 15, 25, 30) << endl; // x = 10, y = 15, z = 25, w
= 30
return 0;
}
Constructor overloading :
Constructor is a member function of a class that is used to initialize
the objects of the class. Constructors do not have any return type and are
automatically called when the object is created.
Characteristics of constructors:
Types of constructors
There are three types of constructors -
o Default constructor :
A default constructor is one that does not have function
parameters. It is used to initialize data members with a value. The
default constructor is called when the object is created.
Example:
#include <iostream>
class construct // create a class construct
{
public:
int a, b; // initialise two data members
construct() // this is how default constructor is created
{
// We can also assign both values to zero
a = 10; // intialise a with some value
b = 20; // initialise b with some value
}
};
int main()
{
construct c; // creating an object of construct calls defualt
onstructor
cout<< "a:" <<c.a<<endl
<< "b:" <<c.b; // print a and b
return 1;
}
o Parameterized constructor :
Example:
#include <iostream>
o Copy constructor :
Syntax:
class_name(constclassname&old_object).
Example:
#include <iostream>
class Point // create point class
{
private:
int x, y; // data members of the class
public:
Point(int x1, int y1)
{
x = x1;
y = y1;
} // parameterised constructor
// Copy constructor
Point(const Point& p1) // initialisation according to syntax
{
x = p1.x;
y = p1.y;
}
intgetX() { return x; } // return value of x
intgetY() { return y; } // return valur of y
};
int main()
{
Point p1(10, 15); // call parameterised constructor
Point p2 = p1; // call Copy constructor
// use getter and setter to print x and y
cout<< "p1.x = " << p1.getX() << ", p1.y = " << p1.getY();
cout<< "\np2.x = " << p2.getX() << ", p2.y = " << p2.getY();
return 0;
}
Operator Overloading:
Operator overloading is a compile-time polymorphism in which
the operator is overloaded to provide the special meaning to the
user-defined data type. Operator overloading is used to overload or
redefines most of the operators available in C++. It is used to
perform the operation on the user-defined data type. For example,
C++ provides the ability to add the variables of the user-defined
data type that is applied to the built-in data types.
o Sizeof
o member selector(.)
o ternary operator(?:)
Example:
#include <iostream>
class Test
{
private:
int num;
public:
Test(): num(8){}
void operator ++()
{
num = num+2;
}
void Print() {
cout<<"The Count is: "<<num;
}
};
int main()
{
Test tt;
++tt; // calling of a function "void operator ++()"
tt.Print();
return 0;
}
Here, 'op' represents the Operator you want to overload (e.g., '+', '-', '*',
'/'), and 'parameters' represent the Operator's operands. The 'return_type'
specifies the type of value the Operator should return.
Example:
class Vector {
Public:
Vector(int x, int y) : x(x), y(y) {}
The function is declared a 'friend' of the class with this syntax, enabling it
to access its private members. The remaining portions of the grammar
resemble member function overloading.
Let's now talk about the benefits and applications of Operator overloading
with buddy functions:
Benefits of Overloading Operators with Friend Functions:
o Symmetry:
o Non-Member Function:
o Global Functions:
o Enhanced Readability:
o Flexibility:
When choosing which operators to overload, friend functions
offer flexibility. Without being constrained by the member functions
of the class, you can decide to overload only the operators that
make sense for your class.
o Mathematical Operations:
o Cross-Class actions:
o Declare the operator function inside the class as a friend. The friend
function can now access the class's private members.
Example :
Adding a Friend Function to the '+' Operator:
#include <iostream>
class Complex {
Public:
Complex(double real, double image) : real(real), imag(imag) {}
// Declare the '+' operator as a friend
friend Complex operator+(const Complex& c1, const Complex c2);
// Display the complex number
void display() const {
std::cout << real << " + " << imag << "i" << std::endl;
}
Private:
double real, imag;
};
// Define the '+' operator using a friend function
Complex operator+(const Complex& c1, const Complex& c2)
{
return Complex(c1.real + c2.real, c1.imag + c2.imag);
}
int main() {
Complex c1(2.0, 3.0);
Complex c2(1.5, 2.5);
// Use the overloaded '+' operator
Complex sum = c1 + c2;
std::cout << "Sum: ";
sum.display();
return 0;
}
[ ] operator overloading:
The subscript operator [] is normally used to access array
elements. This operator can be overloaded to enhance the existing
functionality of C++ arrays.
// operator
#include <iostream>
int main()
{
return 0;
( )operator overloading:
The function call operator () can be overloaded for objects of
class type. When you overload ( ), you are not creating a new way
to call a function. Rather, you are creating an operator function that
can be passed an arbitrary number of parameters.
Example:
// above concepts
#include <bits/stdc++.h>
#include <iostream>
#define N 3
#define M 3
// Matrix Class
class Matrix {
private:
int arr[N][M];
public:
istream&, Matrix&);
ostream&, Matrix&);
};
// ">>" operator
Matrix& m)
int x;
// Overloading operator()
// to take input
}
}
return cin;
// "<<" operator
Matrix& m)
// Overloading operator() to
return cout;
{
return arr[i][j];
// Driver Code
int main()
Matrix m;
cin >> m;
cout << m;
return 0;
->Operator overloading:
The class member access operator (->) can be overloaded but it is
bit trickier. It is defined to give a class type a "pointer-like" behavior. The
operator -> must be a member function. If used, its return type must be a
pointer or an object of a class to which you can apply.
class Ptr {
//...
X * operator->();
};
Objects of class Ptr can be used to access members of class X in a very
similar manner to the way pointers are used.
void f(Ptr p ) {
p->m = 10 ; // (p.operator->())->m = 10
}
Syntax:
class ClassName
{
// ...
// operator function
ClassName* operator->()
{
// operator function body
// ...
Example:
#include <iostream>
class Double
{
public:
double d; // internal variable
void main()
{
// access operator overload by pointer ->
Double D; // instance of class D
double x;
D.d = 3.85;
This section will discuss the type casting of the variables in the C++
programming language. Type casting refers to the conversion of one data
type to another in a program. Typecasting can be done in two ways:
automatically by the compiler and manually by the programmer or user.
Type Casting is also known as Type Conversion.
int num = 5;
float x;
x = float(num);
x = 5.0
Example:
#include <iostream>
int main ()
{
short x = 200;
int y;
y = x;
cout << " Implicit Type Casting " << endl;
cout << " The value of x: " << x << endl;
cout << " The value of y: " << y << endl;
(type) expression;
type:
It represents the user-defined data that converts the given
expression.
expression:
int num;
num = (int) 4.534; // cast into int data type
cout << num;
When the above statements are executed, the floating-point value will be
cast into an integer data type using the cast () operator. And the float
value is assigned to an integer num that truncates the decimal portion
and displays only 4 as the integer value.
Example:
#include <iostream>
int main ()
{
// declaration of the variables
int a, b;
float res;
a = 21;
b = 5;
cout << " Implicit Type Casting: " << endl;
cout << " Result: " << a / b << endl; // it loses some information
return 0;
}