Topic: Overloading << and >> operators
Presented by:
Ayanabh Saikia R. No: 23BSA10205
Ayanabh Saikia
R. No: 23BSA10205
What is Operator Overloading?
Operator overloading in C++ is a feature that allows you to redefine the way operators work for user-
defined types (such as classes and structures). It enables operators to be used with objects in a manner
similar to built-in types, thus providing intuitive and readable code.
Which Operators can be overloaded?
Almost all operators can be overloaded, with some exceptions such as:
:: (Scope resolution)
. (Member access)
.* (Pointer to member)
?: (Ternary conditional)
In this presentation we will be overloading the >> and << operators in a class declaring an object for specifying distance.
Ayanabh Saikia
R. No: 23BSA10205
Implementation:
Output:
The code first declares a variable of type Distance which we have declared before.
The code then takes in user input and sets the values to the desired inputs.
As we can see, the object uses std::cin and std::cout like primitive datatypes in cpp which improves code readablility.
Enter feet: 5 <- input 1
Enter inches: 6 <- input 2
The value is 5'6"
Why Use Operator Overloading?
Enhanced Readability: It allows the use of operators with objects, making the code more natural and easier to read.
1.
Consistency with Built-in Types: It provides a consistent way to handle user-defined types, similar to how built-in types are handled.
2.
Code Reusability: It enables the reuse of existing operators for new data types.
3.
Ayanabh Saikia
R. No: 23BSA10205
Ayanabh Saikia
R. No: 23BSA10205
The alternative is to define class specific functions for the objects.
Although this is fine for smaller projects, it can get quite convoluted
when there are a large number of classes. Overloading operators helps
us to keep the code simple and reusing the same operators for all user
defined objects.
Ayanabh Saikia
R. No: 23BSA10205

C++ Overloading Operators << and >> input output overloading

  • 1.
    Topic: Overloading <<and >> operators Presented by: Ayanabh Saikia R. No: 23BSA10205
  • 2.
    Ayanabh Saikia R. No:23BSA10205 What is Operator Overloading? Operator overloading in C++ is a feature that allows you to redefine the way operators work for user- defined types (such as classes and structures). It enables operators to be used with objects in a manner similar to built-in types, thus providing intuitive and readable code. Which Operators can be overloaded? Almost all operators can be overloaded, with some exceptions such as: :: (Scope resolution) . (Member access) .* (Pointer to member) ?: (Ternary conditional) In this presentation we will be overloading the >> and << operators in a class declaring an object for specifying distance.
  • 3.
    Ayanabh Saikia R. No:23BSA10205 Implementation:
  • 4.
    Output: The code firstdeclares a variable of type Distance which we have declared before. The code then takes in user input and sets the values to the desired inputs. As we can see, the object uses std::cin and std::cout like primitive datatypes in cpp which improves code readablility. Enter feet: 5 <- input 1 Enter inches: 6 <- input 2 The value is 5'6" Why Use Operator Overloading? Enhanced Readability: It allows the use of operators with objects, making the code more natural and easier to read. 1. Consistency with Built-in Types: It provides a consistent way to handle user-defined types, similar to how built-in types are handled. 2. Code Reusability: It enables the reuse of existing operators for new data types. 3. Ayanabh Saikia R. No: 23BSA10205
  • 5.
    Ayanabh Saikia R. No:23BSA10205 The alternative is to define class specific functions for the objects. Although this is fine for smaller projects, it can get quite convoluted when there are a large number of classes. Overloading operators helps us to keep the code simple and reusing the same operators for all user defined objects.
  • 6.