ADTs and C Classes - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

ADTs and C Classes

Description:

Reference Parameters. A reference parameter is declared by writing the type name followed by the ... Friend Functions. A friend function is a function that is ... – PowerPoint PPT presentation

Number of Views:10
Avg rating:3.0/5.0
Slides: 27
Provided by: scien63
Learn more at: https://www.mc3.edu
Category:

less

Transcript and Presenter's Notes

Title: ADTs and C Classes


1
ADTs and C Classes
  • Classes and Members
  • Constructors
  • The header file and the implementation file
  • Classes and Parameters
  • Operator Overloading

2
Classes and Members
  • In C the mechanism to create objects is called
    the class
  • Member Functions are the way we manipulate
    objects
  • Classes can support information hiding
  • Programs use classes without knowing how they are
    implemented

3
Class Members
  • C classes have Member Functions and Member
    variables
  • Classes can have a public section and a private
    section
  • Members in the public session can be seen from
    outside the class
  • Members in the private session can not be seen
    from outside the class

4
C class syntax (definition)
  • The class head
  • The member list
  • The public section
  • Member functions (Typically)
  • Member variables (Occasionally)
  • The private section
  • Member variables (Typically)
  • Member functions (Occasionally)

5
A common pattern for new classes
  • Public member functions permit programmers to
    modify and examine objects of the new class
  • Constant public members let a member function
    examine data (but not modify it)
  • Private member variables store information about
    the status of an object of the new class (not
    available outside the class itself)

6
Using a Class
  • To use a class, we create an instance of the
    object
  • Each instance has its own private data and its
    own member functions
  • We activate the member functions to tell the
    object to perform the behavior This is sometimes
    referred to as sending a message to the object

7
Implementing a Member Function
  • Member functions are attached to their respective
    classes with the
  • scope resolution operator ()
  • The operator is placed between the class name and
    the function name
  • A function definition looks like this
  • ltreturn typegt ltclass namegtltfunction name()gt
  • void myclassmyFunction()

8
The Key to Member Variables
  • Each object keeps its own copies of all member
    variables
  • When a member functions implementation refers to
    a member variable, then the actual member
    variable always comes from the object that
    activated the member function

9
Constructors
  • Provides an initialization function that is
    guaranteed to be called.
  • It is called every time an instance of the class
    is created.
  • It can have several different number of and type
    of parameters.
  • Since it operates inside a class, it does not
    have any return value (no return type allowed)

10
Constructor (cont)
  • A constructor with no parameters is called the
    default constructor
  • When a class has no constructor, the compiler
    automatically creates the automatic default
    constructor that simply calls the default
    constructor for member variables that are objects
    of other classes
  • You should never depend on the automatic default
    constructor

11
Parameters
  • Value parameters
  • Reference parameters
  • Constant reference parameters

12
Inline Member Functions
  • Placing a function definition inside a class
    definition is a called an inline member function
  • You dont have to write the implementation later
  • Inline functions can cause some inefficiency,
    result in messier class definitions and should
    only be used when the function consists of a
    single short statement

13
Header and Implementation Files
  • A header file provides all the information that a
    programmer needs in order to use the class
  • An implementation file that provides the actual
    implementations of the classs member functions

14
Compiler Directive
  • A compiler directive in the header file, called a
    macro guard prevents duplicate class definitions
  • ifndef ltclass identifiergt or (!defined)
  • define ltclass identifiergt
  • ltclass definitiongt
  • endif

15
Value Semantics of a Class
  • The assignment operator copies the values for
    each member variable. Called the automatic
    assignment operator. Do not work under certain
    situations (discussed later on )
  • A copy constructor is a constructor with exactly
    one argument that is the same type as the
    constructors class that creates a new object by
    coping an object of the same type default is
    automatic copy constructor

16
The Constant Declaration
  • A (usually) global definition of a constant that
    can not change while the program is running
  • A more meaningful way to express a value used
    throughout a program
  • Localizes the change of the constant to a single
    point in the program

17
Classes and Parameters
  • When functions communicate, they do so by using
    parameters. Understanding the use of parameters
    is essential for writing correct programs.
  • Default arguments can be listed in the function
    prototypes of any function.
  • A default constructor may be implemented with
    default arguments for all of its arguments.

18
Parameters
  • The function parameter is referred to as the
    formal parameter.
  • The passed value is the argument, also called the
    actual parameter or the actual argument.

19
Value Parameters
  • A value parameter is declared by writing the type
    name followed by the parameter name. A local copy
    is made for use by the function. Data passed into
    a value returning function are always value
    parameters. Some values passed into a void
    function may be value parameters

20
Reference Parameters
  • A reference parameter is declared by writing the
    type name followed by the character and the
    parameter name. Changes made to the formal
    parameter will change value of the actual
    parameter in the calling function. Use to pass
    data out of a function. data may originate in
    the function or be changed from data passed into
    the function

21
Constant Reference Parameters
  • A constant reference parameter is a reference
    parameters declared with the keyword const before
    the reference parameter. The compiler will not
    allow code to be written that would attempt to
    alter the value of the argument.

22
Operator Overloading
  • C lets you define the meaning of many operators
    for a new class
  • This new definition is called overloading the
    operator
  • The overloaded function has the name operator
    ltoperator symbolgt
  • e.g. operator overloads the equivalence
    operator

23
Overloading Input and Output Operators
  • The return type is called a reference return type
    which permits chaining of output statements
  • The function returns the same stream it has just
    written (read)

24
Friend Functions
  • A friend function is a function that is not a
    member function, but still has access to the
    private member variables
  • The functions prototype is proceeded by the
    keyword friend
  • Friendships should be limited to functions that
    are written by the programmer who implemented the
    class

25
Summary
  • Object-oriented programming supports information
    hiding by placing data in packages called
    objects.
  • Objects are manipulated by member functions
    defined along with their classes.

26
Summary
  • Abstract Data Types (ADTs) are programmer defined
    data types. The term abstract emphasizes that the
    behavior of the ADT is what the user is concerned
    with.
  • We create objects that exhibit the behavior of
    the problem we are trying to solve.
Write a Comment
User Comments (0)
About PowerShow.com