Subscribe to Invariance_Under_Translation        RSS Feed
-----

Introduction to Abstraction

Icon Leave Comment
This entry is designed to give a basic overview of one of the many uses of Abstraction. By designing your program to take advantage of abstraction, you are decreasing the amount of overhead by "hiding" implementation. I will use a purely logical example to illustrate this point.

You have an office building with many different departments. You could consider this an example of modularity. A member of the sales department delivers the results of their monthly figures to the finance department. This is where the abstraction takes place. The sales department does not need to know the details of how the finance department implements the information given to them. This keeps the sales department from having to compute the figures themselves, thus decreasing their workload.

This also allows for another aspect of Abstraction to be implemented. Extensibility. The layout of our company allows for additional departments to be created without effecting the existing ones. This is one of the major strong points of abstraction.

The program below illustrates this. A base class of shape has two classes inheriting it's pure virtual functions, square and triangle. Traditionally, if we wanted to use the functions of any of the inherited classes we would need to initialize an object of that class, and then directly call the function itself. What if we could just create one function that would pass a reference of whatever object we needed to use, and have Inheritence take over? That is exactly what you will see here. The program is documented throughout to assist in further explanation.

#include<iostream>

using namespace std;


class Shape  //Base class, uses pure virtual functions. Cannot be
{			 //initialized by a direct object

protected:

	int height;  //Protected variables, only for use by derived classes
	int width;
	int area;

public:
	
	virtual void setParam(int d_h, int d_w) = 0;  //Define pure virtual functions with
												  //assignment of zero. This is essentially
	virtual int getArea() = 0;					  //creating a blueprint, saying these functions
												  //will be defined elseware
};




class Square : public Shape		//A square is a shape, so we inherit shapes member variables 
{								//and functions

public:

	Square(){};
	~Square(){};

protected:

	void setParam(int d_h, int d_w)  //A square has an area unique to other shapes, so
	{								 //we use Shape's member variables  to 
		                             //define it's implementation here. 
		height = d_h;
		width = d_w;
		area = height * width;
	}


	int getArea()   //Defining the implementation of getArea() to return a 
	{				//squares area.
		return area;
	}

};





class Triangle : public Shape   //A Triangle is also a Shape, so we inherit from Shape as well
{

public:

	Triangle(){};
	~Triangle(){};

protected:

	void setParam(int d_h, int d_w)	//A Triangles area is unique from other Shapes, 
	{								//so we implement setParam()'s details here. 
		height = d_h;
		width = d_w;
		area = (height * width) / 2;
	}

	int getArea()
	{
		return area;
	}

};





void passParam(Shape &s, int b_h, int b_w)  //The real impact of Abstraction, instead of defining 
{											//objects of each Shape type we are able use a Shape reference, 
	s.setParam(b_h, b_w);					//and through the use of inheritence whichever class object we pass
}											//in will have our variables passed to it or our class functions called.

void callArea(Shape &t)
{
	cout<<t.getArea()<<endl;
	cout<<endl;
}




int main()
{
	Square obj2;
	Triangle obj3;

	passParam(obj2, 2, 4);	//Notice that through the use of abstraction we
	passParam(obj3, 2, 4);	//only need call our one function using an object of
							//whichever class we need to utilize. 
	callArea(obj2);
	callArea(obj3);

	cin.ignore();
	cin.get();

	return 0;

}

0 Comments On This Entry

 

Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

December 2014

S M T W T F S
 123456
78910111213
141516171819 20
21222324252627
28293031   

Tags

Recent Entries

Recent Comments

Search My Blog

0 user(s) viewing

0 Guests
0 member(s)
0 anonymous member(s)