Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

C++ notes

Rating
-
Sold
-
Pages
12
Uploaded on
24-05-2024
Written in
2023/2024

Hear is the best and easy notes for preparation of c++

Institution
Course

Content preview

Operator Overloading

Operator overloading:-

The process of making an operator to different behavior in different instances is
known as the operator overloading.
Ex: Consider the operation of addition for two nos. The operation will generate
sum. If you consider operands are strings then it would produce third string by
concatenation.
The mechanism of giving a special meaning to an operator is known as operator
overloading. lost
When operator is overloaded, its original meaning is not.
Operator function must be either member function or friend function.
If we declare operator function is member function of class then member function
has no argument for unary operators and binary operators.

Defining an operator overloading:-

Syntax:

Return type class name:: operator op(argument list)
{
Function body
}

Operator:-is a keyword to precede by any operator.
Op: - the op is the operator being overloaded.

Steps for process of operator overloading:-

1. Create a class that defines the data type that is to be used in the overloading
operation.
2. Declare the operator function :- operator op () in public part of the class
3. Implementation of the operator function
Outside the class
Inside the class
op x or x op

Unary operator overloading:-

Write a C++ program for unary – operator overloading.

#include<iostream.h>
#include<conio.h>
class space
{

, int x, y, z;
public:
void getdata (int a, int b, int c);
void display ();
void operator – ();
}
void space :: getdata (int a, int b, int c)
{
x=a;
y=b;
z=c;
}
void space :: display()
{
cout<<x<<endl;
cout<<y<<endl;
cout<<z<<endl;
}
void space ::operator –()
{
x= - x;
y= - y;
z= - z;
}
void main()
{
Space s;
s.getdata (5,-10, 15);
s.display();
-s;
s.display();
getch ();
}

Write a C++ program for unary ++ operator overloading.

#include<iostream.h>
#include<conio.h>
class increment
{
public:
int data;
increment()
{
data=1;

Written for

Institution
Course

Document information

Uploaded on
May 24, 2024
Number of pages
12
Written in
2023/2024
Type
Class notes
Professor(s)
Kxaptl
Contains
All classes

Subjects

$19.29
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
kakshapatel

Get to know the seller

Seller avatar
kakshapatel Ksv university
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
1 year
Number of followers
0
Documents
1
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions