320 likes | 406 Views
Programming II. You gotta be cool . C++ as a better C. C++ ? C++ Standard Library Header Files Inline Functions References and Reference Parameters Default Arguments and Empty Parameter Lists Unary Scope Resolution Operator Function Overloading Function Templates. Why C++.
E N D
Programming II You gotta be cool
C++ as a better C C++ ? C++ Standard Library Header Files Inline Functions References and Reference Parameters Default Arguments and Empty Parameter Lists Unary Scope Resolution Operator Function Overloading Function Templates
Why C++ Improves many of C features C is structured vs. C++ is Object Oriented (increasing software productivity, quality and reusability) The increment operator (++) indicates that C++ is an enhancement of C C++ is a superset of C, therefore C++ compiler can also compile existing C programs
Why C++ Can someone in this class be a “lady” or a “gentleman”? Help me here… Come in front and code a simple C program that adds up 2 numbers Shouldn’t be a problem right? Y’all have learned this in Programming I… kehkehkeh…..evil laugh :D
New things you need to know <iostream> - header files from standard library. To tell C++ preprocessor to include the contents of the input/ output stream header file iostream. coutand the << operator – standard output stream, using namespacestd (will be discussed in later chapters) cinand the << operator – standard input stream, using namespacestd (will be discussed in later chapters) std::endl – stream manipulator abbreviated from “end line” – outputs a new line and flushes the output buffer
Standard Library Revisited **Homework: find out what all these header files do. **Terms and Condition applied
The using statement Remember that when you use the function from iostream, you have to keep the std:: over and over again?
The using statement And of course you can shorten this too, where you eliminate the use of variable sum Use the using statement, we can eliminate the repetition of std:: in every statement
Inline functions In C, functions are good form a software engineering standpoint. However, function calls involve execution-time overhead. C++ provides inline functions to help reduce function-call overhead – especially for small functions. The qualifier inline before the function’s return type in the function definition “advises” the computer to generate a copy of the function’s code in place to avoid a function call. Trade off? Multiple copies of function code makes the program larger. inline qualifier is good for small functions only.
References and Reference Parameters Previously, you have learnt functions in 2 operations: call-by-value and call-by-reference, right? But do you know that there are 2 techniques for call-by-reference? Can someone demonstrate call-by-reference technique that you have learned in Programming I? Com’on!!!!!
The other technique: Reference Parameters $ can also be used as an alias for one other variable. However the alias must represent an existing variable and is only dedicated to that variable only for as long as the program is executed. Or else syntax error will occur.
Default Arguments and Empty Parameter Lists Default Arguments Empty Parameter Lists
Unary Scope Resolution Operator Global Variable Local Variable
Function Overloading First Variation Second Variation Third Variation
Function Template Overloaded functions are used to perform similar operations that involve different program logic on different data types However, if the program logic and operations are identical for each data type, function templates is more suitable C++ generates separate template function derived from a single function template definition, to handle each type of call appropriately
Function Template Each function template definitions begin with keyword template followed by a list of formal type parameters to the function template enclosed in angle brackets (< >) Then every formal type parameter is preceded by either keyword typename or class
Let’s try this ….just for fun Write a C++ program that calculates area for rectangles Use overloading functions with 2 varieties: one accepts 2 integers for height and width. The other accepts 2 floats for height and width Do not use any return value or pointers for either overloaded functions.
Good….. Quiz 1 NOW!!!