More about OOP and ADTs Classes - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

More about OOP and ADTs Classes

Description:

Focuses on the nouns of problem specification. Programmers: Determine objects ... Compiler compares numbers and types of arguments of overloaded functions ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 31
Provided by: Hong3
Category:
Tags: oop | adts | classes | more | nouns | of | types

less

Transcript and Presenter's Notes

Title: More about OOP and ADTs Classes


1
More about OOP and ADTsClasses
  • Chapter 4

2
Chapter Contents
  • 4.1 Procedural vs. Object-Oriented Programming
  • 4.2 Classes
  • 4.3 Example a First Version of Complex Class
  • 4.4 Class Constructors
  • 4.5 Other Class Operators

3
Contrast Procedural, Object Oriented Paradigms
  • Procedural
  • Action-oriented concentrates on the verbs
  • Programmers
  • Identify basic tasks to solve problem
  • Implement actions to do tasks as subprograms
    (procedures/functions/subroutines)
  • Group subprograms into programs/modules/libraries,
  • together make up a complete system for solving
    the problem
  • Object-oriented
  • Focuses on the nouns of problem specification
  • Programmers
  • Determine objects needed for problem
  • Determine how they should work together to solve
    the problem.
  • Create types called classes made up of
  • data members
  • function members to operate on the data.
  • Instances of a type (class) called objects.

4
Structs and Classes Similarities
  • Essentially the same syntax
  • Both are used to model objects with multiple
    attributes (characteristics)
  • represented as data members
  • also called data fields or attribute members
  • Thus, both are used to process non-homogeneous
    data sets.

5
Structs vs. Classes
  • No classes in C
  • Members public by default
  • Can be specified private
  • Both structs and classes in C
  • Structs can have members declared private
  • Class members are private by default
  • Can be specified public

6
Advantages in C(structs and Classes)
  • C structs and classes model objects which have
  • Attributes represented as data members
  • Operations represented as functions (or methods)
  • Leads to object oriented programming
  • Objects are self contained
  • "I can do it myself" mentality
  • They do not pass a parameter to an external
    function (operate on them)

7
Class Declaration
  • Syntaxclass ClassName public
    Declarations of public members private
    Declarations of private members

8
Designing a Class
  • Data members normally placed in private section
    of a class
  • the private members are accessible only by the
    member functions or friend functions
  • Function members usually in public section
  • the public members are accessible by any
    external members

9
A declaration for a complex class
  • class Complex
  • private
  • float real, imag
  • public
  • void assignReal(float)
  • void assignImag(float)
  • float getReal()
  • float getImag()
  • Complex add(Complex)
  • Complex subtract(Complex)
  • Complex multiply(Complex)
  • Complex divide(Complex)

10
Complex Class
  • Note interface for Complex class object,
  • Data members private inaccessible to users of
    the class
  • Information hiding, Implementation details are
    hided to users of the class

11
Class Libraries
  • Class declarations placed in header file
  • Given .h extension
  • Contains data items and function prototypes
  • Implementation file
  • Same prefix name as header file
  • Given .cpp extension
  • Programs which use this class library called
    client programs

12
Translating a Library
13
Member functions in class (1)
  • Ordinary member functions
  • ordinary functions defined with a return type
    (or void) and parameters
  • Constructors
  • Have the same name as the class
  • Do not return any values
  • Are invoked when an object of the class is
    declared and memory allocation is done at the
    constructor
  • There can be any number of overloaded
    constructors in a class (different sets of
    parameters)

14
Member functions in class (2)
  • Destructors
  • Have the same name as the class
  • They are preceded by a operator
  • Are invoked when the object of a class goes out
    of scope

If a constructor/destructor is not declared, the
compiler will automatically create a default one
for each. If we dont need to initialize any data
members to point to dynamically allocated memory
15
An member function implementation
  • The implementation of the assignReal() member
    function would appear in the definition file as
  • void ComplexassignReal(float re) real re
  • The double colon operator used in this
    definition is the scope resolution operator.

