0% found this document useful (0 votes)
3 views19 pages

Module 3 CPP

The document provides an overview of operator overloading in C++, explaining how unary operators can be redefined for user-defined types. It covers the syntax for overloading, examples of unary and arithmetic operator overloading, and the manipulation of strings using operator overloading. Additionally, it explains arrays and string classes in C++, highlighting their definitions, syntax, and advantages.

Uploaded by

amjadshamnad
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views19 pages

Module 3 CPP

The document provides an overview of operator overloading in C++, explaining how unary operators can be redefined for user-defined types. It covers the syntax for overloading, examples of unary and arithmetic operator overloading, and the manipulation of strings using operator overloading. Additionally, it explains arrays and string classes in C++, highlighting their definitions, syntax, and advantages.

Uploaded by

amjadshamnad
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

MODULE 3 | OPERATOR OVERLOADING

Operator Overloading is a feature in object-oriented programming that allows


programmers to redefine the way operators (such as +, -, *, ==, etc.) work for
user-defined data types (classes or structures).

Overloading Unary Operators (C++)

In C++, operator overloading lets you redefine how operators work for user-defined types (classes/structs).
Unary operators operate on a single operand.

Common Unary Operators:

- -(unary minus)

• ++ (increment)

• -- (decrement)

• ! (logical NOT)

• ~ (bitwise NOT)

Syntax:

return_type operator op();

Operator Keyword in C++

The operator keyword is used to declare and define an operator function that gives a new meaning to an
existing operator when it is used with objects of a class.

Syntax

C++

return_type operator operator_symbol(arguments)

// function body

}
Example

C++

