C++ Notes(Cont'd)

C++ Notes(Cont’d)

NOTICE:

The following contents mainly serve the examinations. Some contents might be unreasonable in engineering.

Basics and Controls

  1. Modulo Arithmetic
cout << 17 % 5 << endl;
cout << 17 % (-5) << endl;
cout << (-17) % 5 << endl;
cout << (-17) % (-5) << endl;
/*
output:
2
2
-2
-2
*/
  1. Expression and Statement
    An expression has a value which is result of some operation(s) on it(theirs) operands.
    A statement is a sentence that acts as a commond.
  2. “Dangling else” Problem
    C++ groups a dangling else with the recent if.
  3. “Tricky if” Problem
int x = 10, y = 10;
if(y > 10 && x++ > 10) cout << "haha" << endl;
/*
After the the if expression, x is still 10.
It is because a logical expression is evaluated from left to right,
and the sub-expression (y > 10) is evaluated first. Since the result is false,
then (x++ > 10) won't even be evaluated because C++ supports
short-circuit evaluation (or lazy evaluation) which means that as 
soon as the truth value of a logical expression can be decided,   
any remaining sub-expressions in the logical expression are not evaluated.
*/
/*
In general,
Case 1: if ( false && stmt 1 && stmt 2 && .... && stmt N )
Case 2: if ( true || stmt 1 || stmt 2 || .... || stmt N)
stmt 1, stmt 2,...., stmt N won't be evaluated.
*/
  1. Switch
    The epression for switch must evaluate to an integral value(int, char, bool in C++).
    No two cases may have the same value. It will cause compilatio error.
    When a case constant is matched, the statements asscociated with the case are executed until a break statement, or a return statement, or the end of the switch statement.
    Remember to give a break!
  2. Loops and Iteration
    We can not define all variables or obejects at the for-initialization part.

Pointer

  1. Lvalue and Rvalue
    A variable has dual roles.
    lvalue: location of the memory storage (read-write)
    rvalue: value in the storage (read-only)
  2. Return-by- Reference and Return-by-value
/*
++x: the pre-increment operator
require x to be pass-by-reference
modify x by incrementing it by 1
return x (with its updated value) by reference 
the returned x is an lvalue
*/	
/*
x++: the pre-increment operator
require x to be pass-by-reference
save the current value of x in some temporary local variable
modify x by incrementing it by 1
returns the old value if x in the local variable by value
the returned x is an rvalue
*/
int x = 10;
++++++x;//valid
x++++++;//Compilation Error
  1. Reference Operator &
void f(int y, int &z) {
    cout << "Inside f()" << endl;
    cout << "y = " << y << "\taddress of y = " << &y << endl;
    cout << "z = " << z << "\taddress of a = " << &z << endl;
    int a = 9, b = 11;
    cout << "a = " << a << "\taddress of a = " << &a << endl;
    cout << "b = " << b << "\taddress of b = " << &b << endl;
}
int main() {
	int y = 10, z = 20;
    cout << "Inside main()" << endl;
    cout << "y = " << y << "\taddress of y = " << &y << endl;
    cout << "z = " << z << "\taddress of z = " << &z << endl;
    f(y,z);
}
/*
output:
Inside main()
y = 10	address of y = 0x7ffe33535c14
z = 20	address of z = 0x7ffe33535c18
Inside f()
y = 10	address of y = 0x7ffe33535bdc
z = 20	address of a = 0x7ffe33535c18
a = 9	address of a = 0x7ffe33535be0
b = 11	address of b = 0x7ffe33535be4
*/

We can observe that in the global function f, variable z in the f scope has the same address as the variable z in the main scope.

  1. Pointer Variable
    A pointer is an variable, which stores the address of another variable.
    A pointer has its own address in memory.
    A pointer can point to objects of basic types, user-defined types, another pointer and a function (function pointer).
    The “*” is called Asterisk, and the progress is called Dereference.
int x = 10;
int* xp = &x;/*xp points to x.The type of xp is int pointer.The position of 
Asterisk is not important.*/
cout << *xp << endl;//Dereference.
int x = 10;
int* xp = &x;
int** xxp = &xp;
int*** xxxp = &xxp;
cout << "x = " << x << "\t&x = " << &x << endl;
cout << "xp = " << xp << "\t&xp = " << &xp << "\t*xp = " << *xp << endl;
cout << "xxp = " << xxp << "\t&xxp = " << &xxp << "\t*xxp = " << *xxp
 											   << "\t**xxp = " << **xxp << endl;
cout << "xxxp = " << xxxp << "\t&xxxp = " << &xxxp << "\t*xxxp = " << *xxxp 
	 << "\t**xxxp = " << **xxxp << "\t***xxxp" << ***xxxp << endl;
/*
output (skip some meaningless information):
10                0x7ffe33535c10
0x7ffe33535c10    0x7ffe33535c28	10
0x7ffe33535c28	  0x7ffe33535c30    0x7ffe33535c10   10
0x7ffe33535c30	  0x7ffe33535c38    0x7ffe33535c28   0x7ffe33535c10    10
*/
  1. Pointer and sizeof( )
    All addresses are 8-bit (64-bit machine) or 4-bit (32-bit machine).
char cc = 'A';     char* ccp = &cc;
short ss = 5;      short* ssp = &ss;
int ii = 10;       int* iip = &ii;
double dd = 5.6;   double* ddp = &dd;
cout << sizeof(ccp) << '\t' << sizeof(*ccp) << '\t' << sizeof(&ccp) << endl;
cout << sizeof(ssp) << '\t' << sizeof(*ssp) << '\t' << sizeof(&ssp) << endl;
cout << sizeof(iip) << '\t' << sizeof(*iip) << '\t' << sizeof(&iip) << endl;
cout << sizeof(ddp) << '\t' << sizeof(*ddp) << '\t' << sizeof(&ddp) << endl;
/*
output:
8	1	8
8	2	8
8	4	8
8	8	8
*/   
  1. Variable, Reference Variable, Pointer Variable
    A no-const reference variable can only be bound to a object with a name (not a temporary object).
#include <iostream>
using namespace std;
int xxx = 5;
int& xxxref = xxx;
int* xxxptr = &xxx;
void xprint() {
    cout << hex << endl;//Print number in hexadecimal format
    cout << xxx << "    " << &xxx << endl;
    cout << xxxref << "    " << &xxxref << endl;
    cout << xxxptr << "    " << &xxxptr << "    " << *xxxptr << endl;
}
int main() {
	xxx += 1;
    xprint();
    xxxref += 1;
    xprint();
    xxxptr = &xxxref;
    xprint();
    return 0;
}
/*
output:
6    0x55e34e985010
6    0x55e34e985010
0x55e34e985010    0x55e34e985018    6

7    0x55e34e985010
7    0x55e34e985010
0x55e34e985010    0x55e34e985018    7

7    0x55e34e985010
7    0x55e34e985010
0x55e34e985010    0x55e34e985018    7

*/
  1. Const Pointer and Pointer to Const Object
    A const pointer points to the same object which can not be changed.
    A pointer to const object can not change the value through the pointer.
int x = 10, y = 20;
int* const xcp = &x;
xcp = &y;//Compilation Error: xcp is a const pointer
*xcp = 5;//OK: the variable pointed by xcp is not const
int x = 10, y = 20;
const int* pc = &x;//the same as: int const *pc = &x;
pc = &y;//OK: pc is free to point any int variable
*pc = 5;//Compilation Error: pc's content is const when accessed through pc
y = 8;//OK: y is not a const object
int x = 10;
const int* const xptr = &x;//xptr is a const pointer to const variable 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章