1 / 23

Introduction to C++ (II)

B Smith: Fall 05, rate 2. Redundant and wordy. Class jointly created a hello world .cpp and ran. Discuss using namespace; and std::cout. Introduction to C++ (II). Math 130. Administrivia. Project 5 due date Extra credit: Grading Labs. Overview. C++ Input and Output Function Enhancements

grace
Télécharger la présentation

Introduction to C++ (II)

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. B Smith: Fall 05, rate 2. Redundant and wordy. Class jointly created a hello world .cpp and ran. Discuss using namespace; and std::cout Introduction to C++ (II) Math 130

  2. Administrivia • Project 5 due date • Extra credit: • Grading Labs

  3. Overview • C++ Input and Output • Function Enhancements • Function Calls and Default arguments • Function Overloading • Function Templates

  4. B Smith: Discuss stream objects cin.???, maybe cin.<<() to indicate that it’s a special type of function. It also jumps out that this is not procedural, and that overloading operators is standard order of business Input and Output in C++ • printf() and scanf()still available in C++ • You won’t want to use them • Standard I/O in C++ use stream I/O • In C, the streams (in addition to files) are • stdin • stdout • stderr • In C++, the standard streams are • cin • cout • cerr

  5. variable/ constant standard output device cout << standard input device cin variable >> coutandcin • Input and Output streams of C++ • cout – enables the system to write to the screen • cin – enables the system to read from the keyboard

  6. B Smith: To use the book’s code, change the warning level during compilation to ignore deprecated headers warning cout • The object type need not be specified! • no %s, %d, etc • C++ uses the “inserter” or “put to” operator, << B Smith: This is a grat opportunity to talk about an “implicit parameter.” Output to the terminal could be done using operator<<(cout, "hello"); vs cout << “hello”; Hence, when we are a ready to overload << for our purposes, we will understand better the prototype: operator<<(ostream&, “string”); #include <iostream> int main() { std::cout << "Hello there world!\n"; return 0; } The “put to” symbol std::cout << “The sum of 1 and 2 is” << (1 + 2) << ‘\n’;

  7. cin • Entering data is somewhat simplified in C++ • Note the “extractor,” or the “get from,” operator >> int main() { float num1, num2, product; cout << "Please type in a number: "; cin >> num1; cout << "Please type in another number: "; cin >> num2; product = num1 * num2; cout << num1 << " times " << num2 << " is " << product << endl; return 0; }

  8. Functions in C++ • The primary function enhancements over C: • default arguments • reference arguments (vs oft-troublesome pointers) • function and operator overloading • inline function compilation

  9. The prototype makes it clear that setStatus takes two integers as arguments. Also ok to use variable names. Function Enhancements - Default Arguments • In C, we learned to specify a function prototype #include<stdio.h> void setStatus ( int , int ) ; int stuID , stuStatus ; //global variables int main ( ) { printf( ‘‘ Enter student ID’’) ; scanf ( ‘‘%d’’, &stuID ) ; printf( ‘‘ Enter student status’’) ; scanf ( ‘‘%d’’, &stuStatus ) ; setStatus ( stuID , stuStatus ) ; } void setStatus ( int id , int stat) //func header { stuID = id ; printf( ‘‘ stuID i s %d’’ , id ) ; stuStatus = stat; printf( ‘‘ stuStatus i s %d’’ , stat) ; }`` The function implemented with no default arugments

  10. Function Enhancements • Default Arguments • C++ allows you to specify a function prototype that takes a variable number of arguments void setStatus ( int , int = 20 ) ; • If second argument is omitted, C++ uses 20 as the default argument

  11. Default Arguments • Consider this example function prototype: void func1 ( int , int = 5 , float = 6.78 ) ; • C++ compilers will consider the following calls valid: func1 ( 7 , 2 , 9.3 ) // no defaults used func1 ( 7 , 2 ) // same as func1( 7 , 2 , 6.78 ) func1 ( 7 ) // same as func1( 7 , 5 , 6.78 )

  12. Default Arguments – Rules • Only in function protototypes can function defaults be defined • If any argument is given a default value, those that follow it must also be given default values (think right justify) void func1 ( int , int = 5 , float = 6.78 ) ; // ok void func1 ( int = 0, int = 5 , float ) ; // error void func1 ( int = 0, int, float = 6.78 ) ; // error • If one argument is omitted, then all arguments to the left must be omitted • The default values in the prototype may be an expression consisting of constants and previously declared variables

  13. Reusing Function Names (Overloading) • In C, each function requires its own unique name • Consider taking the absolute value of a number; what would be your functions return value? • In <math.h> , you will see: double fabs( double ) • In <stdlib.h> , you will see: int abs( int ) • C++ allows us to create one function capable of working with various types or to perform multiple functions • Function overloading is the capability of C++ that allows one function to serve multiple, but typically similar, purposes

  14. Function Overloading void showAbs ( int x ) { // for int if ( x < 0 ) x = −x ; cout << “The absolute value is” << x << endl ; } void showAbs ( long x ) { // for long int if ( x < 0 ) x = −x ; cout << “The absolute value is” << x << endl ; } void showAbs ( double x ) { // for double if ( x < 0 ) x = −x ; cout << “The absolute value is” << x << endl ; }

  15. Function Overloading • Of the three showabs() functions, the one chosen depends on the argument(s) with which the function is called • The correct function is chosen based on type and number of arguments • Hence, the appropriate function is called in: int x = −23; showabs ( x ) ; • Function Overloading will be more useful later, c.f. constructor functions • A function template would be appropriate for our purposes here

  16. B Smith: Stopped here. Sp06. Function Templates • Consider again the task of finding the absolute value of a number of some arbitrary type • Below would be an appropriate template, but note the contrast • function header: void showAbs(int x) • template header: void showAbs(T number) template <class T> void showAbs (T number ) { if ( number < 0 ) number = −number ; cout << “The absolute value is” << number << endl ; return ; }

  17. Function Template • In the statement void showAbs(int x) • int represents an integer data type • In the statement void showAbs(T number) • T represents a generic data type • The compiler will will use the appropriate data type based on the function call it encounters

  18. B Smith: Stopped here on 4/20/05 Function Template Example #include <iostream> template <class T> void showtabs (T number ) { if ( number < 0) number = −number ; cout << "The absolute value of the number is " << number << endl ; return ; } int main ( ) { int num1 = −4; float num2 = −4.23; double num3 = −4.23456; showtabs (num1 ) ; showtabs (num2 ) ; showtabs (num3 ) ; return 0 ; }

  19. Function Templates • T is arbitrary and is a placeholder for a data type • It would’ve made just as much sense to use DTYPE • But, T is used widely by many programmers to respresent a generic data type template <class T> void showtabs (T number ) { . . . } template <class DTYPE> void showtabs (DTYPE number ) { . . . }

  20. #include <iostream> int abs(int value ) { int absnum; if ( value < 0) absnum = −value ; else absnum = value ; return absnum; } Function Templates w/ a Return Type #include <iostream> template <class T> T abs(T value ) { T absnum; if ( value < 0) absnum = −value ; else absnum = value ; return absnum; } int main ( ) { int num1 = −4; float num2 = −4.23; double num3 = −4.23456; cout <<"The absolute value of "<<num1 <<" is "<< abs (num1) << endl ; cout <<"The absolute value of "<<num2 <<" is "<< abs (num2) << endl ; cout <<"The absolute value of "<<num3 <<" is "<< abs (num3) << endl ; return 0 ; }

  21. Templates • The compiler substitutes the appropriate data type for the Tdata type in the function template • The following function is built for int datatypes: similar functions are created for the other dataypes int showabs ( int value ) { int absnum; if ( value < 0) absnum = −value ; else absnum = value ; return ( absnum) ; }

  22. B Smith: Class needed to see examples of when this would be useful, or just pull this one Multi-argument Templates • Finally, function templates for functions passing more than one argument: template <class T1 , class T2 , class T3>

  23. Summary • C++ Input and Output • should use over printf/scanf • typesafe (no need to worry about %s, %d, etc) • Function Enhancements • Function Calls and Default arguments • optional number of arguments in C++ • Function Overloading • functions with the same name can handle different data types • will be really useful for overloading operators • Function Templates • create one function to serve multiple data types

More Related