Advertisment

Programming in OOPS 

author-image
PCQ Bureau
New Update

You would have noticed that in the main article, I have not included any code at all in trying to explain the concepts. This was done intentionally so as not to deter any non-programmer from reading and understanding the concepts behind OOPS. But for the codeheads amongst you, the box below should give you a taste of the object side.

Advertisment

Let us begin with a simple example. We’ll use C++ as the language of choice here. I’ll skip the standard ‘Hello, World’ example and move on to ones that start demonstrating the features of object orientation. Let’s create a program to classify cars and calculate their mileage. We will first create classes for each object–wheels, engine and body.

class wheel {



int diameter;


public:


wheel(int dm) { diameter = d; }


}


The above code defines an object called ‘wheel’ that has one hidden or private attribute called ‘diameter’, and one method called ‘wheel’. This is a special method called a ‘constructor’ that is run automatically every time an instance of the object is created. 

Advertisment

class engine {



int type;


public:


int capacity_cc;


engine (int t) { type = t; }


}



Another object, this time having one hidden attribute, one public attribute and one constructor. 

class car {



wheel f1, f2, r1, r2; // Front and rear wheels


engine e1;


public:


car (int dm, int t, int capcc) {


f1(dm); f1(dm); r1(dm); r2 (dm); e1(t);


e1.capacity_cc = 1000;


}


}






Advertisment

And finally, the car itself. This object gets all the properties of both wheel and engine as it contains objects for each of them.

As you can see, the car object is able to initialize the wheel and engine object within its own constructor. 

Creating a car object is as simple as declaring it with three parameters like this:

Advertisment

car myCar(40, 2, 1000);






This creates a new car instance with a wheel diameter of 40, a type 2, 1000cc engine. You can create multiple instances of this structure if you so desire with different parameters.

To demonstrate the use of such a thing, let us add one more method to the car object called calcMileage(). This changes the car class structure to include:

int calcMileage() {



return (e1.capacity_cc/100);


}

Advertisment

This is not a true mileage calculation formula, but it is only meant as an example. So now if you have two car objects, one of capacity 1000 cc and another of 800 cc, calling the calcMileage method on each of them and the mileage returned will be that of the capacity stored for each. This demonstrates how each instance of the object contains its own copy of the structure and member functions, and has the corresponding values for them individually. You can also see how one object can use other objects and inherit their properties.

Hopefully, the coders among you who have not yet jumped into OOPS development yet, can take a deeper look at it and learn to think in terms of objects and their processes rather than a top-down process approach. As mentioned in the main article, code reuse is just one of the benefits you can get from OOPS.

Vinod Unny

Advertisment