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.

10
  • 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 2, 2010 at 10:00
  • 2
  • 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 :-(
    – anon
    Jan 2, 2010 at 11:27
  • 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. Jan 3, 2010 at 7:53
  • 8
    I really don't like the use of booleans to represent gender. Even if guys have that "extra bit".
    – Uri
    Jan 3, 2010 at 9:12

9 Answers 9

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.

3
  • +1 good suggestion. but I'm not starting my classroom with code. I edited my question. thanks Jan 3, 2010 at 7:50
  • I tried your solution recently and It was interesting for students and I realized that they had more difficulties with the concept of the OOP than the implementation. So, CRC cards makes the charm :) Apr 17, 2013 at 9:25
  • Also as @justsomebody mentioned in his answer, I won't teach OOP using C++, Java is my lang of choice. Apr 17, 2013 at 9:27
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

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

5
  • 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 ? Jan 3, 2010 at 7:54
  • You could probably do Smalltalk Jan 3, 2010 at 10:06
  • @Stephan Eggermont ... Smalltalk is pure OOP lang but I paid for teaching C++ :D Jan 3, 2010 at 15:11
  • 3
    @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. Jan 3, 2010 at 17:58
  • +1 for avoiding getters and setters. If you find yourself doing this, just use a struct and abandon the pretense that you have an object in the OOP sense.
    – Crowman
    Aug 23, 2013 at 16:15
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
1
  • 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 ... Jan 2, 2010 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.

2
  • 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 Jan 3, 2010 at 8: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. Oct 24, 2011 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).

2
  • 1
    Objects consist of state and behaviour.
    – deamon
    Jan 2, 2010 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. Jan 3, 2010 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.

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

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

2
  • 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 3, 2010 at 10:16
  • I Agree with "After teaching some basic concepts" part. I agree with Qt part too. Jan 3, 2010 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.