1 / 22

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

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: haibinz@nipissingu.ca URL: http://www. nipissingu.ca/faculty/haibinz. Contents. Introduction

judah
Télécharger la présentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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: haibinz@nipissingu.ca URL: http://www. nipissingu.ca/faculty/haibinz

  2. Contents • Introduction • Six Steps to Introduce the Object-Oriented Programming • Teaching inheritance in C++ • Teaching Other Concepts in C++ • Conclusion

  3. Introduction • 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.

  4. The Six Steps • General Principles • Object • Class • Instances • Inheritance • Metaclass

  5. 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.

  6. Step1(cont’d) • Fundamental concepts • Abstract and induction • Concretion and Deduction • Composition and Decomposition • A class exercise: • Describe yourself.

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

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

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

  10. Step 5 • Illustrate 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.

  11. Step 6 • (Optional) Discuss metaclasses to master completely the class and object concepts

  12. 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

  13. Multiple 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: • … … • }

  14. Inheritance and overloading 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) } #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; } };

  15. Teaching Other Concepts in C++ • Why constructors and destructors cannot be inherited • Polymorphism and pointers • About "this" • Templates • Friends • Operator overloading • Exception handling

  16. Constructors 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.

  17. Polymorphism 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."

  18. Templates • 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.

  19. Friends • 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.

  20. Operator 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) • }

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

  22. Conclusion • 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!

More Related