Unit - 2 C++ - Final
Unit - 2 C++ - Final
Overloading in C++:
o Function overloading
o Operator overloading
Example:
1. #include <iostream>
2. using namespace std;
3. class Cal {
4. public:
5. static int add(int a,int b)
6. { return a + b; }
7. static int add(int a, int b, int c)
8. { return a + b + c; }
9. };
10. int main(void) {
11. Cal C; // class object decla
ration.
12. cout<<C.add(10, 20)<<endl;
13. cout<<C.add(12, 20, 23);
14. return 0;
15. }
Default Arguments:
Example:
#include <iostream>
// Function with two parameters, one with a default value
int add(int a, int b = 0)
{
return a + b;
}
// Function with three parameters, two with default values
int add(int a, int b = 0, int c = 0)
{
return a + b + c;
}
int main()
{
cout << add(2) << endl; // Calls the 1st function with
default value
cout << add(2, 3) <<endl; // Calls the 1st function specifying
'b' value
cout << add(1, 2, 3) ; // Calls the 2 nd function, with values for
'b' and 'c'
return 0;
}
Constructor Overloading in C++
Overloaded constructors have the same name of the class but
with different number of arguments.
Depending upon the number and type of arguments passed,
the corresponding constructor is called.
Example:
// C++ program to demonstrate constructor overloading
#include <iostream>
using namespace std;
class Person {
private:
int age;
public:
Person() // 1. Constructor with no arguments
{ age = 20; }
Person(int a) // 2. Constructor with one argument
{ age = a; }
int getAge() {
return age;
}
};
int main() {
Person person1, person2(45);
cout << "Person1 Age = " << person1.getAge() << endl;
cout << "Person2 Age = " << person2.getAge() << endl;
return 0;
}
Operator Overloading:
Operator overloading is one of the best features of C++.
By overloading the operators, we can give additional meaning
to the operators.
The mechanism of giving special meaning to an operator is
known as operator overloading.
It has the ability to redefine the behaviour of Operators such
as
+, -, *, /, =, ==, <, etc.,
Syntax for C++ Operator Overloading
class className
{
... .. ...
public
returnType operator symbol (arguments)
{
... .. ...
}
... .. ...
};
Conditional [?:],
Size of Operator (size of),
scope(::),
Member selector(.),
member pointer selector(.*) and
the casting operators.
Unary operators:
Operators which work on a single operand are called unary
operators.
Examples: Increment operators(++), Decrement
operators(–),unary minus operator(-), Logical not operator(!)
etc…
Binary operators:
Operators which works on Two operands are called binary
operator.
}
};
int main()
{
UnaryOverload ob;
ob.in();
ob++;
cout<<"\n\n After Incrementing : ";
ob.out();
ob--;
ob--;
cout<<"\n\n After Decrementing : ";
ob.out();
return 0;
}
Output
Enter the time:
5
56
After Incrementing:
Time is 6hr 57 mins
After Decrementing:
Time is 4hr 55 min
#include <iostream>
class Vector {
private:
int x, y;
public:
Vector(int x, int y) : x(x), y(y) {}
// Overloading the + operator for vector addition
Vector operator+(const Vector& other) const {
return Vector(x + other.x, y + other.y);
}
// Method to display vector coordinates
void display() const {
std::cout << "x: " << x << ", y: " << y << std::endl;
}
};
int main() {
Vector v1(2, 3);
Vector v2(1, -1);
Vector result = v1 + v2; // Using the overloaded + operator
std::cout << "Resultant vector: ";
result.display();
return 0;
}
ret-type class-name::operator#(arg-list)
{ // operations }
The Friend functions are not a member of the class and hence they
do not have 'this' pointer.
int main ()
{
Complex C1(5, 3), C2(10, 5), C3;
C1.Display();
cout << " + ";
C2.Display();
cout << " = ";
C3 = C1 + C2;
C3.Display();
}
#include <iostream>
using namespace std;
#include <iostream>
using namespace std;
class Distance {
private:
int feet; // 0 to infinite
int inches; // 0 to 12
public:
// required constructors
Distance() {
feet = 0;
inches = 0;
}
Distance(int f, int i) {
feet = f;
inches = i;
}
// overload function call
Distance operator()(int a, int b, int c) {
Distance D;
D.feet = a + c + 10; // just a random calculation
D.inches = b + c + 100 ;
return D;
}
void displayDistance() {
cout << "F: " << feet << " I:" << inches << endl;
}
};
int main() {
Distance D1(11, 10), D2;
cout << "First Distance : ";
D1.displayDistance();
D2 = D1(10, 10, 10); // invoke operator()
cout << "Second Distance :";
D2.displayDistance();
return 0;
}
ptr->memberFunction();
#include <iostream>
using namespace std;
int main() {
int i, j;
j = 10;
i = (j++, j+100, 999+j);
cout << i;
return 0;
}
1010
---------------------------------------------------
Conversion Functions
1. Implicit Conversion
Float b=9.5;
Int x;
X=b;
Cout <<x;
Output: 9
Explicit Conversion :
When the user manually changes data from one type to another
type is known as explicit conversion. It is also known as Type
Casting.
Example :
Int a=15;
Double x:
X= double(a);
Cout<<x;
Output: 15.000
There are three major ways in which we can use explicit conversion
in C++. They are: