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)
The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.