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
Posted in .Net, Community, Computers and Internet | Leave a comment

The problem of using var to declare variable

People just over use var to declare variable.

var is good if you have a statement like

var result = from r in someList;

It will be very bad to use var like the example below from https://magedfarag.wordpress.com/2012/12/14/call-wcf-sevice-using-only-webclient-and-soap-xml/ (if the link still valid).

using (var client = new WebClient())
{
    var data = GetSoap();
    client.Headers.Add(“Content-Type”, “text/xml;charset=utf-8”);
    client.Headers.Add(“SOAPAction”,  “\”http://tempuri.org/IMyService/MyOperation\””);
    var response =      client.UploadString(“http://localhost:1234/MyService.svc&#8221;, data);
    Console.WriteLine(response);
}

For this one, you need god to tell you what is GetSoap return, a string, XmlDocument, or something else?

Again you need to do google search to know what is UploadString return.
Isn’t good people just use the proper type for declaration?

Posted in .Net, Community, Computers and Internet | Tagged | Leave a comment

The Developer’s Guide to Microsoft Azure SECOND EDITION FREE

This is a book that without advertisement. A lot of walkthroughs, provided the Azure portal still look the same because Microsoft keep changing the Azure interface. Sometime you just do not know where the items that you want or in the book have gone.

Somehow, I searched through and could not find a download location for any source code.

It is still worth to download and take a look since it is free now.

http://download.microsoft.com/download/2/C/F/2CF7401A-B9D7-4828-917D-199

 

Posted in .Net, Cloud, Community, Computers and Internet | Tagged , | Leave a comment

IIS The process cannot access the file because it is being used by another process

This short writing shows you one of the possible ways to resolve the error “The process cannot access the file because it is being used by another process.
(Exception from HRESULT: 0x80070020)
” This error prevent you from starting IIS Website.

The link https://support.microsoft.com/en-us/help/973094/error-0x80070020-when-you-try-to-start-a-web-site-in-iis-7-0 provides you a command to find the process id to End Task from Task
Manager.

I have this problem and I found out from the Task Manager, Skype was the one taking port 80, not sure why.

Posted in .Net, Community, Computers and Internet | Tagged | Leave a comment

SSIS Parameter name is unrecognized

This short article shows you one of the ways to resolve the error “Parameter name is unrecognized”.

You have Execute SQL Task in your Data Flow. You want to pass in variable as parameter so you have a sql statement like DELETE [dbo].[Journeys] WHERE [mcValue] = ?.

Then you click on Parameter Mapping and click Add. If you leave everything by default and click OK. Very likely you will get the error message below.

[Execute SQL Task] Error: Executing the query “DELETE [dbo].[Journeys] WHERE [mcValue] = ?” failed with the following error: “Parameter name is unrecognized.”. Possible failure reasons: Problems with the query, “ResultSet” property not set correctly, parameters not set correctly, or connection not established correctly.

To resolve this, change the NewParameterName to 0.

Reference:

https://stackoverflow.com/questions/2008569/ssis-sql-task-parameter-name-is-unrecognized

Posted in .Net, Community, Computers and Internet | Tagged , | Leave a comment

SQL Server Data Tools for Visual Studio 2017

This shot writing is letting you know SQL Server Data Tools for Visual Studio 2017 is RTM.

You can get this SSDT here https://docs.microsoft.com/en-us/sql/ssdt/download-sql-server-data-tools-ssdt. No need to go to your msdn and look for the iso file for now. It is not there and I am not sure will it be there in the future. Hopeful it will because of all the 3rd world country.

You will see the installation starts as below. Bear in mind choose the Visual Studio instance correctly before you click Install.

You can see the Business intelligence template after this.

Posted in .Net, Community, Computers and Internet | Tagged , , , , | Leave a comment

Microsoft.SqlServer.Dts.Tasks.WebServiceTask.WebserviceTaskException: Could not execute the Web method

This short blog shows you how to resolved the error “Could not execruute the Web method. The error is: Could not load file or assembly ‘Microsoft.SqlServer.WebServiceTask” when running SSIS in Visual Studio 2015 with SSDT SSDT_14.0.61707.300_EN.

When you add a Web Service Task in your SSIS package. Setup the Http Connection Manager and wsdl. Once you run it you get the error below.

[Web Service Task] Error: An error occurred with the following error message: “Microsoft.SqlServer.Dts.Tasks.WebServiceTask.WebserviceTaskException: Could not execute the Web method. The error is: Could not load file or assembly ‘Microsoft.SqlServer.WebServiceTask, Version=14.100.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91’ or one of its dependencies. The system cannot find the file specified..

at Microsoft.SqlServer.Dts.Tasks.WebServiceTask.WebServiceTaskUtil.Invoke(DTSWebMethodInfo methodInfo, String serviceName, Object connection, VariableDispenser taskVariableDispenser)

at Microsoft.SqlServer.Dts.Tasks.WebServiceTask.WebServiceTask.executeThread()”.

One of the possible reason for this is your TargetServerVersion have set to SQL Server 2017, set the TargetServerVersion to SQL Server 2014 you may get the issue resolved. It works for me.

Posted in .Net, Community, Computers and Internet | Tagged , | Leave a comment