Title: Class Inheritance, Template
1Class Inheritance, Template STL
2Everything Could be A Class Object
3Are 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
4Has 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
5Inheritance
// class definition class derivedClass public
baseClass public func()
... private int member1 //
constructor initializing base class and data
members derivedClassderivedClass()
baseClass(...), member1(2)
6Inheritance
- 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?)
7Features 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
8Class Template
- A class with unspecified type
- Provide generic data structure and functions on
various data types
9Writing 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) ...
10Using Class Template
- Declaring a variable
- Altintgt variable1
- Altintgt variable2(4)
- Using member function
- variable1.func(2)
11Standard 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/
12Common 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
13Using STL
- Include the corresponding library
- Add the statement
- using namespace std