开发者

How to teach Object Oriented Programming - Any idea where to start?

开发者 https://www.devze.com 2022-12-15 11:08 出处:网络
I\'m teaching C++ for about 2 years in high schools, computer training institutes and etc. After teaching basics about vari开发者_Go百科ables, arrays, structures, functions, I always start object orie

I'm teaching C++ for about 2 years in high schools, computer training institutes and etc. After teaching basics about vari开发者_Go百科ables, arrays, structures, functions, I always start object oriented examples part with traditional examples, like this one:

class Person {
public:

 Person();
 ~Person();
 char* getFirstName(); //we can use std::string instead of char* in optimization part
 char* getLastName();
 char* getFullName();
 int getAge();
 bool getGender();
 void printFullProfile();

 void setFirstName(char*);
 void setLastName(char*);
 void setAge(unsigned int); 
 void setGender(bool);
 void setGender(char);//f for female, m for male.

private:
 char* first_name; //or std::string but i prefer to use pointers in the beginning.
 char* last_name;
 unsigned int age;
 bool gender; //ladies 1(st) , male 0
}

and then completing this Person class and teach new things like why getter and setters methods are evil and avoiding accessors, inheritance, polymorphism by creating other classes ( like Student, Employee, Moderator etc. ), necessary OOP skills and concepts.

[EDIT]: And make these classes useful for solving programming problems. ( Like calculating salary for each Employee object, Students marks average, and many others )

another basic examples are Vehicle class, Shape class, etc.

I wanna know your ideas about how to (JUST) start an OOP classroom.

looking forward for great ideas.


I would start without code, with CRC cards. Let the class play out the cards roles, and do a real OO design session. There you can introduce the single responsibility principle, talk about has-a vs is-a and inheritance, encapsulation. I meet too many programmers who don't have a clue about OO and are still programming in c++, c#, java or delphi.

[edit] Later you might want to compare class-based with javascript (or self) and prototype based OO to talk about different ways of classification.


As others have suggested, use real world examples to explain things; like a Vehicle class and a Truck class for inheritance. But here is the important part. Once they understand the real world examples, they need to see program related examples next before they truly grasp why its important to programming.

I've seen a lot of terrible C++ code and it's not just from people who don't understand the concepts. I've seen a lot of coders who know exactly what an object is and can explain polymorphism and all those technical OOP terms perfectly. But if they don't know when to use it, they'll never be able to take full advantage of it. I know because I was like this myself. I had read a book on OOP in high school which explained all the concepts, but I went years before I actually used them because I didn't see when they'd actually benefit my code. Try to give programming assignments where doing it without OOP would be much more challenging. Students will naturally take the easier route and begin to understand. Doing is the best way of learning.

You might have a linked list class and ask them to inherit from it to make a stack or queue by writing their own push/pop, enqueue/dequeue methods. Or make a binary tree class and ask them to make it into a binary search tree by overriding the insert method. Make them work with dynamic memory so they can see why [copy]constructors/destructors are important. Have them write a timer class that stores time as seconds, but uses getters/setters to automatically convert to/from minutes or hours.

I don't know if your students have worked with char arrays or std::strings from the beginning. Make them use both. When they understand how difficult char arrays can be, they'll appreciate the string class more and understand that the point of a class is abstraction, not just code organization. If your students ever do ask about code organization and aren't sure where a particular datum or method should go, remember this quote:

"I will, in fact, claim that the difference between a bad programmer and a good one is whether he considers his code or his data structures more important. Bad programmers worry about the code. Good programmers worry about data structures and their relationships." - Linus Torvalds


start by avoiding classes with getters and setters. one of the major promises of OOP is "data and program go together". it's meant to solve a long-standing problem in procedural programs: a change to a data structure at one spot ripples as far through the program as the data flows. OOP solves this by making sure that data stays put. getters and setters are simply a mechanism to circumvent OOP (data travel in a->setX(y->getX())), and you end up with heaps of boilerplate.

also, don't teach OOP using C++. use a language that was designed for OOP. something that has real encapsulation (changes to private members don't require recompilation of client code) and treats all types uniformly (for example string literals are first class objects).


I would not use C++ to teach OOP, because C++ is not exactly an elegant language and OOP is somewhat strange in C++. I'd prefer Java or Scala or maybe Python, which is a fine language for teaching. My first choice would be Scala, because of it strong concepts. There are no static members breaking oop, for example. Don't forget that teaching is more effective if programming is fun (C++ is not so funny).

I would cover:

  • What is the goal of OOP?
  • polymorphism with inheritance and interfaces
  • responsibility of an object
  • encapsulation
  • messages between objects


How about showing how any of this is relevant to programming? What do anyone gain by being told "you can use classes to model physical objects, like, say a person"?

How does this relate to what they're trying to learn? That is, programming?

This way to "teach" OOP is utter nonsense and is completely useless to anyone who isn't already familiar with the concepts of OOP.

If you want to teach OOP, show them something that is useful in programming. How do OOP concepts such as classes help to solve actual programming problems? And do they offer a cleaner solution than what they'd have to use otherwise?

It's easy to accept the abstract fact that "we might have a Person class". It's also easy enough to accept that "we can derive from this Person class to create a class of more specialized persons, like Janitors or Children or Women". What's missing is any kind of clue as to how this is useful in programming. how is inheritance useful in solving programming problems? How is polymorphism useful? Why do we care about data hiding?

For that matter, what does your specific example buy us? It's just a list of getters and setters. There is virtually no data hiding. There is no abstraction. It's just extra code around what we could've done all along. If you can't write a good class, are you qualified to teach?

And for heavens sake, use std::string instead of char*. Yes, it means you have to tell your students that "std::string is a string", but at least you don't have to have all the clutter and noise of char pointer management. Keep your code clean and free of unwanted noise.


With the increased important on interfaces in today's programming world, I would concentrate on what your class does (behavior) rather than its fields and properties. This helps enforce the concept of "Is-A" thinking (a.k.a. Liskov Sustitution Principle).


As we know Object Oriented concepts are very close to human being so, when i start with oo concept i always give the real time examples and it makes lots of difference.. EX... -- Polymorphism : Real time example of Polymorphism is "ME" myself see there is only one person is me but i have DIFFERENT ROLES such as when i am in home 1. I am son of my father and mother. 2. When i am in school that time i am teacher. 3.when i am on ground then i am a player........ see only one person i.e. me but i have different roles.... i.e. POLYMORPHISM

-- Abstraction : I am a teacher and you(students) are interested in my teaching not what i did for to gather this data and what i did in last night. i.e. Abstraction AVOID TO GIVE UNWANTED THING TO USER.


When I teach object-oriented programming, I try not to start with code, but with concepts and ideas and reasons why. Here is a horrible Google translation of an originally Swedish web page, where I wrote up the OO introduction I use to give.


If time permits and the students are eager and able to learn. You can teach them to write easy GUI programs and building GUIs is also where OOP is good at.

Qt, well designed in C++, is great for this task. Students cannot appreciate inheritance, virtual functions, until they have seen real world examples.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号