Total members 11895 |It is currently Sun Dec 22, 2024 7:55 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Attachment:
beginneroop.jpg
beginneroop.jpg [ 36.96 KiB | Viewed 5087 times ]

This article are the Object Oriented features in C++. What are the advantages of it, and presents examples on it:
What is Object Orientation (OO)?


OO is a big improvement in the organization of code. Object Oriented Programming (OOP) is kind of a philosophy. Let me explain: suppose there is a company, a BIG company. And every employee in this company has access to everything (is it still a company?). Even the cleaner has access to the science department ready to mess around with some dangerous toxins. Luckily, everyone is very faithful and will not do such a thing someday soon. Then, one day, after this company grows even bigger and more complex, the whole organization collapses. Quickly now, what's the problem here? Exactly, the organization has become too complicated. Everything is confusing, the scientist couldn't find the toxins anymore because the cleaner messed around with them. The salesmen went crazy because the telephonist accidentally sold everything, etc.

Now, think of this company as a program. This program has no real structure. Everything knows everything, this is not much of a problem in a simple Hello World program. But like I said, this is a BIG program (company). The cleaner, scientist, salesmen, and the telephonist in this program are functions. And the toxins and the products for sale are the data. There is of course much more, use your imagination. The point here is that every function has access to all data, which is the case in procedural languages like C (although C has some features to prevent this, they aren't much I guess). Meaning that, in a situation, data could easily be lost or corrupted when a new function is made. And if a data item needed to be edited, you would also have to edit every function which works with that particular data item... This is not very fun when you have worked on a program for like let's say... a few months.
OO to the rescue!

This is why OO is here, to make irrational programs like the one above more rational. To make the chaotic ordered etc. Let's use our imagination again... suppose there is a company, a BIG company. This company is very successful and is divided into multiple departments. Each department has its own purpose and its own security measures. This time, the telephonist cannot sell any products because all the products are located in a storage facility to which only the salesmen have access (HAH!). And the cleaner doesn't have access to the science department either, because only the scientists have the key to it! This, can be called a company. There is a rational organization with security measures to prevent any corruption or sloppiness within the company. Now, the boss (yes, this time there is a boss) can go home relaxed without having to worry about the wild fantasies of the cleaner. One word: OOP. We could do this to the program by using classes. This is what we're gonna do.
The Company example
OOP-less

Let's take a look at what disaster could occur in a program without OO. (The example is small and unreal, but it will help you get the point). Note: it is still written in C++ but without the OO concept in mind.

cpp code
//FILE: Evil_Workers.cpp


#include <iostream.h>

#include <conio.h> //for getche


const int LastChecked = 10; //unaltered value


int toxin = LastChecked; //toxin data item

int products = LastChecked; //products in stock


void cleaner()
{
cout << "\nHAHAH I AM AN EVIL CLEANER!!\ntoxin = " << toxin
<< "\n*Alters toxin!*\n"; //yes he is evil!

toxin += 2; //cleaner CAN alter toxin!

cout << "toxin = " << toxin << endl;
}

void telephonist()
{
cout << "\nHAHAH I AM AN EVIL TELEPHONIST!!\nproducts = " << products
<< "\n*Sells all products!*\n"; //NOOOOO!!

products -= 10; //telephonist CAN sell products!

cout << "products = " << products << endl;
}
void scientist()
{
cout << "\nScientist:\n";
//scientist checks if toxin is still unaltered

if(toxin == LastChecked)
cout << "I'm glad nobody messed with my toxin!\n";
else
cout << "Oh my god, somebody messed with my toxin!!!!\n";
}
void salesman()
{
cout << "\nSalesman:\n";
//salesman checks if no products are sold

if(products == LastChecked)
cout << "I'm glad nobody sold anything!\n";
else
cout << "Oh my god, somebody sold stuff!!!!!\n";
}

void main()
{
scientist(); //scientist checks

salesman(); //salesman checks

cleaner(); //cleaner alters

telephonist(); //telephonist sells

scientist(); //scientist checks

salesman(); //salesman checks

cout << "\n\nPress enter."; cout.flush();
getche(); //just so the program doesnt terminate immediatly

}

As you can see here, toxin and products are defined globally. Meaning they are accessible by any part of the program. So now, cleaner() and telephonist() can both access these variables. Which is of course what we don't want. We only want scientist() and salesman() to be able to access these variables. Then again, we don't want scientist() to be able to mess with products and the same goes for salesman() and toxin.

There is another problem with the above code. The program doesn't really fit in real life situations. Take scientist(), it is a function. Now in real life, a scientist would be more of an object instead of a function. And the scientist checking up on the toxins would be the function of the scientist. So how does OO solve all this? In C++, it solves this by using classes.

Classes example
cpp code
//FILE: Good_Workers.cpp

#include <iostream.h>

//a class named Scientist will be defined here
class Scientist
{
private: //important!
int toxin;
public:
//constructor with initialization list
Scientist(int Toxin_Value) : toxin(Toxin_Value)
{}

//member functions which do something with toxin
int GetToxin() //return the value of toxin
{
return toxin;
}
void AlterToxin(int Toxin_Value) //set toxin to Toxin_Value
{
toxin = Toxin_Value;
}
};

//for OO's sake let's create a Cleaner class
class Cleaner
{
public:
void Evil_Altertoxin(Scientist& victim, int value)
{
victim.toxin = value; //this will generate a compiler error
}
};

void main()
{
const int DaveVar = 10; //correct toxin value
Scientist Dave(DaveVar); //create object Dave
Cleaner John; //create object John

//Dave checks to see if toxin is still unaltered
cout << "\nScientist Dave:\n";
if(Dave.GetToxin() == DaveVar)
cout << "I'm glad nobody messed with my toxin!\n";
else
cout << "Oh my god, somebody altered my toxin!\n";

//John attempts to alter toxin
cout << "\nEVIL Cleaner John:\nLet's try to alter Dave's toxin!";
John.Evil_Altertoxin(Dave, 0);

//just for demonstration
toxin = 0; //another compiler error
}


cpp code
//FILE: Templates.cpp

#include <iostream.h>
#include <conio.h> //for getche

template <class T> //warning: translate class as type!
T SqrNum(T argNum)
{
return (argNum * argNum); //return type is of T
}

void main()
{
int inum = 2;
long lnum = 4;
double dnum = 5.6;

cout << "inum * inum = " << SqrNum(inum) << endl
<< "lnum * lnum = " << SqrNum(lnum) << endl
<< "dnum * dnum = " << SqrNum(dnum) << endl << endl
<< "Press enter."; cout.flush();
getche(); //just so the program doesnt terminate immediatly
}





Attachments:
beginneroop_demo.zip [42.73 KiB]
Downloaded 809 times
beginneroop_src.zip [7.04 KiB]
Downloaded 834 times

_________________
Please recommend my post if you found it helpful. ,
java,j2ee,ccna ,ccnp certified .
Author:
Expert
User avatar Posts: 838
Have thanks: 2 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Object Orientation in c++, how to a do object Orientation
 use out object in a method at jsp     -  
 Connection object in jsp     -  
 3D-object loader     -  
 How to use connection object in jsp     -  
 object detection     -  
 Object without new Operator     -  
 need help about object detection using haarTraining     -  
 HOW TO KEEP OBJECT MOVING FOLLOW.     -  
 Change the shading of object     -  
 Add texture to teapot object using     -  



Topic Tags

C++ OOP
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com