22
Methodology First and Methodology First and Language Second Language Second -A Way to Teach Object- -A Way to Teach Object- Oriented Programming Oriented Programming Haibin Zhu, PhD Department of Computer Science and Mathematics Nipissing University, North Bay, Canada email: [email protected] URL: http://www. nipissingu.ca/faculty/haibinz

Methodology First and Language Second -A Way to Teach Object-Oriented Programming

  • Upload
    judah

  • View
    30

  • Download
    0

Embed Size (px)

DESCRIPTION

Methodology 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: [email protected] URL: http://www. nipissingu.ca/faculty/haibinz. Contents. Introduction - PowerPoint PPT Presentation

Citation preview

Page 1: Methodology First and Language Second -A Way to Teach Object-Oriented Programming

Methodology First and Methodology First and Language SecondLanguage Second

-A Way to Teach Object-Oriented -A Way to Teach Object-Oriented ProgrammingProgramming

Haibin Zhu, PhD

Department of Computer Science and Mathematics

Nipissing University, North Bay, Canada

email: [email protected]

URL: http://www. nipissingu.ca/faculty/haibinz

Page 2: Methodology First and Language Second -A Way to Teach Object-Oriented Programming

ContentsContents

IntroductionSix Steps to Introduce the Object-Oriented

ProgrammingTeaching inheritance in C++Teaching Other Concepts in C++Conclusion

Page 3: Methodology First and Language Second -A Way to Teach Object-Oriented Programming

IntroductionIntroduction

Object-oriented programming is requiredC++ is an OOPL used to teach OOPC++ 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.

Page 4: Methodology First and Language Second -A Way to Teach Object-Oriented Programming

The Six StepsThe Six Steps

General PrinciplesObjectClassInstancesInheritanceMetaclass

Page 5: Methodology First and Language Second -A Way to Teach Object-Oriented Programming

Step 1 Step 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.

Page 6: Methodology First and Language Second -A Way to Teach Object-Oriented Programming

Step1(cont’d)Step1(cont’d)Fundamental concepts

– Abstract and induction– Concretion and Deduction– Composition and Decomposition

A class exercise:– Describe yourself.

Page 7: Methodology First and Language Second -A Way to Teach Object-Oriented Programming

Step 2Step 2 Introduce an object concept by observing the real

world– Everything is an object – Object ::= <N, s, M, X >, 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

Page 8: Methodology First and Language Second -A Way to Teach Object-Oriented Programming

Step 3Step 3 Acquire the class concept by abstraction of many

common objects– Many objects with common properties form a class– C ::= <N, D, M, X>, 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.

Page 9: Methodology First and Language Second -A Way to Teach Object-Oriented Programming

Step 4Step 4 Introduce instantiation after the class concept is

learned– Every object has a class– O ::= < N, C, S >, where

N is an identification or name of the object; C is the object’s 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.

Page 10: Methodology First and Language Second -A Way to Teach Object-Oriented Programming

Step 5Step 5Illustrate subclasses by adding more details to

an existing class and superclasses by finding common things among several classes – Specialization and generalization– Cs ::= <N, {C}, D, M, X >, 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.

Page 11: Methodology First and Language Second -A Way to Teach Object-Oriented Programming

Step 6Step 6

(Optional) Discuss metaclasses to master completely the class and object concepts

An Object

A class A class

A metaclass A metaclass

Metaclass Metaclass

Class Class

Class class Class class a class

an object An object depends on a class.

A class A class

A class can be taken as an object. 1

2

3

4

5

6

7

8

9

10 11

Page 12: Methodology First and Language Second -A Way to Teach Object-Oriented Programming

Teaching inheritance in C++Teaching 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

Page 13: Methodology First and Language Second -A Way to Teach Object-Oriented Programming

Multiple inheritanceMultiple 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 engines[4]; //4 engines Wing wings[2]; //2 wings Tail tail; public: … … }

Page 14: Methodology First and Language Second -A Way to Teach Object-Oriented Programming

Inheritance and overloadingInheritance and overloading#include <iostream>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) { cout<<"b_class, a= "<<a.x<<endl; } void play(A b) { cout<<"b_class, b = "<<b.y<<endl; }};

class d_class : public b_class{public: void display(B f ) { cout<<"d_class, f = "<<f.i<<endl; } void play(B g ) { cout<<"d_class, g = "<<g.j<<endl; }};

void main()

{ b_class *a;

d_class *b;

a = new b_class();

b = new d_class();

A a1,a2;

b->display(a1); //(1)

b->play(a1); //(2)

b->play(a2); //(3)

}

Page 15: Methodology First and Language Second -A Way to Teach Object-Oriented Programming

Teaching Other Concepts in C++Teaching Other Concepts in C++

Why constructors and destructors cannot be inherited

Polymorphism and pointers– About "this"

TemplatesFriendsOperator overloadingException handling

Page 16: Methodology First and Language Second -A Way to Teach Object-Oriented Programming

Constructors and destructorsConstructors 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.

Page 17: Methodology First and Language Second -A Way to Teach Object-Oriented Programming

Polymorphism and pointersPolymorphism 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."

Page 18: Methodology First and Language Second -A Way to Teach Object-Oriented Programming

TemplatesTemplates

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.

Page 19: Methodology First and Language Second -A Way to Teach Object-Oriented Programming

FriendsFriends

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.

Page 20: Methodology First and Language Second -A Way to Teach Object-Oriented Programming

Operator overloadingOperator overloading

Another way to name a member function For students, we suggest clear names

const Time & Time::operator +=( unsigned n ) {//... } void main() {Time a, b, c; a+=b; //(1) a.operator +=(b); //(2) }

Page 21: Methodology First and Language Second -A Way to Teach Object-Oriented Programming

Exception handlingException 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.

Page 22: Methodology First and Language Second -A Way to Teach Object-Oriented Programming

ConclusionConclusion

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!