C++ Review
C++ Review
Outline ■C++ basic features Programming paradigm and statement syntax ■C| ass definitions Data members methods constructor destructor Pointers, arrays, and strings Parameter passing in functions Templates Friend Operator overloading O streams An example on file copy Makefile
2 Outline ◼ C++ basic features ◼ Programming paradigm and statement syntax ◼ Class definitions ◼ Data members, methods, constructor, destructor ◼ Pointers, arrays, and strings ◼ Parameter passing in functions ◼ Templates ◼ Friend ◼ Operator overloading ◼ I/O streams ◼ An example on file copy ◼ Makefile
Functions memory Every function needs a place to store its local variables Memory Collectively this storage is called location the stack This storage(memory aka "RAM) d2 Is a series of storage spaces and their numerical addresses Instead of using raw addresses we use variables to attach a name to an address all of the data/variables for a particular function call are located void aFunc(int x, int y) in a stack frame double d1, d2; int 1
3 Functions & Memory ◼ Every function needs a place to store its local variables. Collectively, this storage is called the stack ◼ This storage (memory aka “RAM”), is a series of storage spaces and their numerical addresses ◼ Instead of using raw addresses, we use variables to attach a name to an address ◼ All of the data/variables for a particular function call are located in a stack frame Memory location void aFunc(int x, int y) { double d1, d2; int i; } x y d2 d1 i
Functions memory(cont) When a function is called a new stack frame is set aside Parameters and return values are passed by copy(ie, they' re copied into and out of the stack frame) When a function finishes its stack frame is reclaimed void afunc (int x, int y) double dl=x+ int main(int argc, const char argv[])[ int x =7 a Func(1, 2) Y aFunc a Func(2,3)i return 0; 7 main
4 Functions & Memory (cont) ◼ When a function is called, a new stack frame is set aside ◼ Parameters and return values are passed by copy (ie, they’re copied into and out of the stack frame) ◼ When a function finishes, its stack frame is reclaimed void aFunc(int x, int y) { double d1 = x + y; } int main(int argc, const char * argv[]) { int x = 7; aFunc(1, 2); aFunc(2, 3); return 0; } x y d1 x 7 aFunc main
Programming Paradigm: Modular Concept program maln program dat module 1 module data +date data +da 2 DLCCCauLc procedure procedure The main program coordinates calls to procedures in separate modules and hands over appropriate data as parameters
5 Programming Paradigm: Modular Concept ◼ The main program coordinates calls to procedures in separate modules and hands over appropriate data as parameters
Modular Concept- Problems Decoupled Data and operations The resulting module structure is oriented on the operations rather than the actual data a The defined operations specify the data to be used
6 Modular Concept - Problems ◼ Decoupled Data and Operations ◼ The resulting module structure is oriented on the operations rather than the actual data ◼ The defined operations specify the data to be used
Object-Oriented Concept(C++) pro object data object data data object data Objects of the program interact by sending messages to each other
7 Object-Oriented Concept (C++) ◼ Objects of the program interact by sending messages to each other
Basic ctt Inherit all c syntax Primitive data types Supported data types: int, long, short, float double, char, bool and enum The size of data types is platform-dependent Basic expression syntax Defining the usual arithmetic and logical operations such as +r &,!,and| Defining bit-wise operations, such as & I, and Basic statement syntax If-else, for, while, and do-while
8 Basic C++ ◼ Inherit all C syntax ◼ Primitive data types ◼ Supported data types: int, long, short, float, double, char, bool, and enum ◼ The size of data types is platform-dependent ◼ Basic expression syntax ◼ Defining the usual arithmetic and logical operations such as +, -, /, %, *, &&, !, and || ◼ Defining bit-wise operations, such as &, |, and ~ ◼ Basic statement syntax ◼ If-else, for, while, and do-while
Basic C++(cont) ■ Add a new comment mark ∥/For1 line comment /.*/for a group of line comment New data type Reference data type & Much likes pointer nt ix/*ix is real variable * int&rx=ⅸ;/rxis"aias"forⅸ* ⅸ=1;/ also rx==1* rx=2:/alsoⅸ==2* const support for constant declaration, just likes C
9 Basic C++ (cont) ◼ Add a new comment mark ◼ // For 1 line comment ◼ /*… */ for a group of line comment ◼ New data type ◼ Reference data type “&”. Much likes pointer int ix; /* ix is "real" variable */ int & rx = ix; /* rx is "alias" for ix */ ix = 1; /* also rx == 1 */ rx = 2; /* also ix == 2 */ ◼ const support for constant declaration, just likes C
Class Definitions AC++ class consists of data members and methods(member functions) class Intcell Initializer list used to initialize the data Avoid implicit type conversion members directly public explicit Intcell( int initialvalye=0) storedvalue( initialvalue y[] Member functions int read( )const return storedValue iNdicates that the member's invocation does void write( int x) not change any of the data members storedvalue private int storedvalue Data member(s)
10 Class Definitions ◼ A C++ class consists of data members and methods (member functions). class IntCell { public: explicit IntCell( int initialValue = 0 ) : storedValue( initialValue ) {} int read( ) const { return storedValue;} void write( int x ) { storedValue = x; } private: int storedValue; } Member functions Data member(s) Indicates that the member’s invocation does not change any of the data members. Avoid implicit type conversion Initializer list: used to initialize the data members directly