views:

694

answers:

9

I'm teaching C++ for about 2 years in high schools, computer training institutes and etc. After teaching basics about variables, 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;
 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.

+10  A: 

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.

Stephan Eggermont
+1 good suggestion. but I'm not starting my classroom with code. I edited my question. thanks
Michel Kogan
+1  A: 

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.

This is what I suggest in my question .... Person class ???!!!
Michel Kogan
+1  A: 

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.

Yin Zhu
You suggest start my first OOP examples with writing simple GUI programs ???
Michel Kogan
@Michel Yes. After teaching some basic concepts, you can use simple GUI programs to illustrate the style and power of OOP. I suggest using Qt, 1/4 of the OOP in C++ course in our university is using Qt. And students like this part :)
Yin Zhu
I Agree with "After teaching some basic concepts" part. I agree with Qt part too.
Michel Kogan
+2  A: 

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

Mike Hanson
Objects consist of state and behaviour.
deamon
Yes, of course it does. However, I've seen too many class designs that look like glorified entity relationship diagrams. Many classes end up having lots of fields and virtually no methods. Applying them to MVC platforms, etc. is quite a challenge at that point.
Mike Hanson
+4  A: 

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

just somebody
I forgot somethings in my question( edited! ). I agree with "don't teach OOP using C++" part but Can I teach Java or Python in C++ class ?
Michel Kogan
You could probably do Smalltalk
Stephan Eggermont
@Stephan Eggermont ... Smalltalk is pure OOP lang but I paid for teaching C++ :D
Michel Kogan
@Michel: in that case, don't teach OOP. C++ is a terrible language to teach any paradigm, maybe except procedural. Teach them how to translate into it the stuff they've learned in other, cleaner languages.
just somebody
+3  A: 

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 are 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

Nick Manley
+4  A: 

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
deamon
@deamon I agree with you. C++ is not good for start teaching OOP but in C++ class I don't have any choice. Can I teach Java or Python in C++ class ? I don't think so ...
Michel Kogan
+2  A: 

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.

jalf
Thank you @jalf . I forgot somethings in my question and just edited it. "For that matter, what does your specific example buy us?" what's your proposed example ? This is what I wanna know
Michel Kogan
+1  A: 

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.

Thomas Padron-McCarthy
me 2 ... thanks for the link and suggestion
Michel Kogan