Chapter 1 C Basics Review Section 1 4
Chapter 1 C++ Basics Review (Section 1. 4)
Classes l Defines the organization of a data user-defined type. l Members can be ¡ ¡ l Information Hiding Labels ¡ ¡ ¡ l Data Functions/Methods public private protected Constructors ¡ ¡ We have two in this example Why?
Additional Syntax and Accessors l Initializer list ¡ l Explicit constructor ¡ l Init data members directly in the constructor Avoids automatic type conversion (and resulting bugs) Constant member functions ¡ ¡ ¡ Examines, but does not change the object state Also called ‘accessor’ Non-const functions are called ‘mutators’
Interface Vs. Implementation Interface l Interface typically defined in. h files ¡ l #include in. c file Preprocessor commands ¡ Guards against multiple inclusion of. h files
Interface Vs. Implementation (contd. ) l Scoping operator ¡ l To identify the class corresponding to each function Remember ¡ ¡ Function signatures must match in both interface and implementation Default parameters are specified only in the interface Implementation
main() function l Objects are declared just like primitive data types. l Legal Declarations l ¡ Intcell obj 1; // zero parameter constructor ¡ Intcell obj 2(12); // one parameter constructor Illegal declarations ¡ Intcell obj 3 = 37; // explicit constructor used ¡ Intcell obj 4(); // function declaration main() function
Vectors l Replaces built-in C++ arrays ¡ l Standard vector class ¡ ¡ l Built-in arrays do not act as proper C++ objects Gives a size() function Can be assigned using = Similarly C++ also provides standard string class.
Pointers l Pointer variable ¡ l Declaration ¡ ¡ ¡ l Stores the address of another object in memory. * before the variable name indicates a pointer declaration Pointers are uninitialized at declaration time. Reading uninitialized pointer values results in bugs. Dynamic object creation ¡ Using the new keyword
Pointers (contd) l Garbage collection ¡ ¡ ¡ l Accessing members of an object ¡ l Objects allocated using new must be explicitly deleted. Else your program will have memory leaks There’s no automatic GC in C++. Use the -> operator Address-of operator ¡ &obj gives the address where obj is stored. Memory leaks= errors and grade penalties in your programming assignment (we will check for those)
Parameter Passing l double avg( const vector<int> & arr, int n, bool & error. Flag); l Call by value ¡ ¡ ¡ l Call by reference ¡ l Copies the value of parameter being passed. Called function an modify the parameter, but cannot alter the original variable. What happens if the parameter is an object? Used when the function needs to change the value of original argument Call by constant reference ¡ Typically used when l l l parameter is a large object Should not be changed by the function Using call-by-value would result in large copying overhead.
Return Passing l Return by value ¡ l Return by reference ¡ l Return the address of the variable returned Correct Return by constant reference ¡ ¡ l Makes a copy of the variable returned Return the address of the variable returned Return value cannot be modified by caller. Last two techniques ¡ Lifetime of returned value should extend beyond the function called Incorrect Why? ?
Reference Variables l Synonyms of objects they reference ¡ l Reference are not pointers Can be used for ¡ ¡ Parameter passing Local variables l Avoid the cost of copying l E. g. string x = find. Max(a); string &y = x; cout << y << endl; l Also used for referencing objects with complex expression ¡ list<T> &which. List = the. Lists[ hash(x, the. Lists. size()) ];
Destructor l Called whenever ¡ ¡ l Object goes out of scope delete called Frees up resource allocated for the object
Copy constructor l l Initializes a new object to another of its own type Invoked during ¡ Declaration Int. Cell B = C; Intcell B (C); ¡ ¡ l Call by value Return by value But not in ¡ B = C; (assignment operator)
operator= Copy assignment operator l Called when both LHS and RHS objects have been created l
Problem with defaults l Usually don’t work when data member is a pointer type. l What is the output of f() in the adjacent example? l In this example, default operator= and copy constructor copy the pointer instead of the value
Exercise l Find out the difference between ¡ ¡ Shallow copy, and Deep copy
- Slides: 17