C++ For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming

 < Day Day Up > 


Expressions are built using operators and operands. The operators and operands in the expression specify a computation from which a value may result. The following statement offers several examples:

In this example, the two integer literals 3 and 6 are the operands to the multiplication operator. The resulting value from the multiplicative expression becomes one of the operands to the assignment operator. The other operand is the integer variable named i which is declared in the same statement.

There are many types of expressions and, in my opinion, they are best learned gradually rather than all at once. However, it will be helpful to get a feel for the different expression forms you will encounter as you learn C++. Table 5-3 lists different expression forms along with an example.

Table 5-3: Expression Forms

Expression Form

Examples

Primary

“Literals are examples of primary expressions!”

Postfix

- subscripting

- function call

- explicit type conversion(functional notation)

- pseudo destructor call

- class member access

- increment

- decrement

- dynamic cast

- type identification

- static cast

- reinterpret cast

- const cast

my_array[3] printScreen() float(int_val) my_class.~my_class() my_class.printScreen() count++ count-- dynamic_cast<float>(int_val) typeid(float).name() static_cast<char>(a + b) reinterpret_cast<int>(long_ptr) const_cast<A*>(&ra2)

Unary

- increment

- decrement

- sizeof

- new

- delete

++i or i++ --i or i-- sizeof i int * int_ptr = new int(7) delete int_ptr

Explicit type conversion(cast notation)

short s = (short) (3.5 + 2.6);

Pointer-to-member operators

class foo{ public: foo(){i=3;} int i; }; foo f1; int foo::* ip = &foo::i; cout<<f1.*ip<<endl;

Multiplicative operators

i * j i / j i % j

Additive operators

i + j i - j

Shift operators

i << 2 i >> 2

Relational operators

i < j i > j i <= j i >= j

Equality operators

i == j i != j

Bitwise AND operator

i & j

Bitwise exclusive OR operator

i ^ j

Bitwise inclusive OR operator

i | j

Logical AND operator

i && j

Logical OR operator

i || j

Conditional operator

(i < j) ? i = 3 : i = 7

Assignment operators

i = j i *= j i /= j i %= j i += j i -= j i >>= j i <<= j i &= j i ^= j i |= j

Comma operator

int i = 3, j = 2, k = 8

Constant expressions

const int MAX_VALUE = 500;

int i = MAX_VALUE;

No doubt some of the examples shown in table 5-3 will seem confusing to you if you are new to C++. Have no fear. Your understanding of complex expressions will grow as you progress in your C++ studies. And, as I said earlier, you don’t have to learn all the expression types at once.

You will, however, need to know some of the common operators and issues surrounding their use to be productive with C++ right away so I will discuss a few of these below.


 < Day Day Up > 

Категории