16
Inline function
  • Problem
  • the function call overhead may actually
    consume more time than the operations the
    functions are performing.
  • Solution Inline function
  • An inline function is declared by placing the
    keyword inline in front of the function
    declaration and definition.
  • If a function is declared inline, the compiler
    attempts to replace every call of that function
    with a copy of the entire function.

17
Inline function
  • the assignReal() member function could be
    rewritten by placing the following definition in
    the header file for the complex class
  • inline void Complexassign_real(float re) real
    re

18
Inline function
  • class Complex
  • private
  • float real, imag
  • public
  • inline void assignReal(float re) real re
  • inline void assignImag(float im) imag im
  • inline float getReal() return real
  • inline float getImag() return imag
  • // remaining members functions not shown
  •  

19
Variable declaration and others
Complex Complexadd (Complex) return
Complex(real num3.real, imag num3.imag)
  • Variables of type Complex can now be declared
  • Complex num1, num2, num3
  • creates three objects of type Complex called
    num1, num2, and num3.
  • These objects may be operated on by invoking
    their member functions.
  • num1.assignReal(3.2)
  • this statement uses the assignReal() member
    function to assign the value 3.2 to the member
    variable real.
  • num1 num2.add(num3)
  • This adds the complex numbers represented by num2
    and num3, and assigns the result to num1.

20
Constructors
  • Constructor specify what should happen when a new
    object is created.
  • It is invoked when a class object is declared, or
    when a class object is dynamically allocated
    using the new operator
  • SyntaxClassNameClassName (parameter_list)
    member_initializer_list // body of constructor
    definition

21
Add Constructor in class
  • class Complex
  • private
  • float real, imag
  • public
  • Complex() //default constructor
  • Complex(float re,float im)
  • //a constructor with initial value
  • // remaining member functions not shown

22
Constructor Definition
  • ComplexComplex() //default constructor
  • real(0), imag(0)
  • ComplexComplex(float re, float im)
  • realre
  • imagim

23
Constructor
  • Constructor can be called as below.
  • Complex num1 // num1.real0.0,num1.imag0.0
  • Complex num2(3.2) // num2.real3.2 num2.imag0.0
  • Complex num3(5.1,2.7)/num3.real5.1,
    num3.imag2.7/
  • three objects of type Complex are declared and
    initialized differently depending upon how the
    constructor is called.
  • (this is also an example of function overloading)

24
Constructor
  • If no constructor is given in a class, compiler
    will provide a default constructor, memory will
    still be allocated for each data member of an
    object by compiler (not dynamic allocation)

25
Overloading Functions
  • Note existence of multiple functions with the
    same nameComplex()
  • Complex(float re,float im)
  • Known as function overloading
  • Compiler compares numbers and types of arguments
    of overloaded functions
  • Checks the "signature" of the functions

26
Friend Functions
  • If a function declaration in a class is preceded
    by the keyword friend, then that function is said
    to be a friend of the class.
  • A friend function is not an actual member of the
    class however, it is still permitted to access
    the private data members of the class.

27
Friend Functions
  • Note - a friend function not member function
  • not qualified with class name and
  • Since they are not members of any class, you
    should not call them using the dot operator.

28
Pointers to Class Objects
  • Possible to declare pointers to class
    objectsComplex Ptr x
  • Access with Ptr-gtgetReal() or
    (Ptr).getReal()

29
The this Pointer
  • Every class has a keyword, this
  • a pointer whose value is the address of the
    object
  • Value of this would be the object itself

30
The this Pointer example
  • Complex Complexoperator(const Complex z)
  • real z.real
  • imag z.imag
  • return this
  • Invoke the function
  • Complex c1(1.1,2.2), c2
  • c2 c1
Write a Comment
User Comments (0)
About PowerShow.com