0% found this document useful (0 votes)
63 views

Arithmetic Operators: Operator Name Syntax Overloadable Included in Prototype Example (T1 and T2 Are Types)

The document summarizes C++ and C operators including: 1) Arithmetic operators like addition, subtraction, multiplication, division and their assignment counterparts. 2) Comparison operators like less than, greater than, equal to. 3) Logical operators like AND, OR, NOT. 4) Bitwise operators like shift, AND, OR and their assignment counterparts. It also lists operator precedence from highest to lowest and whether the operators are overloadable in C++.

Uploaded by

ramuappala
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

Arithmetic Operators: Operator Name Syntax Overloadable Included in Prototype Example (T1 and T2 Are Types)

The document summarizes C++ and C operators including: 1) Arithmetic operators like addition, subtraction, multiplication, division and their assignment counterparts. 2) Comparison operators like less than, greater than, equal to. 3) Logical operators like AND, OR, NOT. 4) Bitwise operators like shift, AND, OR and their assignment counterparts. It also lists operator precedence from highest to lowest and whether the operators are overloadable in C++.

Uploaded by

ramuappala
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

For the purposes of this table, a, b, and c represent valid values (literals, values from variables, or return value),

object names, or lvalues, as appropriate. "Overloadable" means that the operator is overloadable in C++. "Included in C" means that the operator exists and has a semantic meaning in C (operators are not overloadable in C). [edit]Arithmetic Operator name Unary plus Addition (sum) Prefix increment Syntax
+a a + b ++a

operators
Prototype example (T1 and T2 are types)
T1 operator +(const T1& a); T1 operator+(const T1& a, const T1& b); T1& operator++ (T1& a); T1 operator++ (T1& a, int);

Included Overloadable in C Yes Yes Yes Yes Yes Yes

Postfix increment

a++

(The int argument is required by Yes Yes C++ to differentiate the operator from a prefix-increment operator.)

Assignment by addition Unary minus (negation) Subtraction (difference) Prefix decrement Postfix decrement Assignment by subtraction Multiplication (product) Assignment by multiplication Division (quotient) Assignment of division Modulo (remainder) Assignment of modulo

a += b -a a - b --a a-a -= b a * b a *= b a / b a /= b a % b a %= b

Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes

Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes

T1& operator+=(T1& a, const T1& b); T1 operator-(const T1& a); T1 operator-(const T1& a, const T1& b); T1& operator--(T1& a); T1 operator--(T1& a, int); T1& operator-=(T1& a, const T1& b); T1 operator*(const T1 &a, const T1& b); T1& operator*=(T1& a, const T1& b); T1 operator/(const T1& a, const T1& b); T1& operator/=(T1& a, const T1& b); T1 operator%(const T1& a, const T1& b); T1& operator%=(T1& a, const T1& b);

[edit]Comparison

operators/Relational operators

Operator name

Syntax

Overloadable

Included in C

Prototype example
bool operator<(const T1& a, const T2& b);

Less than

a < b

Yes

Yes

bool T1::operator<(const T2& b) const;

Less than or equal to Greater than Greater than or equal to Not equal to Equal to

a <= b a > b a >= b a != b a == b

Yes Yes Yes Yes Yes [edit]Logical

Yes Yes Yes Yes Yes

bool operator<=(const T1& a, const T2& b); bool operator>(const T1& a, const T2& b); bool operator>=(const T1& a, const T2& b); bool operator!=(const T1& a, const T2& b); bool operator==(const T1& a, const T2& b);

operators
Included in C Yes Yes Yes Prototype example
bool operator!(const T1& a); bool operator&&(const T1& a, const T2& b); bool operator||(const T1& a, const T2& b);

Operator name Logical negation (NOT) Logical AND Logical OR

Syntax
!a a && b a || b

Overloadable Yes Yes Yes [edit]Bitwise

operators
Included in C Yes Yes Yes Yes Yes Yes Yes Prototype example
T1 operator<<(const T1& a, const T2& b); T1& operator<<=(T1& a, const T2& b); T1 operator>>(const T1& a, const T2& b); T1& operator>>=(T1& a, const T2& b); T1 operator~(const T1& a); T1 operator&(const T1& a, const T2& b); T1& operator&=(T1& a, const T2& b);

Operator name Bitwise left shift Assignment by bitwise left shift Bitwise right shift Assignment by bitwise right shift Bitwise one's complement (NOT) Bitwise AND Assignment by bitwise AND

Syntax
a << b a <<= b a >> b a >>= b ~a a & b a &= b

Overloadable Yes Yes Yes Yes Yes Yes Yes

Bitwise OR Assignment by bitwise OR Bitwise XOR Assignment by bitwise XOR

