16

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; //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.

Michel Gokan Khan
  • 2,525
  • 3
  • 30
  • 54
  • 8
    A few recommendations for your example: 1. I would almost never encourage use of char* while using C++ -- instead use std::string, 2. There is no need of a setter function for every member of the class (this is important to teach the meaning of encapsulation) – amit kumar Jan 02 '10 at 10:00
  • 2
    duplicate: http://stackoverflow.com/questions/1086086/teaching-oop-to-high-schoolers , related: http://stackoverflow.com/questions/597267/defining-oop-for-a-new-programmer http://stackoverflow.com/questions/355796/how-do-you-explain-oo-to-new-programmers – Nick Dandoulakis Jan 02 '10 at 10:00
  • I know we've had questions like this on here before. The only one I can find is http://stackoverflow.com/questions/1129224/how-to-teach-object-oriented-programming-to-procedural-programmers but I know there are others. – Tyler Jan 02 '10 at 10:01
  • 3
    IMHO, C++ is a poor choice for teaching the principles of OOP because so many of it's basic features are not really OO - for example templates. Having said that, I don't think the perfect OO teaching language exists :-( –  Jan 02 '10 at 11:27
  • @Nick D .................. not exactly .... but they are related – Michel Gokan Khan Jan 02 '10 at 15:50
  • 2
    Remember, when dealing with newbies, prefer `std::string` inside classes rather than `char *`. This will save you some time and frustration having to explain memory allocation, memory ownership and smart pointers. – Thomas Matthews Jan 02 '10 at 18:10
  • 1
    It's not OO to have every data member with a getter and setter and do all the work outside of the class. You might try a "non-traditional" C++ approach, such as http://acceleratedcpp.com/. –  Jan 03 '10 at 01:28
  • 4
    Minor tip. Don't use abbreviated field and function names. What is accomplished by typing "fname" instead of "firstName"? "firstName" is immediately obvious and doesn't leave newcomers wondering why there are funny abbreviations everywhere. – Matthew Olenik Jan 03 '10 at 07:53
  • @Matt Olenik ................ AGREEED!! ... – Michel Gokan Khan Jan 03 '10 at 08:02
  • 8
    I really don't like the use of booleans to represent gender. Even if guys have that "extra bit". – Uri Jan 03 '10 at 09:12

9 Answers9

13

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.

Stephan Eggermont
  • 15,847
  • 1
  • 38
  • 65
7

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

Étienne
  • 4,773
  • 2
  • 33
  • 58
6

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
  • 18,602
  • 6
  • 51
  • 60
4

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
  • 89,107
  • 111
  • 320
  • 448
  • 2
    @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 Gokan Khan Jan 02 '10 at 15:56
3

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
  • 243,077
  • 51
  • 345
  • 550
  • 1
    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 Gokan Khan Jan 03 '10 at 08:00
  • 1
    i checked my question again after more than 1 year. but there is no response from you. what's your suggested "real world" example ? A class that we can call it a "good class"? Let us know. – Michel Gokan Khan Oct 24 '11 at 13:03
2

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
  • 1,059
  • 2
  • 10
  • 21
  • 1
    Objects consist of state and behaviour. – deamon Jan 02 '10 at 12:33
  • 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 Jan 03 '10 at 10:06
1

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.

1

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
  • 27,232
  • 8
  • 51
  • 75
0

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
  • 16,980
  • 13
  • 75
  • 117
  • 1
    @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 Jan 03 '10 at 10:16
  • I Agree with "After teaching some basic concepts" part. I agree with Qt part too. – Michel Gokan Khan Jan 03 '10 at 15:06