S1: Overview of C++
- Textbook Ch. 1, Ch. 2 + Additional Material
- Goals:
- Provide a summary of C++ features
- Review C++ for C programmers
- Executive Summary of C++
- A better C
- C and C++ have a lot in common
- C++ has features unrelated to OOP to improve C
- Data Abstraction
- Object Oriented
- Is Textbook (Ford & Topp) adequate to learn C++?
- Focus on Data Abstraction & Object Orientation
- Inadequate description of C related issues
- Adequate for CS 3321, assuming you know C.
- Following slides provide a summary of improvements to C
- References are provided for further details
- Ex. Public domain slides from WWW
Copyright: S. Shekhar, C. S. Dept.,
University of Minnesota, Minneapolis, MN 55455. Csci,3321,Winter.(home)
S2: Program Organization in C++
- As in C, a C++ program is spread over multiple files
- Header files for declarations
- < iostream.h > , "Tree.h"
- Source files - suffix compiler dependent: .C, .cpp, ...
- #include preprocessor directive to include header files
- Implications of multiple files
- Tedious 3-Steps process: (i) individually compile source files,
- (ii) link object files (iii) load and run
- Recommendation: Use "make" utility
- Another problem with multiple files
- What if a header files is included multiple times?
- How to avoid compilation error
#ifndef FILENAME_H
#define FILENAME_H
// insert contents of the header file here
#include "filename.h"
#endif
Copyright: S. Shekhar, C. S. Dept.,
University of Minnesota, Minneapolis, MN 55455. Csci,3321,Winter.(home)
S3: C++ Statements, Operators, Comments
- C++ Statements: Same syntax and semantics as in C
- Simple: null, break, continue, goto
- Conditionals: if, switch
- Loops: for, while, do-while
- For Review: See Ch# 2 (Slides from Superconductor ... Lab)
- available from WWW & TA-account
- C++ Operators identical to C operators, except
- new, delete for memory management
- stream I/O using << , >>
- C++ allows operator OVERLOADING
- i.e. an operator may have multiple meanings
- Comments in C++
- Has C-like Multiline comments: delimited by /* and */
- Problem with C comments: No nesting
/* code1 /* comment */ code2 */ is not all comments!
- C++ adds Single line comments
- all text after // is ignored by compiler
Copyright: S. Shekhar, C. S. Dept.,
University of Minnesota, Minneapolis, MN 55455. Csci,3321,Winter.(home)
S4: Exercises
- Consider the following program:
#include < stdio.h >
int main(void)
{
int language = 1 //**/ 2
; /* Semicolon by itself */
}
- Q? What is the value of language if a C compiler is used?
- Q? What is the value of language if a C++ compiler is used?
Copyright: S. Shekhar, C. S. Dept.,
University of Minnesota, Minneapolis, MN 55455. Csci,3321,Winter.(home)
S5: Reducing Safety Hazards
- Q? Which features of C are most error prone?
- Inadequate type-checking, Few types
int x = 5; If (0 < x < 2) printf "Do I know boolean type?";
- Overuse of Pointers = > Efficient but UNSAFE
- Ex. Identify problems with the following C-codes:
void main(){
int a[10], b, c; char *greetings ;
scanf( "%d %d", b, c ); /* input */
a[10] = 100 ; /* array indices are non-intuitive */
greetings = "Hello"; /*strings are 2nd class citizen */
swap(&a, &b); /* Pointer Parameter */
}
void swap(int *x, int *y) { int *z; *z = *x; *x = *y; *y = *z;}
- C++ = > safety with efficiency = > reduce pointer usage
void main(){
int b, c; cin >> b >> c; // typesafe input
String greetings = "Hello" + " There" ; //safe strings
Array < int > a(1,10); a[10] = 100; // safe arrays (not standard yet)
swap(&a, &b); // Intuitive Reference Parameter
}
void swap(int& x, int& y) { int z; z = x; x = y; y = z; }
Copyright: S. Shekhar, C. S. Dept.,
University of Minnesota, Minneapolis, MN 55455. Csci,3321,Winter.(home)
S6: Data Declaration in C++
- FOR DETAILS: See Chapter 2 of the textbook and
- Ch# 1 (Superconductor ... Lab slides) at ta-account
- Data declaration need not be at the start of block!
- Constants and Variables
- Constant Values: 5, 'a', 4.3
- Variables
- Constant variables : value can't change after initialization
Sum(const int SIZE) // fixed parameter
- Enumeration type: enum Boolean { FALSE, TRUE };
- Like Pascal, FALSE and TRUE are integers 0 and 1.
- Pointers hold memory address
int i=25; int *np; np = &i;
- REFERENCE type: Alias = provide another name for an object
int i = 5; int& j=i; i=7; cout << j; //prints 7
- Structure types: arrays, struct and class
- arrays can be initialized
- class and struct can include functions!
- Default visibilities are different
- Data abstraction and OOP features
Copyright: S. Shekhar, C. S. Dept.,
University of Minnesota, Minneapolis, MN 55455. Csci,3321,Winter.(home)
S7: Input / Output in C++ : IOstreams
- Overview
- #include < iostream.h > , cin, cout, cerr, << , >>
- format-free, << and >> and be overloaded
- cout : stdout output device (terminal or file)
- << operator separates entities from each other and keyword
int n = 50; cout << "n = " << n << endl;// Prints n = 50
- cin : stdin input device (terminal or file)
- >> operator separates entities from each other and keyword
int a, b; cin >> a >> b;// read a then b
- Formats - stream manipulators - endl, ws, setw, setprecision, ...
cout << endl ; // write newline and flush output stream
cin << ws ; // remove whitespace - space, tab, newline
cout << setprecision( 3 ) ;// floating-point precision = 3 digits
cout << setw( 5 ) ;// field width = 5
cout << "i = " << hex << 91 << endl ;// outputs i = 5b
- File I/O : fstream.h, ofstream
#include < fstream.h >
...
ofstream outFile("my.out", ios::out); // open file to output
outFile << "Hello there"; // use outFile instead of cout
Copyright: S. Shekhar, C. S. Dept.,
University of Minnesota, Minneapolis, MN 55455. Csci,3321,Winter.(home)
S8: Functions in C++
- Details in Ch# 3 (Superconductor ... Lab) at ta-account
- Two kinds : regular functions, member functions
- member functions are part of a "class"
- Parameter Passing
- By Value (like C) - copy value
- By Reference (like Pascal) - copy address (share storage)
int max( int par1, int& par2, const int& par2)
//par1 - value copied, function can't change actual parameter
//par2 - share storage, actual can be changed
//par3 - share storage, actual can't be changed
- Array are passed by Reference like C
- Functions Name Overloading
- Conflict resolved by number/types of arguments/result
int max(int, int);
int max(float, int);
- Inline Functions
inline int sum( int a, int b) { return a+b; }
Copyright: S. Shekhar, C. S. Dept.,
University of Minnesota, Minneapolis, MN 55455. Csci,3321,Winter.(home)
S9: Scope in C++
- Four delimiters - block, function, file, class
- Block: hierarchical unit
- Function: labels have function scope
- Files for names declared outside functions and Classes
- extern - import variables from other files
- static - allow 2 global vars in two files to have same name
- :: operator - Absolute names
- Designate member function
- avoid hole-in-the scope in nested blocks
- CLASS: declaration associated with class
const float PI = 3.14152;
class Circle { float radius;
float Area(void) const; // function prototype
}
float Circle::Area(void) const { // member function
return PI * radius * radius; // radius from class Circle
}
- Classes can span multiple files: circle.h, circle.C
- For Details: See Ch# 3 in the textbook
- and Ch# 3 (Superconductor ... Lab slides) at ta-account
Copyright: S. Shekhar, C. S. Dept.,
University of Minnesota, Minneapolis, MN 55455. Csci,3321,Winter.(home)
S10: Memory Management
- For Details: See Ch# 8 in the textbook
- and Ch# 3 (Superconductor ... Lab slides) at ta-account
- Q? How did we allocate/deallocate memory in C?
- Operators "new" and "delete"
- new: create object and return pointer or 0
- delete: destroy object pointed to
- Example
int *ip = new int; if (ip == 0) cerr << "memory problems" << endl;
int *jp = new int[10];
if (jp == 0) cerr << "memory problems" << endl;
delete ip; // delete an object
delete [] jp; // delete an array
Copyright: S. Shekhar, C. S. Dept.,
University of Minnesota, Minneapolis, MN 55455. Csci,3321,Winter.(home)