How to describe object-oriented programming to a beginner? Is there a good real analogy?

My brother-in-law is a freshman in college. He has no programming experience. He studies programming in his classes, but he seems to be struggling with basic concepts. This does not help that he seems to be the only person in all his classes without any programming experience.

He worked well in Matlab (which I don’t know), and then I helped him when he learned the basics of Python. Pretty soon, his courses will begin in C and C ++. I fear that he will stay away when object-oriented programming appears.

I tried to explain it to him by analogy with a car.

pseudo code:

Class Car { public string make; public string model; private string milesPerGallon; private float gasolineGallonsInTank = 0; private float tankCapacity; private float odometer = 0; public Car(maxGas, mpg) { tankCapacity = maxGas; milesPerGallon = mpg; } public void fillTank() { gasolineGallonsInTank = tankCapacity; } public void drive(float miles) { if (miles == 0) { print("You don't want to drive?"); return; } if(miles < 0) { print("Ok, we're driving in reverse!"); miles = Math.AbsoluteValue(miles); } float maxDistance = gasolineGallonsInTank / milesPerGallon; if (maxDistance >= miles) { odometer += maxDistance; gasolineGallonsInTank = 0; print("You've run out of gas!"); return; } odometer += miles; gasolineGallonsInTank -= miles / milesPerGallon; } public float readOdometer() { return odometer; } } 

I said that the Car class was like a factory car, and var mySedan = new Car(12, 20) was like producing a new car with a 12 liter gas tank and 20 mpg. Then I showed him how you can control the methods, and it was like how everything happens with the machine.

Then I made a second car: var myMiniVan = new Car(21.5, 14) and showed how the working methods on one car do not affect the others.

But he did not understand this. All this passed over his head. Is there a better or simple visual analogy that I can use? Am I explaining this wrong?

+4
source share
5 answers

Our teacher used:

  • automobiles and their components - explain classes, fields, methods and show what aggregation and composition are.
  • animals (man, tiger and cat, for sure :)) - explain the inheritance
  • shape - to explain more inheritance and polymorphism

In addition, as I recall, good examples were presented in the OOA & D book by Grady Booch .

At the first OOP seminar, we did a rather unusual interesting exercise: we implemented the “classes” in C (not C ++). We had to use structures and pointers for functions - this made us feel what state was, what behavior was, what class and objects were. Then we moved on to C ++.

UPDATE

I just remembered another good and descriptive example of the basic concepts of OOP: GUI components (buttons, text fields, titles, dialogs). These examples are not "abstract", like animals and cars, and they are more descriptive - the result can be seen immediately. There are many graphical GUIs, you can offer your brother to play with one of them.

+1
source

Maybe you should take a program that he understands (e.g. in python). And show him the benefits after the oo-approach. This is how I learned C ++ after gaining some basic knowledge of C. But I thought your explanation was already pretty clear.

+1
source

Another good analogy (especially for an engineering student) can be part of a machine.

Take the carburetor. Carburrettor A is designed to meet the specific characteristics of a particular engine, including the INTERFACE (usually sealed with a gasket, which also matches the interface) between the manifold and the carburetor.

There are certain openings on any surface that should align exactly like that, and it is expected that fuel will be supplied from the gas line to the carburetor at a certain pressure and volume. Carb is expected to deliver a specific air-fuel mixture to a manifold for a specific vacuum pressure, etc.

This is a good perspective for an open interface. Carb manufacturers do not need to know much about the engine, except for a template for the interface between their carburetor and manifold, as well as some specifications for the fuel-air mixture expected on the manifold. Similarly, the engine doesn’t care how the carburetor does what it does, it just needs to deliver the fuel under the right pressure to the proper hole in the manifold so that the carburetor can perform some kind of magical function and deliver the proper mixture of fuel air on request. Different manufacturers can achieve this in different ways, but as long as the inputs and outputs are the same, everything works fine.

INSIDE the carburetor, there is everything that happens to better control fuel flow, and measure vacuum pressure with pitot tubes, etc. They are akin to PRIVATE functions and methods. The means by which knwos carburetor, which, given the vacuum pressure X, I need to put the amount of fuel Y and the volume of air Z into the manifold.

While this does not necessarily do a good job of describing private member variables, getters vs seters, etc., MAY help with the concept of an interface, excapsulation and Private vs Public. For me it was initially more complicated than private member variables and such (especially the "Interface" part ...).

0
source

Programming is best studied.

Work with him to write a simple address book application. (There is no need to save anything, as this is OOP learning experience.) Make a CEntry class that will represent each entry in the address book. It will contain things like a person’s name, address, city, state, zip code and phone number. Make another CName class that will have members first , middle, and last . Finally, make a third CPhone class that will have members for country , area_code , prefix and number . As he writes this, you can explain why the use of classes makes sense for this application, as well as the advantages and disadvantages of having CEntry inheritance from CName and CPhone or contain new instances of these classes.

0
source

Source: https://habr.com/ru/post/1337141/


All Articles