C %20Review%20(4) - PowerPoint PPT Presentation

About This Presentation
Title:

C %20Review%20(4)

Description:

C++ Review (4) Inheritance and Overloading. Pointers, Virtual Functions, and Template – PowerPoint PPT presentation

Number of Views:91
Avg rating:3.0/5.0
Slides: 42
Provided by: nwa74
Category:

less

Transcript and Presenter's Notes

Title: C %20Review%20(4)


1
C Review (4)
  • Pointers, Virtual Functions, and Template

2
Templates
3
Function Overloading
  • C supports writing more than one function with
    the same name but different argument lists. This
    could include
  • different data types
  • different number of arguments
  • The advantage is that the same apparent function
    can be called to perform similar but different
    tasks. The following will show an example of
    this.

4
Function Overloadingdifferent data types
  • void swap (int a, int b)
  • int temp temp a a b b temp
  • void swap (float c, float d)
  • float temp temp c c d d temp
  • void swap (char p, char q)
  • char temp temp p p q q temp

5
Function Overloading
  • void swap (int a, int b)
  • void swap (float c, float d)
  • void swap (char p, char q)
  • int main ( )
  • int a 4, b 6
  • float c 16.7, d -7.89
  • char p 'M' , q 'n'
  • swap (a, b)
  • swap (c, d)
  • swap (p, q)

6
Templates
  • Can we write code without being tied to
    particular type.
  • Yes template
  • But how?
  • Question How do you swap two elements of any
    type?

7
Templates
  • Function Templates
  • Class Templates
  • Syntax for templates
  • template ltclass Typegt
  • Declaration
  • Where type is the name of a data type, build-in,
    or user defined.
  • Declaration is either function declaration or
    class declaration.
  • Class refers to any user-defined or build-in type.

8
Function templates
  • Template ltclass Typegt
  • void swap (Type a, Type b)
  • Type temp
  • temp a
  • a b
  • b temp

9
Class templates
  • template ltclass elemTypegt
  • class listType
  • Private
  • elemType list100
  • int length
  • Public
  • bool search(const elemType searchItem) const
  • viod insert(const elemType newElement)

10
Pointer
11
Pointers
  • A pointer is a variable that holds the address of
    something else.
  • datatype identifier

foo
123
int foo int x foo 123 x foo
x
3
12
Pointer variables
  • Declaring pointers of simple data type
  • int pInt
  • char pCh
  • double pNum
  • Declaring pointers of structured data type
  • Datatype pointerVariable
  • Declaring pointers of pointer data type
  • int p

13
Pointer to Structs
  • struct studentType
  • string name
  • int age
  • double gpa
  • studentType aStudent
  • studentType pStudent
  • pStudent aStudent
  • /pStudent aStudent/
  • aStudent.name Nan Wang
  • (pStudent).age 18
  • pStudent-gtgpa 4.0
  • -gt member access operator arrow

14
Pointer to Classes
  • class studentType
  • public
  • string name
  • int age
  • double gpa
  • void print() const
  • studentType aStudent
  • studentType pStudent
  • pStudent aStudent
  • /pStudent aStudent/
  • aStudent.name Nan Wang
  • (pStudent).age 18
  • pStudent-gtgpa 4.0
  • pStudent-gtprint()
  • -gt member access operator arrow

15
Passing pointers as parameters
  • void swap( int x, int y)
  • int tmp
  • tmp x
  • x y
  • y tmp

16
  • Dynamic Objects

17
Memory Management
  • Static Memory Allocation
  • Memory is allocated at compilation time
  • Dynamic Memory
  • Memory is allocated at running time

18
Static vs. Dynamic Objects
  • Dynamic object
  • Memory is acquired by program with an allocation
    request
  • new operation
  • Dynamic objects can exist beyond the function in
    which they were allocated
  • Object memory is returned by a deallocation
    request
  • delete operation
  • Static object
  • (variables as declared in function calls)
  • Memory is acquired automatically
  • Memory is returned automatically when object goes
    out of scope

