oprators in cpp,types with example and details.pptx
komalrokade4
16 views
20 slides
Oct 18, 2024
Slide 1 of 20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
About This Presentation
oprators in c++
Size: 314.01 KB
Language: en
Added: Oct 18, 2024
Slides: 20 pages
Slide Content
Ashokrao Mane Group of Institution’s Department of Computer Science &Engineering Sub-OOP By Prof.K.V.Rokade
An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. C++ is rich in built-in operators and provide the following types of operators − Arithmetic Operators Relational Operators Logical Operators Bitwise Operators Assignment Operators Misc Operators Operators in C++
Arithmetic Operators There are following arithmetic operators supported by C++ language − Assume variable A holds 10 and variable B holds 20, then −
Relational Operators There are following relational operators supported by C++ language Assume variable A holds 10 and variable B holds 20, then −
Logical Operators There are following logical operators supported by C++ language. Assume variable A holds 1 and variable B holds 0, then −
Misc Operators
C++ allows you to specify more than one definition for a function name or an operator in the same scope, which is called function overloading and operator overloading respectively. An overloaded declaration is a declaration that is declared with the same name as a previously declared declaration in the same scope, except that both declarations have different arguments and obviously different definition (implementation). When you call an overloaded function or operator , the compiler determines the most appropriate definition to use, by comparing the argument types you have used to call the function or operator with the parameter types specified in the definitions. The process of selecting the most appropriate overloaded function or operator is called overload resolution . Function Overloading in C++ You can have multiple definitions for the same function name in the same scope. The definition of the function must differ from each other by the types and/or the number of arguments in the argument list. You cannot overload function declarations that differ only by return type. Following is the example where same function print() is being used to print different data types − C++ Overloading (Operator and Function)
What is Operator Overloading? Using operator overloading in C++, you can specify more than one meaning for an operator in one scope. The purpose of operator overloading is to provide a special meaning of an operator for a user-defined data type. With the help of operator overloading, you can redefine the majority of the C++ operators. You can also use operator overloading to perform different operations using one operator.
class class_name { ... .. ... public return_type operator symbol (argument(s)) { ... .. ... } ... .. ... }; syntax
No. There are C++ operators that can't be overloaded. They include: :: -Scope resolution operator ?: -ternary operator. . -member selector Sizeof operator * -member pointer selector Can all C++ Operators be Overloaded?
Type Conversion refers to conversion from one type to another. The main idea behind type conversion is to make variable of one type compatible with variable of another type to perform an operation. For example, to find the sum of two variables, one of int type & other of float type. So, you need to type cast int variable to float to make them both float type for finding the sum. In this blog we will learn how to perform type conversion in C++. In C++, there are two types of type conversion i.e. implicit type conversion & explicit type conversion. Implicit Type Conversion Implicit type conversion or automatic type conversion is done by the compiler on its own. There is no external trigger required by the user to typecast a variable from one type to another. This occurs when an expression contains variables of more than one type. So, in those scenarios automatic type conversion takes place to avoid loss of data. In automatic type conversion, all the data types present in the expression are converted to data type of the variable with the largest data type. Below is the order of the automatic type conversion. You can also say, smallest to largest data type for type conversion. bool -> char -> short int -> int -> unsigned int -> long -> unsigned -> long long -> float -> double -> long double Implicit conversions can lose information such as signs can be lost when signed type is implicitly converted to unsigned type and overflow can occur when long is implicitly converted to float. Now let us look at an example to understand how implicit type conversion works in C++. Type Conversion In C++
#include < iostream.h > int main() { int int1 = 100; // integer int1 char char1 = 'c'; // character char1 // char1 implicitly converted to int using ASCII value of 'c' i.e. 99 int1 = int1 + char1; // int1 is implicitly converted to float float flt1 = int1 + 2.7; cout << "int1 = " << int1 << endl << "char1 = " << char1 << endl << "flt1 = " << flt1 << endl ; return 0; } Output int1 = 199 char1 = c flt1 = 201.7 Implicit conversion program
Explicit Type Conversion Explicit type conversion or type casting is user defined type conversion. In explicit type conversion, the user converts one type of variable to another type. Explicit type conversion can be done in two ways in C++: Converting by assignment Conversion using Cast operator Now let’s look at each of the ways to explicit type cast one type to another. 1. Converting by assignment In this type conversion the required type is explicitly defined in front of the expression in parenthesis. Data loss happens in explicit type casting. It is considered as forceful casting. Let’s look at an example. Type Conversion In C++
#include < iostream > using namespace std; int main() { double dbl1 = 8.9; // Explicit conversion from double to int int res = ( int )dbl1 + 1; cout << "Result = " << res; return 0; } Output Result = 9 Converting by assignment
2. Conversion using Cast Operator Cast operator is an unary operator which forces one data type to be converted into another data type. There are four types of casting in C++, i.e. Static Cast, Dynamic Cast, Const Cast and Reinterpret Cast . #include < iostream.h > int main() { float flt = 30.11; // using cast operator int int1 = static_cast < int >( flt ); cout <<int1; } Output 30 Conversion using Cast Operator