C Overview, Part II (continued) - PowerPoint PPT Presentation

About This Presentation
Title:

C Overview, Part II (continued)

Description:

int main(int argc, char *argv[]) where argc = number of command line arguments, ... argv[0] = pointer to command line name as string ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 14
Provided by: SusanMi6
Category:

less

Transcript and Presenter's Notes

Title: C Overview, Part II (continued)


1
C Overview, Part II(continued)
Reference Chapter 2, Sections 2.2-2.9
CMSC 202

1
2
Function 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
3
Function 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
4
Inline 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
5
Command-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
6
Vector 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
7
Vector 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
8
Vector 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
9
Default 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
10
Operator 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
11
C 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
12
Error 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
13
Student 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
Write a Comment
User Comments (0)
About PowerShow.com