Object Oriented Programming pillars

After almost three decades in IT and two decades in programming industrial. I realized people keep talking about some terms without even understand the fundamental of the concept such as dependency injection.

The four key pillars of object oriented programming listed as below.

1. Abstraction

Abstraction how your code exposing essential feature of an entity while hiding other irrelevant detail. This can reduces code complexity and also makes your aesthetically pleasant.

This is how abstraction looks like using C++.

// Base class

class Shape {

public:

// pure virtual function providing interface framework.

virtual int getArea() = 0;

void setWidth(int w) {

width = w;

}

void setHeight(int h) {

height = h;

}

protected:

int width;

int height;

};

// Derived classes

class Rectangle: public Shape {

public:

int getArea() {

return (width * height);

}

};

class Triangle: public Shape {

public:

int getArea() {

return (width * height)/2;

}

};

2. Encapsulation

Encapsulation is when you hide your modules internal data and all other implementation details from other classes. In order words it is related to Data Hiding. it is also a method of restricting access to certain properties or methods.

To achieve this, we need to use access modifiers, the public, private, and protected especially.

class Adder {

public:

// constructor

Adder(int i = 0) {

total = i;

}

// interface to outside world

void addNum(int number) {

total += number;

}

// interface to outside world

int getTotal() {

return total;

};

private:

// hidden data from outside world

int total;

};

3.Inheritance

Like the word Inheritance literally means it is a practice of passing on property, titles, debts, rights and obligations upon the death of an individual. in OOP this is somehow true(Except the death of an individual) , where The base class(the existing class sometimes called as the Parent class) has properties and methods that will be inherited by the sub class(sometimes called a subtype or child class) and it can have additional properties or methods.

The ability of creating a new class from an existing class.

class Person

{

public:

string profession;

int age;

Person(): profession(“unemployed”), age(16) { }

void display()

{

cout << “My profession is: ” << profession << endl;

cout << “My age is: ” << age << endl;

walk();

talk();

}

void walk() { cout << “I can walk.” << endl; }

void talk() { cout << “I can talk.” << endl; }

};

// MathsTeacher class is derived from base class Person.

class MathsTeacher : public Person

{

public:

void teachMaths() { cout << “I can teach Maths.” << endl; }

};

// Footballer class is derived from base class Person.

class Footballer : public Person

{

public:

void playFootball() { cout << “I can play Football.” << endl; }

};

In the above code snippet MathsTeacher is a Person, and Footballer is a Person. This establish the is a relationship for inheritance.

4. Polymorphism

It us like in biology, Polymorphism refers to the ability to take into different forms or stages. A subclass can characterize its own particular extraordinary conduct and still offer similar functionalities or conduct of its parent/base Subclass can have their own particular conduct and offer some of its conduct from its parent class not the a different way. A parent class can’t have the conduct of its subclass.

// virtpers.cpp

// virtual functions with person class

#include <iostream>

using namespace std;

////////////////////////////////////////////////////////////////

class person //person class

{

protected:

char name[40];

public:

void getName()

{ cout << ” Enter name: “; cin >> name; }

void putName()

{ cout << “Name is: ” << name << endl; }

virtual void getData() = 0; //pure virtual func

virtual bool isOutstanding() = 0; //pure virtual func

};

////////////////////////////////////////////////////////////////

class student : public person //student class

{

private:

float gpa; //grade point average

public:

void getData() //get student data from user

{

person::getName();

cout << ” Enter student’s GPA: “; cin >> gpa;

}

bool isOutstanding()

{ return (gpa > 3.5) ? true : false; }

};

////////////////////////////////////////////////////////////////

class professor : public person //professor class

{

private:

int numPubs; //number of papers published

public:

void getData() //get professor data from user

{

person::getName();

cout << ” Enter number of professor’s publications: “;

cin >> numPubs;

}

bool isOutstanding()

{ return (numPubs > 100) ? true : false; }

};

////////////////////////////////////////////////////////////////

int main()

