C Overview Part II continued Reference Chapter 2

  • Slides: 13
Download presentation
C++ Overview, Part II (continued) Reference: Chapter 2, Sections 2. 2 -2. 9 CMSC

C++ Overview, Part II (continued) Reference: Chapter 2, Sections 2. 2 -2. 9 CMSC 202 1

Function Calls and Argument Passing • Passing by value vs. passing by reference (address)

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

Function Calls and Argument Passing (continued) • C++ supports reference parameters • C++ example

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

Inline Functions • A function can have the code for its body included with

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 > 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

Command-line Arguments • The main function can receive arguments when invoked • Implemented as

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) argv[0] = pointer to command line name as string argv[n] = pointer to command line argument as string • Example lowercase input. dat output. dat // lowercase program in text CMSC 202 5

Vector Class Example // File: Vector. h class Vector { public: Vector() {} //

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

Vector Class Example (continued) // File: Vector. C #include “Vector. h” #include <iostream. h>

Vector Class Example (continued) // File: Vector. C #include “Vector. h” #include <iostream. h> Vector: : Vector(float a, float b) { // constructor overloaded x = a; y = b; } Vector: : operator -(Vector a) { // operator overloading Vector tmp; tmp. x = x - a. x; tmp. y = y - a. y; return(tmp); } CMSC 202 7

Vector Class Example (continued) float Vector: : inner(Vector a) { return(x * a. x

Vector Class Example (continued) float Vector: : inner(Vector a) { return(x * a. x + y * a. y); } int Vector: : nonzero() { return(x != 0. 0 || y != 0. 0) } void Vector: : display() { cout << “(“ << x << “, “ << y << “)”; } CMSC 202 8

Default Constructors • A constructor with no arguments is called a default constructor. •

Default Constructors • A constructor with no arguments is called a default constructor. • Usually used to set the attributes equal to default values Vector: : Vector() { 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

Operator Overloading • Predefined operators can be extended to operate on different types of

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: : operator -(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

C++ I/O Streams • fstream is a predefined class in <fstream. h> • fstream

C++ I/O Streams • fstream is a predefined class in <fstream. h> • fstream represents a stream I/O class • General format fstream-name(disk-filename, mode); • Examples fstream in. Data(“mydata”, ios: : in); fstream out. Data(“results”, ios: : out); in. Data >> x; out. Data << x; in. Data. close(); out. Data. close(); CMSC 202 // read a value from input file // send value to output file // done by default on some systems 11

Error Handling • Catch all errors that you can! • cerr is a predefined

Error Handling • Catch all errors that you can! • cerr is a predefined output stream used for error messages • Example fstream myin(file, ios: : in); if (myin. fail()) { // fail is a member function of fstream cerr << “Can’t open “ << file << ‘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 <stdlib. h>. CMSC 202 12

Student Exercise • Copy the code for program lowercase. C (pp. 67 -68) from

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 << “Usage: lower infile outfilen”; //incorrect cerr << “Usage: “ << argv[0] << “ infile outfilen”; • Create an appropriate input file. • Run the program with no arguments, one argument (error), two arguments, and three arguments (error). CMSC 202 13