Page 1 of 1

Introduction to Abstraction Abstraction & Inheritence using C++ Rate Topic: -----

#1 alias120  Icon User is offline

  • The Sum over All Paths
  • member icon

Reputation: 122
  • View blog
  • Posts: 700
  • Joined: 02-March 09

Posted 27 June 2010 - 07:53 PM

Introduction
Scope: Abstraction using Inheritence and pure virtual functions.

Audience: Those who have a basic understanding of the concepts of Inheritence, OOP and memory reference. For more information on any of these subjects, I have additional tutorials written here -> http://www.dreaminco...Balias120sblog/

Specs: Written with the C++ Standard Library. Compiled in VS2008 C++ Express Edition.

Implementation
This tutorial 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.

//////////////////////////////////////////////////////////////
//     Example of Abstraction using pure virtual functions  //
//                                                          //
//     Using the C++ Standard Library                       //
//                                                          //
//     Compiled in VS2008 C++ Express Edition				//
//															//
//                                                          //
//////////////////////////////////////////////////////////////


#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;

}



Is This A Good Question/Topic? 0
  • +

Page 1 of 1