Title: Methodology First and Language Second -A Way to Teach Object-Oriented Programming
1Methodology First and Language Second-A Way to
Teach Object-Oriented Programming
- Haibin Zhu, PhD
- Department of Computer Science and Mathematics
- Nipissing University, North Bay, Canada
- email haibinz_at_nipissingu.ca
- URL http//www. nipissingu.ca/faculty/haibinz
2Contents
- Introduction
- Six Steps to Introduce the Object-Oriented
Programming - Teaching inheritance in C
- Teaching Other Concepts in C
- Conclusion
3Introduction
- Object-oriented programming is required
- C is an OOPL used to teach OOP
- C is a flexible language that needs the
instructor to guide the students to master C
and OOP. - It is a failure for both teaching and learning
OOP with C that students know how to program in
C but not in OOP.
4The Six Steps
- General Principles
- Object
- Class
- Instances
- Inheritance
- Metaclass
5Step 1
- Discuss fundamental principles of
object-orientation with respect to conventional
thinking - Fundamental Principles
- Everything in the world is an object 4.
- Every system is composed of objects (certainly a
system is also an object). - The evolution and development of a system is
caused by the interactions among the objects
inside or outside the system.
6Step1(contd)
- Fundamental concepts
- Abstract and induction
- Concretion and Deduction
- Composition and Decomposition
- A class exercise
- Describe yourself.
7Step 2
- Introduce an object concept by observing the real
world - Everything is an object
- Object ltN, s, M, X gt, where
- N is an identification or name of an object
- s is a state or a body represented by a set of
attributes - M is a set of methods (also called services or
operations) the object can perform and - X is an interface that is a subset of the
specifications of all the methods of the object. - A class exercise
- Describe yourself by this expression
8Step 3
- Acquire the class concept by abstraction of many
common objects - Many objects with common properties form a class
- C ltN, D, M, Xgt, where
- N is an identification or a name of the class
- D is a space description for memory
- M is a set of the method definitions or
implementations and - X is a unified interface of all the objects of
this class. - A class exercise
- Describe a class you belong to.
9Step 4
- Introduce instantiation after the class concept
is learned - Every object has a class
- O lt N, C, S gt, where
- N is an identification or name of the object
- C is the objects class identified by the class
identification or name and - S is a body or an actual space whose values are
called attributes, properties, or state. - A class exercise
- Describe yourself as an instance of the class you
described.
10Step 5
- Illustrate subclasses by adding more details to
an existing class and superclasses by finding
common things among several classes - Specialization and generalization
- Cs ltN, C, D, M, X gt, where
- C denotes a set of superclasses identified by
their identifications or names, and - N, D, M, and X have the same meanings as those of
C. - A class exercise
- Describe a class that is a superclass of the
class you belong to.
11Step 6
- (Optional) Discuss metaclasses to master
completely the class and object concepts
12Teaching inheritance in C
- Classification-based view of inheritance
- Consistent structures, single inheritance and
whole inheritance are inherent - Code reuse-based view of inheritance
- Inconsistent structure, multiple inheritance and
part inheritance reasonable
13Multiple inheritance
- Multiple inheritance
- Multiple inheritance brings about replicated
inheritances, and programmers must use a
"virtual" specifier to make it clear. - Multiple inheritance and aggregation
- class Plane
-
- private
- Head head
- Engine engines4 //4 engines
- Wing wings2 //2 wings
- Tail tail
- public
-
14Inheritance and overloading
class d_class public b_class public
void display(B f )
coutltlt"d_class, f "ltltf.iltltendl
void play(B g )
coutltlt"d_class, g "ltltg.jltltendl
void main() b_class a d_class b a
new b_class() b new d_class() A
a1,a2 b-gtdisplay(a1) //(1) b-gtplay(a1) //(2
) b-gtplay(a2) //(3)
- include ltiostreamgt
- using namespace std
- class A
- public
- A()x 10, y 20
- int x,y
-
- class B public A
- public
- B()i 10, j 20
- int i,j
-
- class b_class
- public virtual void display(A a)
- coutltlt"b_class, a "ltlta.xltltendl
- void play(A b)
- coutltlt"b_class, b
"ltltb.yltltendl
15Teaching Other Concepts in C
- Why constructors and destructors cannot be
inherited - Polymorphism and pointers
- About "this"
- Templates
- Friends
- Operator overloading
- Exception handling
16Constructors and destructors
- Constructors and destructors actually belong to
the metaclass of a class. - Why can the destructors of a base class not be
defined without a virtual specifier? " - A single polymorphic function (every class has
only one destructor). - "Virtual" specifiers shows that there are derived
classes to be responsible for managing the spaces
by themselves.
17Polymorphism and pointers
- A single name has different meanings.
- Without pointers, C would not support
polymorphism. - Fruit bag assumption
- An apple can be put in a bag of fruit, but a
fruit cannot be put in a bag of apple."
18Templates
- One way to abstract
- Class templates are not classes, but
parameterized classes from a class template can
be used as ordinary classes - Class templates are not classes, but they can be
defined by inheriting an ordinary class and - Class templates are not the same as the Smalltalk
metaclasses.
19Friends
- Be careful to friends
- It increase the coupling among classes
- It is killing encapsulation
- Properties of friends
- Friend has no transitivity.
- Friend cannot be inherited.
- Friend has no reflexivity.
20Operator overloading
- Another way to name a member function
- For students, we suggest clear names
- const Time Timeoperator ( unsigned n )
- //...
-
- void main()
- Time a, b, c
- ab //(1)
- a.operator (b) //(2)
21Exception handling
- A pool assumption.
- Any time when the program tries (TRY) some
message and - encounters a defined exception, it throws (THROW)
the exception object to the pool. - After that, the program searches the pool and
tries to catch (CATCH) an exception with its
class identification the same as what is declared
in the parameter list of the CATCH statement. - If it catches such kind of exception, it does
some exception handling operations. If not, it
just follows the normal procedure.
22Conclusion
- Clear concepts and correct methodologies for
students to learn OOP are very important - C is a flexible object-oriented programming
language, so that C programmers might use it in
different ways. - We must clarify which ways are good and which
ways are bad. - Need to mention This teaching methodology
obtains better results for mature students than
un-mature students!