Ch 12. Features Found Only in C PowerPoint PPT Presentation

presentation player overlay
About This Presentation
Transcript and Presenter's Notes

Title: Ch 12. Features Found Only in C


1
Ch 12. Features Found Only in C
  • Timothy Budd
  • Oregon State University

2
Introduction
  • C includes a number of features that have no
    equivalent in Java.
  • Table 12.1 summarizes these features.

3
Table 12.1 Features Found in C but not in Java
4
Global Variables
  • Global variable a variable that is declared
    outside the scope of any class or function.
  • int top 0 // global variable, top of
    stackint data100 // data area for stackint
    push(int val) datatop val int pop()
    return datatop--
  • Can be declared static and accessible only within
    the file in which they are declared.

5
Global Variables
  • A global variable used in two or more files must
    be declared as extern in all but one of the
    files.
  • A value declared as extern cannot at the same
    time be initialized
  • extern int top
  • // top is declared and initialized in another
    file
  • Whenever possible, avoid the use of global
    variables.

6
The Preprocessor
  • Initial step prior to analysis by the compiler.
  • The text of a C program is scanned and
    directives that begin with a hash mark are
    processed.
  • Major categories are
  • include ltfilenamegt define name value
    undef name if test ifdef name ifndef
    name else endif

7
The Preprocessor
  • Include files are used to incorporate header
    files that are used by many files within a
    project or by many different projects.
  • include ltcstdiogt // include standard I/O
    library include "rational.h" // include my own
    header file
  • The include directive differs from the import
    statement
  • Not a statement and does not end with a
    semicolon.
  • Performs a textual inclusion, which is not true
    for Java

8
The Preprocessor
  • A define directive creates a symbolic constant.
    Associating the given text with the name
  • // limit of twelve elements define MAX 12
  • Do not use the preprocessor to define symbolic
    constants.
  • const int max 12 // limit of twelve elements

9
The Preprocessor
  • A common idiom is found in header files to
    prevent the contents of file from being scanned
    by the compiler twice, should the file happen to
    be included more than one time.
  • // see if file has already been scanned
    ifndef FOOCLASSDEFINITION // if not, then
    define symbol now, so later // inclusions will
    skip this file define FOOCLASSDEFINITION ...
    // contents of file endif

10
typedef Statement
  • Use typedef to give a contextually meaningful
    name to an existing data type.
  • typedef double fahrenheit // define alias for
    double fahrenheit currentTemp // declare a new
    variable
  • Two types that expand to the same base are
    considered the same
  • currentTemp 2.3 // can combine with
    doublestypedef double celsius // define new
    aliascelsius newTemp 42 // declare another
    variablecurrentTemp newTemp // can combine,
    both doubles

11
typedef Statement
  • typedef statements are often used to eliminate
    confusing syntax.
  • int (fun)(char )10
  • typedef int ( funPtr)(char ) // funPtr -
    pointer to a functionfunPtr fun10 // declare
    array of pointers to functions

12
typedef Statement
  • typedef statements are also frequently used to
    shorten compound type names.
  • map lt const char , map lt const char , unsigned
    int, charCompare gt, charCompare gt aGraph
  • typedef map lt const char , unsigned int,
  • charCompare gt cityInfotypedef map ltconst char
    , cityInfo, charComparegt graphgraph aGraph
    // declare a new graph value

13
The const Keyword
  • Used to define a quantity that does not change.
  • Applied to variables, it provides a way of
    creating symbolic constants
  • const int maxCharCount 80 // upper limit on
    character
  • Applied to an object value, it indicates that the
    object will not changed in any fashion stronger
    than the Java notion of final, only the object
    will not be reassigned.

14
The const Keyword
  • Applied to arguments, particularly argument
    by-reference arguments, it indicates that the
    value will not be modified inside the function.
  • int foo (const box aBox) // contents of
    aBox cannot be changed ... aBox.value 17
    // ERROR! compiler will issue error
    message ...

15
The const Keyword
  • Applied to a member function, the keyword
    indicates that the function does not make a
    change to the receiver class string
    public ... // computing length does not
    alter string int length() const ...
  • Only member functions that have been declared as
    can be used with objects that are themselves
    declared const.

16
The const Keyword
  • When applied to a pointer, the const keyword
    either indicates that the pointer itself is
    constant, or that the value it references is
    constant, depending on the placement
  • int i 7 const int p i // pointer to
    a constant int const q i // constant
    pointer p 8 // not allowed, p points to a
    const q 8 // allowed, q is pointing to non
    const p q // allowed, p itself is not
    constant q p // not allowed q is
    constant const int const r i // both
    aspects are constant

