How to advance to OOP after procedural?

I understand all the fundamentals of procedural programming from learning in college and at home. Have you guys got any sources that teach OOP very good as i am struggling at the moment learning the basics like classes etc. I understand the concepts, but i can't remember them so i can't implement them off the top of my head. I also struggle to remember how the classes etc work.
Last edited on
closed account (48T7M4Gy)
https://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp3_OOP.html
but i can't remember them so i can't implement them off the top of my head. I also struggle to remember how the classes etc work.


You can always look up the exact syntax. OOP is about how you solve the problems. It's about how you think about them. It's about how you design the software you're going to implement. Do not make the mistake of thinking that OOP is a set of C++ syntax; OOP is not inheritance, is not polymorphism, is not encapsulation. These are tools and techniques you will use to implement your OOP solutions, but they are just the tools. You need to know that they're available, you need to be able to look up what the syntax is. OOP is thinking about the problem is such a way that the solution you come up is neatly represented in objects and their interactions.
start smaller and slower.
a simple class is nothing but some data and some *procedures* that operate on that data. Its honestly like a legit way to use global variables... the class data variables are 'global' to the class itself, so you write your *procedures* as if tapping a global (don't pass them into the subroutines).

get that down, and then step 2 would be to go back and add an 'accessor' for each of the data variables you want to see. This just passes back the value or a const pointer to the internal data so it can be seen but not tampered with outside the class.

After that try adding a constructor, copy constructor, assignment operator.

Those 3 will get you going. Put inheritance and templates and complex issues aside until all the above stuff makes good sense to you.

a few classes that are easy to write and good to start out would be
-- create your own string class using C-strings to emulate c++ strings
-- create your own complex double class to emulate complex<double>
-- create some sort of container class that uses dynamic memory and exercises a destructor.

after this try overloading some methods(why the name changes for subroutines used in classes is a mystery) and look at a virtual function.

most coursework on this would pull you into inheritance once you have a solid grasp of the above and move to templates last.

at the end of the day a class is just tools to keep data and procedures that use it packaged together. It works just like procedural in many ways, but there are some extra things they can do beyond that which would take a big complex mess to emulate in procedural programming (Ive seen OOP C code via function pointers and all sorts of twisted code to get it there... ).
Last edited on
**I very much dislike that link to ehchua — much of the leading information is just plain wrong.

encapsulation / data hiding 
The idea of OO is to encapsulate data. Well-written procedural code does the same thing.

For example, suppose you have an (x y) point that you wish to maintain. Procedurally, you will create some functions to operate over a point.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
typedef struct
{
  double x, y;
}
point;

point point_create( double x, double y )
{
  point p;
  p.x = x;
  p.y = y;
  return p;
}

point point_add( point a, point b )
{
  return point_create( a.x + b.x, a.y + b.y );
}

And so on. With OO, the functions that operate over your data are treated as part of the data:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct point
{
  double x, y;

  point() { }

  point( double x, double y )
  {
    this->x = x;
    this->y = y;
  }

  point add( point a, point b )
  {
    return point( a.x + b.x, a.y + b.y );
  }
};

There are some differences. In C++, I would rather write that addition as an actual ‘+’ operator overload, and I’ll use references instead of a copy of the entire struct:

1
2
3
4
5
6
7
8
9
struct point
{
  ...

  point operator + ( const point& a, const point& b )
  {
    return point( a.x + b.x, a.y + b.y );
  }
};

This also makes using the point significantly more idiomatic in code:

1
2
3
// C
point a = point_create( 1, 2 );
point s = point_add( a, point_create( 3, 5 ) );
1
2
3
// C++
point a( 1, 2 );
point s = a + point( 3, 5 );

These examples, BTW, are simplistic in the extreme. Consider the extra care that you must take in C to handle dynamic data, for example. In C++, RAII is automatic (assuming you use it).

Design Rationale Behind Object Oriented Programming 
OOP takes it a little further. The primary principles behind OOP include:

  • encapsulation (bundling data with operations on that data)
  • data abstraction (internals are hidden behind the API)
  • polymorphism (inheritance)
  • dynamic binding (virtual functions)

None of these are things that can’t be done in C, and almost as easily. But as part of the C++ language you get it for free, without having to write all the boilerplate yourself and leaving actual code looking clean and readable.

It isn’t perfect 
In spite of what you may read, OOP is not the One True Way™. As has always been the case, many, many poorly-written and bloated code is written in OOP as is procedural.

Because of the way it is designed, some problems manifest more in one methodology than the other, but the problem with, say, code that knows too much about its environment (such as is often simplified into anti-global variable sentiment) exists just as commonly in OOP as procedural environments.

Whether using an OOP methodology or not, the important point is to design your code properly. That takes careful effort to learn, and is frighteningly uncommon in real-world code. (IMHO)
Last edited on
Topic archived. No new replies allowed.