{

person* persPtr[100]; //array of pointers to persons

int n = 0; //number of persons on list

char choice;

do {

cout << “Enter student or professor (s/p): “;

cin >> choice;

if(choice==’s’) //put new student

persPtr[n] = new student; // in array

else //put new professor

persPtr[n] = new professor; // in array

persPtr[n++]->getData(); //get data for person

cout << ” Enter another (y/n)? “; //do another person?

cin >> choice;

} while( choice==’y’ ); //cycle until not ‘y’

for(int j=0; j<n; j++) //print names of all

{ //persons, and

persPtr[j]->putName(); //say if outstanding

if( persPtr[j]->isOutstanding() )

cout << ” This person is outstanding\n”;

}

return 0;

} //end main()

The Classes

In the above code, the person class is an abstract class because it contains the pure virtual functions getData() and isOutstanding(). No person objects can ever be created because of the pure virtual functions. This class exists only to be the base class for the student and professor classes. The student and professor classes add new data members to the base class. The student class contains a property gpa with data type float that represents the student’s grade point average (GPA). The professor class contains a variable numPubs, with data type int, which represents the number of scholarly publications the professor has published.

The isOutstanding() Function

The isOutstanding() method is declared as a pure virtual function in person. In the student class this function returns a bool true if the student’s GPA is greater than 3.5, and false otherwise.

In professor it returns true if the professor’s numPubs variable is greater than 100. The getData() function asks the user for the GPA for a student, but for the number of publications for a professor.

Now, how to do Polymorphism in C# or Java. It uses the keyword interface. This is where the “Dependency Injection” comes in. I will reserve this in another article.

The below C# code is implementing the above C++ code. You can see there is no pure virtual function.

public interface IPerson

{

void getData();

bool isOutstanding();

}

public abstract class person //person class

{

protected string name = new string(new char[40]);

public void getName()

{

Console.Write(” Enter name: “);

name = Console.ReadLine();

}

public void putName()

{

Console.Write(“Name is: “);

Console.Write(name);

Console.Write(“\n”);

}

public virtual void getData() { }

public virtual bool isOutstanding() { return true; }

}

////////////////////////////////////////////////////////////////

public class student : person //student class

{

private float gpa; //grade point average

public override void getData() //get student data from user

{

base.getName();

Console.Write(” Enter student’s GPA: “);

gpa = float.Parse(Console.ReadLine());

}

public override bool isOutstanding()

{

return (gpa > 3.5F) ? true : false;

}

}

////////////////////////////////////////////////////////////////

public class professor : person //professor class

{

private int numPubs; //number of papers published

public override void getData() //get professor data from user

{

base.getName();

Console.Write(” Enter number of professor’s publications: “);

numPubs = int.Parse(Console.ReadLine());

}

public override bool isOutstanding()

{

return (numPubs > 100) ? true : false;

}

}

References:

What is Abstraction in OOPs? Learn with Java Example. (n.d.). Retrieved 03 Jan 2018, from
https://www.guru99.com/java-data-abstraction.html

Encapsulation in C++ with example. (n.d.). Retrieved 03 Jan 2018, from
https://beginnersbook.com/2017/09/cpp-encapsulation/

Data Encapsulation in C++. (n.d.). Retrieved 03 Jan 2018, from
https://www.tutorialspoint.com/cplusplus/cpp_data_encapsulation.htm

C++ Inheritance. (n.d.). Retrieved 03 Jan 2018, from
https://www.programiz.com/cpp-programming/inheritance

Advertisements

About chanmingman

Since March 2011 Microsoft Live Spaces migrated to Wordpress (http://www.pcworld.com/article/206455/Microsoft_Live_Spaces_Moves_to_WordPress_An_FAQ.html) till now, I have is over 1 million viewers. This blog is about more than 50% telling you how to resolve error messages, especial for Microsoft products. The blog also has a lot of guidance teaching you how to get stated certain Microsoft technologies. The blog also uses as a help to keep my memory. The blog is never meant to give people consulting services or silver bullet solutions. It is a contribution to the community. Thanks for your support over the years. Ming Man is Microsoft MVP since year 2006. He is a software development manager for a multinational company. With 25 years of experience in the IT field, he has developed system using Clipper, COBOL, VB5, VB6, VB.NET, Java and C #. He has been using Visual Studio (.NET) since the Beta back in year 2000. He and the team have developed many projects using .NET platform such as SCM, and HR based applications. He is familiar with the N-Tier design of business application and is also an expert with database experience in MS SQL, Oracle and AS 400.
This entry was posted in .Net, Community, Computers and Internet. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s