1 / 13

C++ Overview, Part II (continued)

C++ Overview, Part II (continued). Reference: Chapter 2, Sections 2.2-2.9. CMSC 202 1. Function Calls and Argument Passing.

stokesr
Download Presentation

C++ Overview, Part II (continued)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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 <iostream.h> Vector::Vector(float a, float b) { // constructor overloaded x = a; y = b; } Vector Vector::operator -(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 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

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

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

  11. C++ I/O Streams • fstream is a predefined class in <fstream.h> • fstream represents a stream I/O class • General format • fstream stream-name(disk-filename, mode); • Examples • fstream inData(“mydata”, ios::in); • fstream outData(“results”, ios::out); • inData >> x; // read a value from input file • outData << 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, 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

  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 << “Usage: lower infile outfile\n”; //incorrect • cerr << “Usage: “ << argv[0] << “ 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

More Related