4 Operator Overloading

Object Oriented Programming · Unit 4 · 7 hrs

Operator Overloading

Exam-focused notes for Operator Overloading (Object Oriented Programming, CSC166): what the TU syllabus asks and how it has actually been tested, with 8 solved past questions from this unit.

What this unit covers

  • Fundamental of operator overloading
  • Restriction on operator overloading
  • Operator functions as a class members
  • Overloading unary and binary operator
  • Data Conversion (basic to basic, basic to user-defined, user-defined to basic, user-defined to user-defined)

Operator functions as a class members

20805 marks

Create a class named Point with data members x(int) and y(int). Add operator overloading to find the Euclidean distance between two points. [5]

Operator overloading allows us to redefine the behavior of an operator for user-defined types (classes). Here, we overload the - operator (or a suitable operator) to compute the Euclidean distance between two Point objects. The Euclidean distance formula be...

Full solved answer →

Fundamental of operator overloading

207910 marks

What is operator overloading? Why it is necessary to overload and operator? Write a program for overloading comparison operators.[10]

"The method of making operators to work for user defined class and having the ability to provide operators with a special user defined meaning is known as operator overloading." In simple terms, operator overloading allows us to redefine the behavior of exi...

Full solved answer →
207610 marks

Explain the concept of operator overloading? List the operators that cannot be overloaded. Write programs to add two object of distance class with data members feet and inch by using member function and friend function.[10]

--- Operator overloading is a compile-time polymorphism feature in C++ that allows the programmer to redefine or extend the meaning of built-in operators (such as +, -, , ==, etc.) for user-defined data types (classes/objects). When an operator is overloade...

Full solved answer →

Data Conversion

20795 marks

What are possible data conversion types? Write a program to show basis to user defined conversion. [5]

In C++, there are mainly two types of data conversion: --- - Done automatically by the compiler without any external trigger from the user. - Takes place when there is more than one data type in an expression. - All data types are upgraded to the largest da...

Full solved answer →
207810 marks

What is meant by type conversion? Define two way of converting one user defined data type (object) to another user defined object? Write a program that converts object of another distance class with data members feet and inch. (Assume 1m = 3.3 feet and 1cm = 0.4 inch)[10]

- Source class: Meter with data members meter (m) and centimeter (cm). - Destination class: Distance with data members feet and inch. - Conversion factors: - $1\ \text{m} = 3.3\ \text{feet}$ - $1\ \text{cm} = 0.4\ \text{inch}$ - Task: write a program conver...

Full solved answer →
207510 marks

Explain the concept of user-defined to user-defined data concession with the conversion routine located in the destination class.[10]

In C++, type conversion refers to converting a value from one data type to another. When both the source and destination are user-defined data types (i.e., classes), a special conversion routine must be written., "classes are user-defined data types and beh...

Full solved answer →
2080.15 marks

Create two classes Rupee and Dollar respectively. Write conversion operator to convert between Rupee and Dollar assuming that 1 dollar equals 133 rupees. Write a main program that allows the user to enter an amount in either currency and then converts it to other currency and displays the result. [5]

This problem uses type conversion between class objects using a conversion operator (casting operator) defined inside the source class. The conversion operator converts an object of one class type to another class type. Rate: 1 Dollar = 133 Rupees --- --- C...

Full solved answer →

Overloading unary and binary operator

2080.110 marks

Write a program to create a class named height with data members meter(int) and centimeter(int). Overload the binary + operator using friend function to add two heights.[10]

Binary Operator Overloading occurs when an operator works on two operands. When overloaded using a friend function, the function is not a member of the class but has access to all private and protected members of the class. A friend function takes both oper...

Full solved answer →