a | b a |= b a ^ b a ^= b

Yes Yes Yes Yes [edit]Other

Yes Yes Yes Yes

T1 operator|(const T1& a, const T2& b); T1& operator|=(T1& a, const T2& b); T1 operator^(const T1& a, const T2& b); T1& operator^=(T1& a, const T2& b);

operators
Included in C Yes Yes Yes Yes Yes Yes Yes No No Prototype example
T1& T1::operator= (const T2& b); void operator() (T1& a); const T2& operator[] (const T1& a, const T2& b); T2& operator* (T1& a); T2* operator& (T1& a); T2* T1::operator-> (); // must be a member function

Operator name Basic assignment Function call Array subscript Indirection (dereference) Address-of (reference) Member by pointer Member Bind pointer to member by pointer Bind pointer to member by reference cast

Syntax
a = b a() a[b] *a &a a->b a.b a->*b a.*b

Overloadable Yes Yes Yes Yes Yes Yes No Yes No

N/A N/A N/A


T1::operator type(); // must be a member function and return an object of type type T2& operator, (const T1& a, T2& b);

(type) a

Yes

Yes

comma ternary conditional Scope resolution Pointer to member size-of Type identification Allocate storage Allocate storage (array)

a , b a ? b : c a::b a::*b sizeof a sizeof(type) typeid(a) typeid(type) new type new type[n]

Yes No No No No No Yes Yes

Yes Yes No No Yes No No No

N/A N/A N/A N/A N/A


void *T1::operator new(size_t x) void *T1::operator new[](size_t x)

Deallocate storage Deallocate storage (array) [edit]Operator

delete a delete[] a

Yes Yes

No No

void T1::operator delete(void *x) void T1::operator delete[](void *x)

precedence

The following is a table that lists the precedence and associativity of all the operators in the C++ and C programming languages (when the operators also exist inJava, Perl, PHP and many other recent languages the precedence is the same as that given). Operators are listed top to bottom, in descending precedence. Descending precedence refers to the priority of evaluation. Considering an expression, an operator which is listed on some row will be evaluated prior to any operator that is listed on a row further below it. Operators that are in the same cell (there may be several rows of operators listed in a cell) are evaluated with the same precedence, in the given direction. An operator's precedence is unaffected by overloading. The syntax of expressions in C and C++ is specified by a context-free grammar.[citation needed] The table given here has been inferred from the grammar.[citation needed] A precedence table, while mostly adequate, cannot resolve a few details. In particular, note that the ternary operator allows any arbitrary expression as its middle operand, despite being listed as having higher precedence than the assignment and comma operators. Thus a ? b , c : d is interpreted as a ? (b, c) : d, and not as the meaningless (a ? b), (c : d). Also, note that the immediate, unparenthesized result of a C cast expression cannot be the operand of sizeof. Therefore, sizeof (int) * x is interpreted as (sizeof(int)) * x and not sizeof ((int)
*x).

Precedence Operator

Description

Associativity

::

Scope resolution (C++ only)

Left-to-right

++ --

Postfix increment and decrement

()

Function call

[]

Array subscripting

Element selection by reference

->

Element selection through pointer

typeid()

Run-time type information (C++ only)

const_cast

Type cast (C++ only)

dynamic_cast

Type cast (C++ only)

reinterpret_cast Type cast (C++ only)

static_cast

Type cast (C++ only)

++ --

Prefix increment and decrement

Right-to-left

+-

Unary plus and minus

!~

Logical NOT and bitwise NOT

(type)

Type cast

3
*

Indirection (dereference)

&

Address-of

sizeof

Size-of

new new[]

Dynamic memory allocation (C++ only)

delete delete[]

Dynamic memory deallocation (C++ only)

.* ->*

Pointer to member (C++ only)

Left-to-right

*/%

Multiplication, division, and modulus (remainder)

+-

Addition and subtraction

<< >>

Bitwise left shift and right shift

< <=

Relational less than and less than or equal to

8
> >=

Relational greater than and greater than or equal to

== !=

Relational equal to and not equal to

10

&

Bitwise AND

11

Bitwise XOR (exclusive or)

12

Bitwise OR (inclusive or)

13

&&

Logical AND

14

||

Logical OR

15

c ? t : f

Ternary conditional (see ?:)

Right-to-Left

Direct assignment (provided by default for C++ classes)

(Not available for throw)

+= -=

Assignment by sum and difference

16

*= /= %=

Assignment by product, quotient, and remainder

<<= >>=

Assignment by bitwise left shift and right shift

&= ^= |=

Assignment by bitwise AND, XOR, and OR

17

throw

Throw operator (exceptions throwing, C++ only)

18

Comma

Left-to-right

You might also like