EEL 3801 C++ as an Enhancement of C Comments Can be done with // at the start of the commented line. The end-of-line terminates.

Download Report

Transcript EEL 3801 C++ as an Enhancement of C Comments Can be done with // at the start of the commented line. The end-of-line terminates.

EEL 3801
C++ as an Enhancement of C
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.
EEL 3801 – Lotzi Bölöni
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.
EEL 3801 – Lotzi Bölöni
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.
EEL 3801 – Lotzi Bölöni
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.
EEL 3801 – Lotzi Bölöni
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!”;
}
EEL 3801 – Lotzi Bölöni
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’;
}
EEL 3801 – Lotzi Bölöni
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.
EEL 3801 – Lotzi Bölöni
Declarations
The variables can even be declared inside
a loop specification parenthesis.
for (int i = 0; i <= 5; i++) {
cout << i << ‘\n’;
}
EEL 3801 – Lotzi Bölöni
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
are not required.
typedef also not necessary - it is implicit.
EEL 3801 – Lotzi Bölöni
Example
struct Name {
char first[10];
char last[10];
};
Name x;
No need to use typedef or to say:
struct Name x;
EEL 3801 – Lotzi Bölöni
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.
EEL 3801 – Lotzi Bölöni
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.
EEL 3801 – Lotzi Bölöni
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);
}
EEL 3801 – Lotzi Bölöni
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.
EEL 3801 – Lotzi Bölöni
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, nonmodifiable parameters can be passed to
functions as references to constant
variables
EEL 3801 – Lotzi Bölöni
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; }
EEL 3801 – Lotzi Bölöni
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.
EEL 3801 – Lotzi Bölöni
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.
EEL 3801 – Lotzi Bölöni
Constant Variables
The const keyword can be used to declare
constant variables in a function.
Better than using the #define preprocessor directive.
Such variables cannot be changed.
const float PI = 3.14159;
Constant variable must be initialized at
declaration.
EEL 3801 – Lotzi Bölöni
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.
EEL 3801 – Lotzi Bölöni
Dynamic Memory Allocation
C++ Uses new and delete in lieu of
malloc() and free.
Already saw that.
EEL 3801 – Lotzi Bölöni
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.
EEL 3801 – Lotzi Bölöni
Default Arguments
int area(int length = 1, int width = 1)
{
return length * width;
}
Can be called in the following ways:
same as area(1,1)
area(5) ==> same as area(5,1)
area(5,5) ==> same as area(5,5)
area() ==>
EEL 3801 – Lotzi Bölöni
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.
EEL 3801 – Lotzi Bölöni
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.
EEL 3801 – Lotzi Bölöni
Scope Resolution Operator
#include <iostream.h>
float value = 1.2345;
main()
{
int value = 7;
cout << “Local value = “ << value
<< “Global value = “ <<
::value
‘\n’;
}
EEL 3801 – Lotzi Bölöni
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.
EEL 3801 – Lotzi Bölöni
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.
EEL 3801 – Lotzi Bölöni
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’;
}
EEL 3801 – Lotzi Bölöni