EEL 3801 - PowerPoint PPT Presentation

About This Presentation
Title:

EEL 3801

Description:

EEL 3801 C++ as an Enhancement of C – PowerPoint PPT presentation

Number of Views:125
Avg rating:3.0/5.0
Slides: 31
Provided by: Lot96
Learn more at: http://www.cs.ucf.edu
Category:

less

Transcript and Presenter's Notes

Title: EEL 3801


1
EEL 3801
  • C as an Enhancement of C

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 gtgt to serve the same
    purpose of scanf()in C.
  • gtgt 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 ltiostream.hgt include file.

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

6
Example
  • include ltiostream.hgt
  • main()
  • cout ltlt Enter your age
  • int my_age
  • cin gtgt my_age
  • if (my_age lt 18)
  • cout ltlt you are a minor
  • else
  • cout ltlt You are getting old!

7
Another Example
  • include ltiostream.hgt
  • main()
  • cout ltlt Enter two integers
  • int x,y
  • cin gtgt x gtgt y
  • cout ltlt The sum of ltlt x ltlt and
  • ltlt y ltlt is ltlt xy ltlt \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 lt 5 i)
  • cout ltlt i ltlt \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 are not
    required.
  • typedef also not necessary - it is implicit.

11
Example
  • struct Name
  • char first10
  • char last10
  • 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 ltiostream.hgt
  • inline float cube(const float s)
  • return sss
  • main()
  • cout ltlt The answer is ltlt 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 ltiostream.hgt
  • void square_by_reference(int )
  • main()
  • int z 4
  • cout ltlt z ltlt z
  • square_by_reference(z)
  • cout ltlt Z is now ltlt 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() gt same as area(1,1)
  • area(5) gt same as area(5,1)
  • area(5,5) gt 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 ltiostream.hgt
  • float value 1.2345
  • main()
  • int value 7
  • cout ltlt Local value ltlt value
  • ltlt Global value ltlt 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 ltiostream.hgt
  • int square(int x) return x x
  • float square(float y) return y y
  • main()
  • cout ltlt square(7) ltlt \n
  • cout ltlt square(7.5) ltlt \n
Write a Comment
User Comments (0)
About PowerShow.com