Title: C Overview, Part II (continued)
1C Overview, Part II(continued)
Reference Chapter 2, Sections 2.2-2.9
CMSC 202
1
2Function Calls and Argument Passing
- Passing by value vs. passing by reference
(address) - C example
- int a 5, b 10
- swap(a, b) // function call
- void swap(int a, int b)
-
- int temp a
- a b
- b temp
-
CMSC 202
2
3Function Calls and Argument Passing (continued)
- C supports reference parameters
- C example
- int a 5, b 10
- swap(a, b) // function call -- no
addresses! - void swap(int a, int b)
-
- int temp a
- a b
- b temp
-
- No dereferencing of formal parameters is
necessary
CMSC 202
3
4Inline Functions
- A function can have the code for its body
included with - its prototype
- Example
- inline int max(int a, int b) return(a gt
b ? a b) - Each function call is expanded by the compiler
- Increases code efficiency
- No guarantee that the compiler will do the
expansion - Use only for very small (single statement)
functions
CMSC 202
4
5Command-line Arguments
- The main function can receive arguments when
invoked - Implemented as follows
- int main(int argc, char argv)
- where argc number of command line
arguments, - including command line
name (default 1) - argv0 pointer to command line
name as string - argvn pointer to command line
argument as string - Example
- lowercase input.dat output.dat //
lowercase program in text
CMSC 202
5
6Vector Class Example
// File Vector.h class Vector public
Vector() //
default constructor Vector(float a, float
b) // constructor overloaded
Vector operator-(Vector a) // operating
overloading float inner(Vector a)
// vector inner product int nonzero()
// host vector nonzero
void display() // displays
host vector private float x
// vector x-coordinate float y
// vector y-coordinate
CMSC 202
6
7Vector Class Example (continued)
// File Vector.C include Vector.h include
ltiostream.hgt VectorVector(float a, float b)
// constructor overloaded x a y
b Vector Vectoroperator -(Vector a) //
operator overloading Vector tmp tmp.x x
- a.x tmp.y y - a.y return(tmp)
CMSC 202
7
8Vector Class Example (continued)
float Vectorinner(Vector a) return(x a.x
y a.y) int Vectornonzero()
return(x ! 0.0 y ! 0.0) void
Vectordisplay() cout ltlt ( ltlt x ltlt ,
ltlt y ltlt )
CMSC 202
8
9Default Constructors
- A constructor with no arguments is called a
default - constructor.
- Usually used to set the attributes equal to
default values - VectorVector()
- a b 0.0
-
- If a class has no constructors, a default
constructor that - does nothing is supplied automatically.
- If a class defines any constructor, no default
constructor - is automatically supplied. The appropriate
user-supplied - constructor will be invoked upon object
instantiation.
CMSC 202
9
10Operator Overloading
- Predefined operators can be extended to
operate on different - types of operands. This is called operator
overloading. - Some consider this a type of polymorphism
- Example
- Vector Vectoroperator -(Vector a)
// overloading minus - Vector tmp
- tmp.x x - a.x tmp.y y - a.y
- return(tmp)
-
- The minus operator now works with Vector
objects - u v.operator - (w) OR u v - w
- where u, v, and w are all of type
Vector
CMSC 202
10
11C I/O Streams
- fstream is a predefined class in ltfstream.hgt
- fstream represents a stream I/O class
- General format
- fstream stream-name(disk-filename,
mode) - Examples
- fstream inData(mydata, iosin)
- fstream outData(results, iosout)
- inData gtgt x // read a value from
input file - outData ltlt x // send value to
output file - inData.close() // done by default on
some systems - outData.close() // done by default on
some systems
CMSC 202
11
12Error Handling
- Catch all errors that you can!
- cerr is a predefined output stream used for
error messages - Example
- fstream myin(file, iosin)
- if (myin.fail()) // fail is a member
function of fstream - cerr ltlt Cant open ltlt file ltlt
\n - exit(1) // terminates execution
-
- Usually want to terminate execution and give
the user a - meaningful error message for debug purposes.
Use exit - function from ltstdlib.hgt.
CMSC 202
12
13Student Exercise
- Copy the code for program lowercase.C (pp.
67-68) from - the diskette in your text.
- Make the following correction before running
- cerr ltlt Usage lower infile outfile\n
//incorrect - cerr ltlt Usage ltlt argv0 ltlt infile
outfile\n - Create an appropriate input file.
- Run the program with no arguments, one
argument - (error), two arguments, and three arguments
(error).
CMSC 202
13