19
Memory Allocation
new delete
int a200
int ptr ptr new int200 delete ptr
20
Object (variable) creation New
  • Syntax
  • ptr new SomeType
  • where ptr is a pointer of type SomeType

Example
int p new int
21
Object (variable) destruction Delete
  • Syntax
  • delete p
  • storage pointed to by p is returned to free
    store and p is now undefined

Example
int p new int p 10 delete p
10
p
22
Array of New dynamic arrays
  • Syntax
  • P new SomeTypeExpression
  • Where
  • P is a pointer of type SomeType
  • Expression is the number of objects to be
    constructed -- we are making an array
  • Because of the flexible pointer syntax, P can be
    considered to be an array

23
Example
  • Dynamic Memory Allocation
  • Request for unnamed memory from the Operating
    System
  • int p, n10p new int
  • p new int100

p
new
p
new
p new intn
p
24
Memory Allocation Example
  • Want an array of unknown size

int main() cout ltlt How many students?
cin gtgt n int grades new
intn for(int i0 i lt n i)
int mark cout ltlt Input Grade for
Student ltlt (i1) ltlt ? cin gtgt
mark gradesi mark . .
. printMean( grades, n ) // call a function
with dynamic array . . .
25
Freeing (or deleting) Memory
26
Virtual function
27
(No Transcript)
28
(No Transcript)
29
(No Transcript)
30
Virtual Functions
  • A virtual function is a function whose behavior
    can be overridden within an inheriting class by a
    function with the same signature.
  • virtual void eat() const stdcout ltlt "I eat
    like a generic Animal." ltlt stdendl

31
(No Transcript)
32
output
33
Purpose of Virtual function
  • In OOP when a derived class inherits from a base
    class, an object of the derived class may be
    referred to (or cast) as either being the base
    class type or the derived class type. If there
    are base class functions overridden by the
    derived class, a problem then arises when a
    derived object has been cast as the base class
    type. When a derived object is referred to as
    being of the base's type, the desired function
    call behavior is ambiguous.

34
  • The distinction between virtual and not virtual
    resolves this ambiguity. If the function in
    question is designated "virtual" in the base
    class then the derived class's function would be
    called (if it exists). If it is not virtual, the
    base class's function would be called.
  • Virtual functions overcome the problems with the
    type-field solution by allowing the programmer to
    declare functions in a base class that can be
    redefined in each derived class.

35
Abstract Class
36
Abstract classes
  • Sometimes, we want to have a class to represent
    something, but we dont want anyone to actually
    create an instance of that class.
  • We do this because we except people to inherit
    from our base class, and then create instances of
    the derived class.
  • An abstract class is a class that is designed to
    be specifically used as a base class.

37
Abstract Class
  • An abstract class contains at least one pure
    virtual function. You declare a pure virtual
    function by using a pure specifier ( 0) in the
    declaration of a virtual member function in the
    class declaration.

class AB public virtual void f() 0
38
Abstract Classes
  • An abstract class represents an abstract concept
    in C (such as Shape class)
  1. Defines the interfaces that all of the concrete
    classes (subclasses) share
  2. Does not define state and implementation unless
    it is common to all concrete classes
  3. Cannot be instantiated

39
(No Transcript)
40
Linear List As C Abstract Class
  • templateltclass Tgt
  • class linearList
  • public
  • virtual linearList()
  • virtual bool empty() const 0
  • virtual int size() const 0
  • virtual T get(int theIndex) const 0
  • virtual int indexOf(const T
    theElement)const 0
  • virtual void erase(int theIndex) 0
  • virtual void insert(int theIndex,
  • const T theElement)
    0
  • virtual void output(ostream out) const
    0

41
Extending A C Class
  • templateltclass Tgt
  • class arrayList public linearListltTgt
  • // code for all abstract methods of linearList
    must come here
  • class linkedList public linearListltTgt
  • // code for all abstract methods of linearList
    must come here
Write a Comment
User Comments (0)
About PowerShow.com