The basic concept of data hiding is shown in figure 7-1.
CLASS
Figure 11-1
For OOPS, you need to know the following concepts.
1. Objects
2. Data abstraction 3. Inheritance
4. Dynamic binding 5. Classes
6. Data encapsulation 7. Polymorphism
Please note that I shall make no attempt to teach you Object Oriented Programming from scratch in this book. I expect that you already have at least basic idea of what OOP is all about. However, I shall help you to clear many of your doubts normally you might have with OOP.
We shall discuss various aspects of Object Oriented Programming throughout this book!
Data
Functions
Data
Functions Private
Area No Entry
Public Area Entry Allowed
Basically you can assume that Class is a template from which Object is created.
Classes have no existence of their own. They are like blue prints of plan from which buildings (i.e. Objects) are made.
Different programs handle object-oriented concept differently. Here, I shall show you a program in C++, which incorporates a number of OOP concepts.
Please note that if you are new to OOP, it may seem complicated at first (I felt the same, too). But later you will definitely realize that OOP is for simpler programming. That’s why it is so popular nowadays. OOP programs are easier to write and maintain as well.
Code 11-1 illustrates an OOP concept. First we define class ‘Car’. Then we define ‘Truck’ by extending ‘Car’. Then we define ‘Bird’. A bird has wings and it can ‘Fly’. Next we make ‘Aeroplane’. It derives from ‘Car’ and ‘Bird’. (I won’t blame you if you raise doubt about my sanity after finishing this chapter, but things are really so wonderful. Believe me!)
A car has engine, wheels, doors etc. So does an aeroplane. A bird has wing and it flies by flapping its wings. An aeroplane also has wings (made of metal) and it also flies through starting engine, taxing, taking off and navigating instruments (wow)!
So, we need to tell our program that both ‘Bird’ and ‘Aeroplane’ can ‘Fly’ but their flying procedure is different from each other!
Well, enough talking, study the following C++ code.
Code 11-1
// demonstration of Object Oriented features of C++
// a car building program
#include <iostream.h>
#include <string.h>
// define engine components struct engine
{
float displacement_L;
float Power_kW;
int cylinder;
char fuel[7];
int valve;
char transmission[10];
};
void InstallEngine(struct engine);
void FitBrake(char[]);
void PaintBody(char[]);
};
// Truck extends Car class Truck:public Car
// Bird is something which can fly by flapping wings class Bird
// Aeroplane extends from Car and Bird
// ie it has doors, wheels, engine etc. and it can fly // like bird, the flying method is different from that // of a bird
class Aeroplane: public Car, public Bird {
public:
void Fly(int);
};
// now define the functions for Car
void Car::MakeDoor(int door) {
doors = door;
cout << doors << " doors" << endl;
}
void Car::FitWheels(int wheel) {
wheels = wheel;
cout << wheels << " wheels" << endl;
}
ee.displacement_L = e.displacement_L;
cout << ee.displacement_L << " L engine displacement" <<
endl;
ee.Power_kW = e.Power_kW;
cout << ee.Power_kW << " kW Power" << endl;
ee.cylinder = e.cylinder;
cout << ee.cylinder << " cylinders" << endl;
strcpy(ee.fuel,e.fuel);
cout << ee.fuel << " engine" << endl;
ee.valve = e.valve;
cout << ee.valve << " valves" << endl;
strcpy(ee.transmission, e.transmission);
cout << ee.transmission << " transmission" << endl;
cout << brake << " brake" << endl;
}
void Car::PaintBody(char b[]) {
strcpy(color,b);
cout << color << " color" << endl;
}
// now define the functions exclusive for Truck
void Truck::FitLoader(float LoadVolume) {
LoadSpace = LoadVolume;
cout << LoadVolume << " cubic meter payload" << endl;
}
// function for bird
void Bird::MakeWing(float wingspan) {
WingSpan_m = wingspan;
cout << WingSpan_m << " wing span" << endl;
}
// birds fly by flapping wings void Bird::Fly(void)
{
cout << "Bird is flying by flapping wings..." << endl;
}
// function for Aeroplane
// aeroplane's complicated Fly should override bird's Fly method
void Aeroplane::Fly(int speed) {
cout << "starting engine..." << endl;
cout << "taxing..." << endl;
cout << "taking off..." << endl;
cout << "Flying aeroplane at speed of " << speed << " km/h.
Whoosh..." << endl;
}
// main function begins here
cout << "Assmebling MyCar..." << endl;
// assemble the car based on parameters supplied // attach doors
MyCar.MakeDoor(4);
// fit wheels
MyCar.FitWheels(4);
// engine specification engine ce;
ce.displacement_L=1.5;
ce.Power_kW=100;
ce.cylinder=4;
strcpy(ce.fuel,"petrol");
cout << "Car is ready. Have a safe driving..." << endl;
// now make a truck
cout << "Now we are making a Truck..." << endl;
cout << "Assmebling MyTruck..." << endl;
// engine specification engine te;
cout << "The truck is ready... Do you want to carry your car?" << endl;
// create the aeroplane Aeroplane MyPlane;
The output of the above program will be as shown below.
Assmebling MyCar...
4 doors 4 wheels
1.5 L engine displacement 100 kW Power
Car is ready. Have a safe driving...
Now we are making a Truck...
Assmebling MyTruck...
2 doors 6 wheels
6 L engine displacement 250 kW Power
8 cylinders diesel engine 16 valves
manual transmission 100 cubic meter payload drum brake
The truck is ready... Do you want to carry your car?
starting engine...
taxing...
taking off...
Flying aeroplane at speed of 300 km/h. Whoosh...
Hmm, pretty complicated code, eh? Study it carefully and you will find this simple code demonstrates function overriding, simple and multiple inheritance etc. This code is actually rather straightforward. If you just read the comments and follow the code thereafter, everything will be transparent to you.
I am providing the equivalent Java code here.
Code 11-2
// demonstration of Object Oriented features of Java // a console mode program
// main class is oopsdemo
// note: a separate .class file will be generated for each class
// name of source code file is oopsdemo.java // define class Car
class Car {
private int wheels;
private int doors;
private float displacement_L;
private String color;
public void MakeDoor(int door) {
doors = door;
System.out.println("doors = " +doors);
}
public void FitWheels(int wheel) {
wheels = wheel;
System.out.println("wheels = " +wheels);
}
public void InstallEngine(float displacement, float power, int cylinderno, String fueltype,int valveno, String
transmissiontype) {
displacement_L = displacement;
System.out.println("displacement is " +displacement_L);
Power_kW = power;
System.out.println("Power is " +Power_kW);
cylinder = cylinderno;
transmission = transmissiontype;
System.out.println("transmission " +transmission);
}
public void FitBrake(String braketype) {
brake = braketype;
System.out.println("brake = " +brake);
}
public void PaintBody(String paintcolor) {
color = paintcolor;
System.out.println("color = " +color);
}
} // end of class Car declaration class Truck extends Car
{
private float LoadSpace;
public void FitLoader(int loadvolume) {
LoadSpace = loadvolume;
System.out.println("Load space = " +LoadSpace);
}
} // end of class Truck declaration // beginning of main class
class oopsdemo {
public static void main(String args[]) {
Truck MyTruck = new Truck();
System.out.println("Assembling truck...");
Please note that, the above Java code is not an exact equivalent of C++ code shown in code 7-1. Here I showed only one extension of class i.e. from Car to Truck. Since Java does not support multiple inheritance, it is not possible to include Bird and Aeroplane classes here as those of in C++.
Now here is the VB.NET code for class (in file say VehicleClass.vb).
Code 11-3
Namespace Vehicle
Public Class CVehicle
Inherits System.ComponentModel.Component
Private wheels As Integer method and try it. If it fails, admit it frankly and try another. But above all, try something.
Public Sub PaintBody(ByVal MyColor As String) color = MyColor
MsgBox("Color = " & color, MsgBoxStyle.Information, "Vehicle") End Sub
Public Sub InstallEngine(ByRef EngineData As Engine)
VehicleEngine.displacement_L = EngineData.displacement_L VehicleEngine.power_kW = EngineData.power_kW
MsgBox("Displacement " & VehicleEngine.displacement_L & " L", MsgBoxStyle.Information, "Vehicle")
MsgBox("Valve " & VehicleEngine.valve, MsgBoxStyle.Information,
"Vehicle")
VehicleEngine.transmission = EngineData.transmission MsgBox("Transmission is " & VehicleEngine.transmission, MsgBoxStyle.Information, "Vehicle")
End Sub
#Region " Component Designer generated code "
'Required by the Component Designer
To use it in a client project, you can call it in following way.
Private Sub cmdMakeCar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdMakeCar.Click
Dim MyCar As New VehicleClass.Vehicle.CTruck()
MyCar.FitWheels(4) MyCar.MakeDoor(4) MyCar.FitBrake("disc")
Dim MyEngine As VehicleClass.Vehicle.CCar.Engine
MyEngine.cylinder = 4
MyEngine.displacement_L = 1.1 MyEngine.power_kW = 50
MyEngine.fuel = "petrol"
MyEngine.valve = 16
MyEngine.transmission = "RWD"
MyCar.InstallEngine(MyEngine)
MyCar.PaintBody("green") End Sub
Private Sub cmdMakeTruck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdMakeTruck.Click
Dim MyTruck As New VehicleClass.Vehicle.CTruck() MyTruck.FitLoader(100)
End Sub
So, mind boggling, eh? Have fun!
Comparison of OOPS features
Interface No (though
abstract class is there)
Yes Yes
Function overloading Yes Yes Yes *
Function overriding Yes Yes Yes *
Polymorphism Yes Yes Yes (through
interface)
* Indicates feature either unavailable in earlier versions or was implemented differently. For example, simple inheritance was supported in VB5 onwards though you needed to write function/procedure bodies yourself in derived classes.