Four pillars of object oriented programming

Started by
27 comments, last by swiftcoder 9 years, 11 months ago

What are the four pillars of object oriented programming, with regards to C++? I've googled and googled this subject, and there are different answers depending on which site you visit. What is your impression as to what the four pillars are? I've given my impression below, in the form of code snippets. Are these snippets the correct way to think of the four pillars?

Simple class / object:


#include <iostream>
using std::cout;
using std::endl;


class A
{
public:
    A(void)
    {
        value1 = 123;
    }

    int value1;
};


int main(void)
{
    A a;
    cout << a.value1 << endl;

    return 0;
}

Pillar 1 encapsulation:


#include <iostream>
using std::cout;
using std::endl;


class A
{
public:
    A(void)
    {
        value1 = 123;
    }

// Encapsulating the member variable by making it protected from code
// that exists outside of the class    
protected:
    int value1;

public:
    int getvalue1(void)
    {
        return value1;
    }

    void setvalue1(const int value)
    {
        value1 = value;
    }
};


int main(void)
{
    A a;
    cout << a.getvalue1() << endl;

    return 0;
}

Pillar 2 inheritence:


#include <iostream>

using std::cout;
using std::endl;


// Base class
class A
{
public:
    A(void)
    {
        value1 = 123;
    }

protected:
    int value1;

public:
    int getvalue1(void)
    {
        return value1;
    }

    void setvalue1(const int value)
    {
        value1 = value;
    }
};


// Inheriting class
class B: public A
{
public:
    B(void)
    {
        value2 = 456;
    }

protected:
    int value2;

public:
    int getvalue2(void)
    {
        return value2;
    }

    void setvalue2(const int value)
    {
        value2 = value;
    }
};


int main(void)
{
    B b;
    cout << b.getvalue1() << ' ' << b.getvalue2() << endl;

    return 0;
}

Pillar 3 abstraction (abstract class):


#include <iostream>
using std::cout;
using std::endl;


// Abstract base class (cannot be instantiated due to pure virtual member function)
class A
{
public:
    virtual void talk(void) = 0;
};

// Inheriting class must implement pure virtual function talk()
class B: public A 
{
public:
    void talk(void)
    {
        cout << "Class B" << endl;
    }
};


int main(void)
{
    B b;
    b.talk();

    return 0;
}

Pillar 4 polymorphism:


#include <iostream>
using std::cout;
using std::endl;


// Abstract base class (cannot be instantiated due to pure virtual member function)
class A
{
public:
    virtual void talk(void) = 0;
};

// Inheriting class must implement pure virtual function
class B: public A 
{
public:
    void talk(void)
    {
        cout << "Class B" << endl;
    }
};

// Inheriting class must implement pure virtual function
class C: public A 
{
public:
    void talk(void)
    {
        cout << "Class C" << endl;
    }
};


int main(void)
{
    B b;
    b.talk();

    C c;
    c.talk();

    // Also see general function overloading as an example of polymorphism
    // ie:
    // int add(int a, int b) { return a + b }
    // float add(float a, float b) { return a + b }
    //
    // Also see templates
    // ie:
    // STL containers

    return 0;
}
Advertisement

C++ is a light-weight abstraction programming language. Type- and exception- safety I think is at the core of the language.

The constructor acquire resources and the destructor releases them. I believe OOP in C++ is just a consequence of these fundamental principles; OOP is not a design goal in itself.

I think you've got it for the first three. The polymorphisim, however, is missing something. This means that objects can change (morph) into different types (poly) at runtime:


int main(void)
{
    A a = B;
    a.talk(); // calls B.talk()

    a = C;
    a.talk(); // calls C.talk()

 
    return 0;
}

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

Should Pillar #2 be Composition ?

Beginner in Game Development?  Read here. And read here.

 

Should Pillar #2 be Composition ?

You know, I've been asked this question in job interviews, and I always say "composition." But I googled it, and everything comes up as encapsulation. I was wondering that myself. Why do we both think its composition? But I always say, "Composition, inheritance, abstraction, and polymoprhism." I don't replace encapsilation... I don't even know what's real...

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

What is the point of this laundry list of features? Did it ever help anyone write good code? It certainly doesn't help me...


But I always say, "Composition, inheritance, abstraction, and polymoprhism." I don't replace encapsilation... I don't even know what's real...

Actually, I was saying: encapsulation, composition, abstraction, and polymorphism :)

But I was under the impression that the pillars of OOP was SOLID. I guess I was wrong :|

Beginner in Game Development?  Read here. And read here.

 

Yeah, a search I did found this site which seems to agree with the OP : http://ruelbelmonte.tumblr.com/post/6066837330/4-pillars-of-oop

@Glass_Knife, @Alpha_ProgDes

Sounds like you are describing parts of the C++ pillars of OOP: http://www.c4learn.com/cplusplus/cpp-pillars-of-oop/

If it seems that no one can agree what the "four pillars of OOP" are, perhaps you should take that as a sign that the idea of "four pillars" is bullshit.

What is the point of this laundry list of features? Did it ever help anyone write good code? It certainly doesn't help me...

Crappy interviewer questions? Also some of the random things that shows up in poor introductory classes that have written tests?

This topic is closed to new replies.

Advertisement