Class Inheritance, Template - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Class Inheritance, Template

Description:

A class has some types as data members. E.g. alphabetSet has an array 'Is a... Common components are grouped and implemented as a general base class ... – PowerPoint PPT presentation

Number of Views:62
Avg rating:2.0/5.0
Slides: 14
Provided by: raym168
Category:

less

Transcript and Presenter's Notes

Title: Class Inheritance, Template


1
Class Inheritance, Template STL
  • COMP103 Lab 12

2
Everything Could be A Class Object
3
Are Objects of Different Classes Completely
Separated?
  • Usually NO!
  • Different classes are somewhat related
  • E.g. Raymond has a bag
  • E.g. Raymond is a boy

4
Has a Is aRelationship
  • Has a
  • A class has some types as data members
  • E.g. alphabetSet has an array
  • Is a
  • A class is also another class
  • A derived class is inherited from another base
    class
  • A derived class has all the features of a base
    class

5
Inheritance
  • Syntax

// class definition class derivedClass public
baseClass public func()
... private int member1 //
constructor initializing base class and data
members derivedClassderivedClass()
baseClass(...), member1(2)
6
Inheritance
  • Data members and member functions of base class
    are brought to derived class
  • But visibility of base classs members to derived
    class is controlled by their access modifiers at
    base class
  • public/protected at base class gt visible to
    derived class
  • private at base class gt invisible to derived
    class
  • But not all functions are brought to derived
    class (which of them?)

7
Features of Inheritance
  • Common components are grouped and implemented as
    a general base class
  • More specific classes derive from base class
    which contain
  • Shared features from base class
  • Some specific features
  • Reuse of code using a base class

8
Class Template
  • A class with unspecified type
  • Provide generic data structure and functions on
    various data types

9
Writing Class Template
  • Put class definition and function member
    definitions in a single .h file
  • Syntax

templateltclass Tgt // class definition class A
public func(T para)
... templateltclass Tgt // function member
definition AltTgtfunc(T para) ...
10
Using Class Template
  • Declaring a variable
  • Altintgt variable1
  • Altintgt variable2(4)
  • Using member function
  • variable1.func(2)

11
Standard Template Library (STL)
  • A collection of common containers, algorithms and
    iterators
  • Allow users reuse the common items without
    writing the same codes repeatedly
  • Reference of STL
  • E.g. http//www.sgi.com/tech/stl/

12
Common Containers
  • Containers are data structures for storing data
    with various insertion/deletion/searching
    performance
  • Containers in STL are designed with similar
    function prototypes
  • Simplified programming
  • Ease for performance tuning
  • E.g. linked lists, vector, etc

13
Using STL
  • Include the corresponding library
  • Add the statement
  • using namespace std
Write a Comment
User Comments (0)
About PowerShow.com