17
The const Keyword
  • Casting away const is using a cast operator to
    remove the const restriction.
  • const char name "Fred" char p
    static_castltchar gt(name)
  • // throw away the const part
  • p0 'D' // name is now Dred
  • The use of cast operation such as static_cast and
    dynamic_cast is preferable to the traditional
    cast syntax.

18
Default Arguments
  • The use of default arguments can greatly reduce
    the amount of code in a class.
  • class box public box (int v 7) val(v)
    void test (int v 3, double d) val v
    (int) d private int valbox aBox(3)
    // explicit argumentbox bBox // uses default
    value of 7aBox.text(2, 3.14) // both arguments
    specifiedbBox.test(2.3) // uses implicit
    definition for first argument

19
Default Arguments
  • Java programmers sometimes achieve the same
    effect by having one procedure simply invoke
    another of the same name
  • class box public box () box(7) // invoke
    myself public box (int v) val v public
    void test (int v, double d) val v (int)
    d public void test (double d) // use more
    general method test(3, d) private int
    val

20
Friends
  • A friend is a function or class that is permitted
    to access the internal state of another class.
  • class box public box (int v) value(v)
    private int value // declare plus
    function as friend friend int plus(const box ,
    const box )

21
Friends
  • int plus (const box left, const box right)
    // access to private data field value // is
    permitted, since plus is declare // as
    friend return left.value right.valueint
    minus (const box left, const box right) //
    ERROR will produce an error message // since
    access to private data field // is not
    permitted return left.value - right.valuebox
    a(3), b(4)int i plus(a, b)

22
Friends
  • Following program illustrates how to define a
    class that is guaranteed to have only a single
    instance
  • class singletonBox
  • public int value private singletonBox
    () value(0) friend theBox() const
    singletonBox theBox() // declare the one and
    only one box static singletonBox actualBox //
    return this singleton object return actualBox

23
Name Spaces
  • A programmer can explicitly declare that all the
    functions and definitions in one part of a
    program are defined within a certain name space
  • // declare everything in name space
    stacknamespace StackADT int height class
    Stack ...

24
Name Spaces
  • Names defined inside a namespace can be used
    outside the namespace description.
  • Use a fully qualified name, similar to a fully
    qualified method name // read the value of the
    height variable int max StackADTheight
  • Alternatively, all the names defined in a name
    space can be included in a program using a single
    statement using StackADT
  • // include all the names defined in the
    namespace

25
Multiple Inheritance
  • In C, a class can inherit from two or more
    parent classes.
  • Avoid the use of multiple inheritance.

26
Example of Multiple Inheritance
  • class GraphicalDeck
  • public CardDeck, public GraphicalObject
    public void draw (Window w) // draw image on
    window GraphicalObjectdraw(w) Card
    draw () // draw card from deck return
    CardDeckdraw() ...

27
Example of Multiple Inheritance
  • class GraphicalDeck
  • public CardDeck, public GraphicalObject
    public void draw () // draw image on
    window GraphicalObjectdraw() Card
    getCard () // draw card from deck return
    CardDeckdraw() ...

28
Example of Multiple Inheritance
  • class A ...private int dataField
  • class B public A ...
  • class C public A ...
  • class D public B, public C ...// question
    how many dataFields does this have?

29
Example of Multiple Inheritance
  • class A ...private int dataField
  • class B virtual public A ...
  • class C virtual public A ...
  • class D public B, public C ...

30
Example of Multiple Inheritance
  • class A public // constructor A (int d)
    dataField(d) ... private int dataField
  • class B virtual public A public B (int b)
    A(7) // initialize A with 7 ...
  • class C virtual public A public C (int c)
    A(11) // or, initialize A with 11 ...
  • class D public B, public C public // Nope,
    neither, initialize A with 42 D () A(42),
    B(12), C(22) ...

31
goto Statement
  • Avoid using a goto statement, except to break out
    of nested loops.
  • int test (int i) // example of bad code
  • i i 2 tst if (i gt 10) ret return i
    2 i i - 3 if (i lt 1) goto ret i i
    5 goto tst

32
goto Statement
  • One of the most common uses for the goto
    statement is to break out of multiple levels of
    looping, something that in Java is performed
    using a break
  • static final int gold 1static final int
    dross 0int field100100...searchForGold
    // a Java search of a doubly nested loopfor
    (int i 0 i lt 100 i) for (int j 0 j lt
    100 j) if (fieldij GOLD) break
    searchForGold // ... execution comes here on
    break

33
goto Statement
  • The same search could be written in C as
    follows
  • // a C search of a doubly nested loopfor (int
    i 0 i lt 100 i)
  • for (j 0 j lt 100 j) if (fieldij
    GOLD) goto FoundGoldfoundGold // ...
    execution comes here on found value
Write a Comment
User Comments (0)
About PowerShow.com