1 / 31

Programming II

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

ebony
Download Presentation

Programming 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. Programming II You gotta be cool 

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

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

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

  5. Simple program that you already know  with C

  6. ……and same results with C++ 

  7. ……and same results with C++ 

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

  9. Comparison

  10. Standard Library Revisited **Homework: find out what all these header files do. **Terms and Condition applied 

  11. The using statement Remember that when you use the function from iostream, you have to keep the std:: over and over again?

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

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

  14. Inline functions

  15. Keywords common in C and C++

  16. Keywords only in C++

  17. 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!!!!!

  18. The other technique: Reference Parameters

  19. The other technique: Reference Parameters

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

  21. Default Arguments and Empty Parameter Lists Default Arguments Empty Parameter Lists

  22. Unary Scope Resolution Operator Global Variable Local Variable

  23. Function Overloading First Variation Second Variation Third Variation

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

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

  26. Function Template

  27. Function Template

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

  29. So you guys and gals understand?

  30. Good….. Quiz 1 NOW!!!

More Related