class Number {

private:

int value;

public:

Number(int v) {

value = v;

Number operator +(Number n) {

return Number(value + [Link]);

void display() {

cout << value;

};

In the statement:

C++

Number operator +(Number n)

•operator → keyword used for operator overloading.

•+ → operator being overloaded.

•Number → return type.

Common Operator Keywords

C++

operator +
operator -

operator *

operator /

operator ==

operator !=

operator ++

operator --

operator =

Arguments and Return Values

1. Member Function

Arguments: No explicit arguments.

Return value: Usually returns an object of the class.

Syntax:

C++

return_type operator op();

2. Friend Function

Arguments: One object of the class.

Return value: Usually returns an object of the class.

Syntax:

C++

friend return_type operator op(ClassName);

Syntax of Unary Operator Overloading

Member Function
C++

class ClassName {

public:

return_type operator op();

};

Friend Function

C++

class ClassName {

friend return_type operator op(ClassName);

};

Example Program (Unary Minus Operator)

C++

#include <iostream>

using namespace std;

class Number {

int num;

public:

Number(int n) {

num = n;

void display() {

cout << "Number = " << num << endl;

}
// Overloading unary minus

Number operator-() {

return Number(-num);

};

int main() {

Number n1(10);

Number n2 = -n1;

[Link]();

[Link]();

return 0;

Output

Number = 10

Number = -10

Working of the Program

•The class Number contains an integer data member num.

•The constructor initializes the value of num.

•The unary minus operator operator-() is overloaded.

•When -n1 is executed, the overloaded function is called automatically.

•It creates and returns a new object with the negated value.

•The original object remains unchanged.

Key Points

•Unary operators act on a single operand.


•The operator keyword is used for operator overloading.

•Member function: No explicit argument, usually returns a class object.

•Friend function: One class object as an argument, usually returns a class object.

•Operator overloading improves the readability and usability of user-defined types.

TYPES OF OVERLOADING

1. Overloading Unary Operators

Definition:

A unary operator works on a single operand. Common unary operators are -, ++, --, !, and ~.

Syntax:

return_type operator op();

Example Program (Unary Minus)

C++

#include<iostream>

using namespace std;

class Number {

int x;

public:

Number(int a) {

x = a;

Number operator-() {

return Number(-x);

void display() {

cout << x << endl;

};
int main() {

Number n1(10);

Number n2 = -n1;

[Link]();

return 0;

Output

-10

3. Arithmetic Operator Overloading

Definition:

Arithmetic operator overloading allows arithmetic operators such as +, -, *, and / to work with class objects.

Syntax:

return_type operator+(ClassName obj);

return_type operator-(ClassName obj);

return_type operator*(ClassName obj);

return_type operator/(ClassName obj);

Example Program (Multiplication)

#include<iostream>

using namespace std;

class Multiply {

int x;

public:

Multiply(int a=1) {

x = a;

}
Multiply operator*(Multiply m) {

return Multiply(x * m.x);

void display() {

cout << x;

};

int main() {

Multiply m1(5), m2(4);

Multiply m3;

m3 = m1 * m2;

[Link]();

return 0;

Output: 20

4. Manipulation of Strings Using Operator Overloading

Definition:

String manipulation using operator overloading allows operations such as string concatenation by overloading
operators like +.

Syntax:

return_type operator+(ClassName obj);

Example Program

Cpp
#include<iostream>

#include<cstring>

using namespace std;

class String {

char str[100];

public:

String() {

str[0] = '\0';

String(char s[]) {

strcpy(str, s);

String operator+(String s) {

String temp;

strcpy([Link], str);

strcat([Link], [Link]);

return temp;

void display() {

cout << str;

};

int main() {

String s1("Good ");

String s2("Morning");

String s3;

s3 = s1 + s2;

[Link]();

return 0;

}
Output:Good Morning

Advantages of Operator Overloading

•Improves code readability.

•Makes user-defined objects behave like built-in data types.

•Simplifies complex operations.

•Enhances program reusability.

Arrays in C++

Definition

An array is a collection of elements of the same data type stored in contiguous memory locations and referred
to by a single name.

Key Points

•Stores multiple values of the same type.

•Elements are accessed using an index.

•Index starts from 0.

Syntax

C++

data_type array_name[size];

Example

C++

int marks[5];

This declaration creates an array named marks that can store 5 integer values.
Strings in C++

Definition

A string is a collection of characters stored in a character array and terminated by a null character ('\0').

Key Points

•Used to store text such as names and addresses.

•In Turbo C++, strings are represented using character arrays.

•Every string ends with the null character '\0'.

Syntax

C++

char string_name[size];

Example

C++

char name[20];

Simple Turbo C++ Program

C++

#include<iostream.h>

#include<conio.h>

void main()

clrscr();

char name[20];

cout<<"Enter your name: ";

cin>>name;

cout<<"Name = "<<name;
getch();

Output

Plain text

Enter your name: Farhan

Name = Farhan

One-Dimensional Array

Definition

One-dimensional array: A one-dimensional array is a collection of similar data elements stored in contiguous
memory locations and accessed using a single subscript (index).

Syntax

C++

data_type array_name[size];

Example

C++

int marks[5];

Turbo C++ Program

C++

#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int a[5]={10,20,30,40,50};

for(int i=0;i<5;i++)
cout<<a[i]<<" ";

getch();

Output:

10 20 30 40 50

Multidimensional Array

Definition

A multidimensional array is an array that contains more than one dimension. The most common type is the
two-dimensional array, which stores data in rows and columns.

Syntax

C++

data_type array_name[row_size][column_size];

Example

C++

int marks[2][3];

Turbo C++ Program

C++

#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int a[2][3]={{1,2,3},{4,5,6}};

for(int i=0;i<2;i++)
{

for(int j=0;j<3;j++)

cout<<a[i][j]<<" ";

cout<<endl;

getch();

Output:

123

456

Exam Definition (2 Marks)

Multidimensional array: A multidimensional array is an array with two or more dimensions, where each element
is accessed using multiple indices. It is commonly used to represent tables, matrices, and grids.

Difference

One-Dimensional Array

•Uses one index

•Stores data in a single list

Example: int a[5];

Multidimensional Array

•Uses two or more indices

•Stores data in rows and columns

Example: int a[2][3];

Strings and String Classes – Array and String Manipulation in C++

Definition:

In C++, a string is a sequence of characters used to store and manipulate text.

There are two ways to represent strings:


A. Character Array (C-style string) – An array of characters terminated by the null character ('\0').

B. String Class – Provided by the <string> library, offering built-in functions for easy string manipulation.

Character Array (C-Style String)-

A character array is a collection of characters stored consecutively in memory and terminated by a null
character ('\0').

Syntax:

char string_name[size];

or

char string_name[] = "Hello";

Example Program

C++:

#include<iostream>

using namespace std;

int main() {

char str[] = "Hello World";

cout << str;

return 0;

Output:

Hello World

String Class

Definition:

The string class is a built-in class in C++ that provides functions for storing and manipulating strings
dynamically.

Syntax:

#include<string>
string variable_name;

Example Program

C++

#include<iostream>

#include<string>

using namespace std;

int main() {

string str = "Hello World";

cout << str;

return 0;

Output:

Hello World

String Manipulation:

String manipulation involves operations such as:

•Input and output

•Concatenation

•Finding length

•Comparison

•Copying

•Extracting substrings

A. Concatenation

Syntax:

string3 = string1 + string2;


Example:

#include<iostream>

#include<string>

using namespace std;

int main() {

string s1 = "Good ";

string s2 = "Morning";

cout << s1 + s2;

return 0;

Output:

Good Morning

B. Finding Length:

Syntax:

[Link]();

Example

#include<iostream>

#include<string>

using namespace std;

int main() {

string str = "Computer";

cout << [Link]();

return 0;

Output:
8

C. String Comparison:

Syntax:

[Link](str2);

Example:

#include<iostream>

#include<string>

using namespace std;

int main() {

string s1 = "Apple";

string s2 = "Apple";

if(s1 == s2) {

cout << "Equal";

else

cout << "Not Equal";

return 0;

Output:

Equal

D. Copying a String

Syntax:

string2 = string1;
Example

#include<iostream>

#include<string>

using namespace std;

int main() {

string s1 = "C++";

string s2;

s2 = s1;

cout << s2;

return 0;

Output: C++

Advantages of String Class

•Dynamic size.

•Easy to manipulate.

•Built-in functions for common operations.

•Safer than character arrays because it manages memory automatically.

You might also like