1 / 30

EEL 3801

C++ as an Enhancement of C. EEL 3801. Comments. Can be done with // at the start of the commented line. The end-of-line terminates the comment. Cannot be used for more than one line. The C comment indicator ( /* ... */ ) can be used also if comment is to be for longer than one line.

jsamsel
Download Presentation

EEL 3801

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++ as an Enhancement of C EEL 3801

  2. Comments • Can be done with // at the start of the commented line. • The end-of-line terminates the comment. • Cannot be used for more than one line. • The C comment indicator ( /* ... */) can be used also if comment is to be for longer than one line.

  3. Stream Input • C++ uses the standard input stream cin and the stream extraction operator >> to serve the same purpose of scanf()in C. • >> also called the get from operator. • Does not require format strings and conversion specifiers.

  4. Stream Input • C++ knows what is the type of the data being read because the variable has been declared already. • The variable name to which the value read will be assigned does not need to be preceded by the & operator. • Must include the <iostream.h> include file.

  5. Stream Output • C++ uses the standard output stream cout and the stream insertion operator << to serve the same purpose of printf()in C. • << also called the put to operator. • Other than that, it has the same basic features as the cin stream and the >> operator.

  6. Example #include <iostream.h> main() { cout << “Enter your age:” ; int my_age; cin >> my_age; if (my_age < 18) cout << “you are a minor”; else cout << “You are getting old!”; }

  7. Another Example #include <iostream.h> main() { cout << “Enter two integers:” ; int x,y; cin >> x >> y; cout << “The sum of “ << x << “and “ << y << “ is “ << x+y << ‘\n’; }

  8. Declarations • In C, all variable declarations must appear before any executable statements. • In C++, that is not the case. • Variables can be declared anywhere in the program as long as they precede the statement where the declared variable is first used. • See previous example.

  9. Declarations • The variables can even be declared inside a loop specification parenthesis. for (int i = 0; i <= 5; i++) { cout << i << ‘\n’; }

  10. Creating New Data Types • Like in C, enum, struct or union data structures are defined as models. • Unlike C, when an instance is declared, a data type is being automatically created. • The keywords struct, enum or union arenot required. • typedef also not necessary - it is implicit.

  11. Example struct Name { char first[10]; char last[10]; }; Name x; No need to use typedef or to say: struct Name x;

  12. Function Prototypes • Unlike in C, functions prototypes are required. • When the function is defined prior to its use - the header serves as the prototype • C++ uses the prototype for type checking. • If nothing is placed within the parenthesis of the function prototype, C++ interprets that as void. C interprets as turning off checks.

  13. Inline Functions • Similar in nature to preprocessor macros, but with some significant differences: • type checking is done in inline functions • no unexpected side effects as in macros • can be debugged with a debugger. • Only advises the compiler to replace the code segment in the text itself. • Only done by compiler for small functions.

  14. Inline Functions • Otherwise, used as a regular C++ function. • If inline function is changed, all files in the program that use it must be recompiled. #include <iostream.h> inline float cube(const float s) { return s*s*s;} main() { cout << “The answer is” << cube(4); }

  15. Reference Parameters • Permit call by reference without using pointers. • A reference parameter is an alias for its corresponding argument. • Uses the & operator just as * is for pointers. • All operations on the reference parameter are actually performed on the original variable itself.

  16. Reference Parameters • Do not have to be used only in function calls. int count = 1; //declare integer variable int &c = count;// c is alias for count c++; // increments count using its alias • For efficiency if argument is large, non-modifiable parameters can be passed to functions as references to constant variables

  17. Reference Parameters #include <iostream.h> void square_by_reference(int &); main() { int z = 4; cout << “z = “ << z; square_by_reference(z); cout << Z is now = “ << z; } void square_by_reference(int &cref) { cref = cref * cref; }

  18. Reference Parameters - Caveats • Reference variables must be initialized in their declarations. • Cannot be reassigned as aliases to other variables. • Cannot be de-referenced using * • Cannot be used to perform pointer arithmetic.

  19. Reference Parameters - Caveats • Can point to references, but you are actually pointing at the variable for which the reference is an alias. • Cannot compare references for the same reason. • Cannot get the address of a reference (same) • When returning reference (or a pointer) to a variable defined in function, make it static.

  20. Constant Variables • The const keyword can be used to declare constant variables in a function. • Better than using the #define pre-processor directive. • Such variables cannot be changed. const float PI = 3.14159; • Constant variable must be initialized at declaration.

  21. Constant Variables • Placing const in array declaration is illegal in C, but legal in C++. • Visible to debugger, while #define directives are not. • Pointers can also be declared as constant with the const operator.

  22. Dynamic Memory Allocation • C++ Uses new and delete in lieu of malloc() and free. • Already saw that.

  23. Default Arguments • Values can be specified for the parameters so that if an argument is not supplied, the default value is used by the function. • Must be the leftmost arguments in the parameter list. • Can be done with an inline function also.

  24. Default Arguments int area(int length = 1, int width = 1) { return length * width; } Can be called in the following ways: area() ==> same as area(1,1) area(5) ==> same as area(5,1) area(5,5) ==> same as area(5,5)

  25. Scope Resolution Operator • In C and C++, variables of the same name can be declared when one is global and the other local. • References to a variable while the local is in scope, refer to the local variable. • In such cases, the global variable is “invisible” and cannot be referenced.

  26. Scope Resolution Operator • Scope resolution operator :: permits such a global variable to become visible where the local variable of the same name is in scope. • Cannot be used to refer to another variable of the same name in another function - just global variables. • In general, using two variables of the same name is not good programming practice.

  27. Scope Resolution Operator #include <iostream.h> float value = 1.2345; main() { int value = 7; cout << “Local value = “ << value << “Global value = “ << ::value ‘\n’; }

  28. Function Overloading • In C, it is not legal to have two functions of the same name in the same program. • That is not the case in C++ as long as the functions have different sets of parameters. • Even the same parameters but of a different type is sufficient to distinguish them. • This is called function overloading.

  29. Function Overloading • When overloaded function called, the C++ compiler selects the proper function by inspecting the order, types and number of arguments. • Typically used to write common functions that do the same things to different data types.

  30. Function Overloading #include <iostream.h> int square(int x) { return x * x; } float square(float y) { return y * y; } main() { cout << square(7) << ‘\n’; cout << square(7.5) << ‘\n’; }

More Related