300 likes | 321 Views
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.
E N D
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.
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.
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.
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.
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!”; }
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’; }
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.
Declarations • The variables can even be declared inside a loop specification parenthesis. for (int i = 0; i <= 5; i++) { cout << i << ‘\n’; }
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.
Example struct Name { char first[10]; char last[10]; }; Name x; No need to use typedef or to say: struct Name x;
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.
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.
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); }
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.
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
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; }
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.
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.
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.
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.
Dynamic Memory Allocation • C++ Uses new and delete in lieu of malloc() and free. • Already saw that.
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.
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)
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.
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.
Scope Resolution Operator #include <iostream.h> float value = 1.2345; main() { int value = 7; cout << “Local value = “ << value << “Global value = “ << ::value ‘\n’; }
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.
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.
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’; }