Some time ago, after coming into
contact with the idea of object oriented programming I began to
wonder about what a real object was. This led me on the journey that
I here attempt to take you on. The philosophical idea in question is
'what is an object?' This is actually a problematic question; I asked my friend and philosopher, Mr. Rodney Chlebek, this question, and in response he wrote quite an interesting post in which he argues that their are no objects bigger than particles. In an esoteric way I certainly might agree, although I definitely am not enough of a philosopher to have a very educated response there. That said, I do believe that objects do exist in a sense that Chlebek is not examining. I am not per se disagreeing, actually I am extending his argument. I believe that objects, like bowls and plates, exist in a special virtual world of cognition where an object is nothing more than an instance of a class. In this virtual world, objects exist in the brains of humans as well as in the computers we create. Let's examine this claim more deeply...
Does this plate really exist? How do I know it is a plate? |
Lets use the "real" plate object again and describe it with the new vocabulary we just introduced: The specific object you hold and call a 'plate' is an object that is an instance of the plate class, yet it also belongs in the table-setting class, the kitchen class, and the pottery, glass or metal class depending on its construction. What is fascinating is that it seemingly takes little intelligence or insight to arrive at these conclusions, despite the fact that in these cognitive processed are ideas of class inheritance, and other equally interesting and abstract ideas.
Object oriented programming is a great allegory for thinking about things, or like I said above, if it is taken like Chlebek and I have stated, maybe the allegory IS reality. I know that speaking of psychology and philosophy at this junction may be quite bizarre, but object oriented programming and the cognitive phenomena of objects are really on this fine line between philosophy, psychology, and computer science. In keeping with the ideas in object oriented programming, I believe that a class of objects is a collection of distinct entities that share a certain defined set of attributes and actions or purposes. What are attributes and actions as I am using the words?
To me an attribute is some characteristic part of an object. I may be a curve, a bump, some physical property, or dimension or ratio of dimensions.
In contrast to an
attribute, an action is some distinct purpose that the object
has or some motion it makes or work it performs. A plate holds food,
and that is the action for which we most often purchase a plate. A
car makes motions that enable us to move between isolated points.
This too is an important action.
This definition is broad enough to work for both "real" objects and the "virtual" objects that we build in object oriented programming. Real objects have both attributes and actions, as do virtual objects. Since we are speaking of object oriented programming, lets see how this works in pseudo code, and then move on to real code. In object oriented programming, we
identify instances of an object of a class as having data members and
functions or methods. In general, the data members are
variables of some data type, and these are analogous to the
attributes we discuss above. The functions of our class take these
data members and attributes and perform actions with them. This means
class Some_Object
{
Attributes; // Think "Data members" (variables)
Actions; // Think "Member Functions"(Methods)
}
class Plate
{
//Attributes ("Data members")
plate_shape;
size_xAxis;
size_yAxis;
size_zAxis;
plate_material;
//Actions ("Functions")
carry_food();
stength();
}
class Bowl
{
//Attributes ("Data members")
bowl_depth;
//Actions ("Functions")
carry_soup();
}
Using Objects in the C++ Language
Why you ask though would I want to make a virtual object through object oriented programming? Well, the reasons are diverse, but maybe I need to store multiple definitions of an employee, a box, or some other thing. Using OOP, I can make this into an object. In programming, this object is a new data type, and is sometimes called an abstract data type. In order to understand classes better, lets define a box class in some detail now. Below is the code for the C++ header file for a box class:
#ifndef Box_Class
#define Box_Class
#include <iomanip>
#include <iostream>
using namespace std;
class box
{
private:
string type;
int length;
int width;
int height;
int box_volume;
public:
void output_value();
int volume();
string units(int);
float volume_units(int);
void input_variables();
};
#endif
Notice the simplicity of this declaration; aside from the statements preceded by # signs (preprocessor directives) which are not necessary to understand the concept, this is a simple class. I left the preprocessor directives in the file in order to allow you to copy this code and run it if you chose to. If you copy it, name it "box_class.h" so that when I later show how to compile it, you can follow along. If you want to download the code, the links to these files is at the end of the document. To see how these functions are implemented, here is the source file that defines the functions declared above. A header file and an implementation or source file together make up a class.
Notice that preceding every function is the box:: syntax. This tells the compiler that this function is part of class box. If you want to compile this project, open your favorite text editor and save this as "source_Box_class.cpp". My favorite text editor is gedit, an open source text editor that works in Linux, Windows, and Mac. Gedit is a project of gnome.org, an organization that promotes free software and makes the Gnome GUI for Linux. To get gedit on an Ubuntu Linux machine run the following in the terminal: #include "box_class.h"
#include
#include
using namespace std;
void box::output_value()
{
cout<< "The box is a " << type << " type box,the length is " << length;
cout<< " inches, the width are " << width;
cout << " inches, and the height is " << height << " inches." << endl;
}
int box::volume()
{
int volume_inches;
volume_inches = length * width * height;
return volume_inches;
}
string box::units(int volume_inches)
{
string units;
if(volume_inches > 1728) {units = "feet";}
else units = "inches";
return units;
}
float box::volume_units(int volume_inches)
{
int volume_inch;
float volume;
volume_inches = length * width * height;
if(volume_inch > 1728)
{
volume = (float)volume_inches/1728;
}
else
{
volume = volume_inch;
}
return volume;
}
void box::input_variables()
{
cout<< "What type is this box?" << endl;
cin>> type;
cout<< "What size is the length?" << endl;
cin>> length;
cout<< "What size is the width" << endl;
cin>> width;
cout<< "What size is the height" << endl;
cin>> height;
}
sudo apt-get install gedit
sudo apt-get install gedit-plugins
#include "box_class.h"
#include "box_class.h"
#include
using namespace std;
void input_value();
string type;
int length;
int width;
int height;
int main()
{
int size;
cout << "How many times do you want to run this" << endl;
cin >> size;
int volume[size];
box this_box[size];
// Must not be (i <= size) or else size =2, and loop iterates 3x
for( int i = 0; i < size; i++)
{
this_box[i].input_variables();
this_box[i].output_value();
volume[i] = this_box[i].volume();
cout<< "The box has a volume of " << std::setprecision(3) << this_box[i].volume_units(volume[i]);
cout<< " cubic " << this_box[i].units(volume[i]) << " ." << endl;
}
return 0;
}
sudo apt-get install build-essential
g++ main_classBox.cpp source_Box_class.cpp -o boxClass.exe
./boxClass.exe
After running this command, you should see something similar to the following:
Running the box class program in terminal after compilation. |
Conclusion
Making a virtual object is a useful technique in a programming language that supports object oriented programming. By understanding it as a way of seeing familiar objects, one can better analyse the real world and can be better able to use OOP as a method.Links:
My HomepagePage on my Website dedicated to hosting the files for this post
main_classBox.cpp
source_Box_class.cpp
box_class.h
All of the above are available under the GPL-3.0 licence by the author. Please contact me for permission if this code will be used for other than not for profit educational reasons. Do not republish without permission.
No comments:
Post a Comment