How to write an Object-Oriented Program?
declaration
and initialization of attributes
Part 2: using class in the main function
This
tutorial aims to give you a basic understanding of class and how it is used in
object-oriented programming.
Let’s
suppose we have a list of many (probably 1000s) newly discovered exoplanets,
and we are interested in predicting whether these Planets lie in a habitable
zone or not.
For
each Planet, we are given the following parameters:
·
Name of the Planet
·
The radius of the Planet’s Orbit
·
Associated Star Name
·
Mass of the Star
A
planet may be habitable if the radius of the Orbit of the Planet relative to
Earth is:
a)
> (Mass of the Star relative to Sun-0.1)
and
b)
< (Mass of the star relative to sun-0.2)/0.1
For more details, refer to (Seager 2013) DOI:
10.1126/science.1232226
To
write a program for such a problem. We first need to calculate the exoplanet:
· Relative Radius of
Orbit and
· Relative Mass of
Planet with respect to Earth.
And then checking whether it is in a habitable
zone.
So, we will have the following different
methods/ functions.
relative_orbit(): to
calculate the relative Orbit of the Planet with respect to Earth.
relative_mass(): to
calculate the relative mass of the associated Star with respect to the Sun.
check_habitable():
to check the condition whether it is habitable or not.
Now
we can have two approaches to solve this problem:
Approach
1: using functions (without class)
Approach2:
using class
One
way to program this problem is to use functions. First define two functions, relative_orbit() and relative_mass(),
and call these functions for each Planet to calculate relative orbit radius and
relative mass of Star. But in that case, we will have to declare and initialize
variables for each Planet. So, if we have 1000 planets, we will have
1000*4=4000 initialization.
Refer to the
Function tutorial for more detail. The code will look something like this
#include
<iostream> double relative_orbit(double
orbit_radius) // { double
Earth_orbit=149.6e6; return
orbit_radius/Earth_orbit; } double relative_mass(double
mass) { double
Sun_mass=1.989e30; return
mass/Sun_mass; } std::string
check_habitable(double
rorbit, double
rmass) { std::string status; if (rorbit>(rmass-0.1)
&& rorbit<((rmass-0.2)/0.1)) { status="habitable"; } else { status="non-habitable"; } return
status; } int main() { //Declaring and initializing variable std::string Planet1_name="X3f5"; std::string Star1_name="FF43"; double
Planet1_orbit=150.8e6; double
Star1_mass=6.7e29; std::string Planet2_name="H2k1"; std::string Star2_name="XJ85"; double
Planet2_orbit=150.8e7; double
Star2_mass=6.7e27; //…… //…… //…… //…..and so on for all each Planet! This will be time-consuming and //redundant //Now, we can call our two functions to calculate relative Orbit and
mass. //we have to define this function later double
Planet1_rorbit=relative_orbit(Planet1_orbit); double
Star1_rmass=relative_mass(Star1_mass); std::string Planet1_status=check_habitable(Planet1_rorbit,Star1_rmass); std::cout<<"Planet "<<Planet1_name<< " may be a "<<Planet1_status<<std::endl; double
Planet2_rorbit=relative_orbit(Planet2_orbit); double
Star2_rmass=relative_mass(Star2_mass); std::string Planet2_status=check_habitable(Planet2_rorbit,Star2_rmass); std::cout<<"Planet "<<Planet2_name<< " may be a "<<Planet2_status<<std::endl; /// ///similar statement can be written for each exoplanet } |
Note that using the
function here will be inefficient and lengthy. The readability of the code will
also be affected.
Let’s
see how we can use class to solve the same problem and reduce the bulk of code.
class in object-oriented programming is used
to create a blueprint or prototype of an object.
For example, a car is an object. An object has
attributes (Characteristics) and methods (functions).
**
method are fancier name for function when used within class.
Characteristics like: Brand name, Model name,
Engine Type, and Automatic/ Manual.
Functions like: Brake, Start/Stop, Accelerate,
etc.
Similarly,
in our case, we can also define a class to create each Planet as an object.
Our
planet object should also have certain attributes (or we can say specific
characteristics like Name, radius of orbit, Name of associated star, Mass of
the star) and some method (or functions like calculating relative radius and
calculating relative mass, etc.)
We
need to define a class to create a blueprint for this object.
#include
<iostream> |
pre-processor
directive for input-output stream library
using namespace std; |
Since
we are using string, cout, etc., functions, which are
part of the standard (std) library, so, we must mention the namespace. (which
is nothing, but a declarative region used to group classes, objects, or
function under a name. In our case, functions like string, cout,
and endl all belong under the std namespace.)
Ever
object-oriented code can be separated into two parts:
First
part: Definition of class.
Second
part: the main function to use the class.
Part 1:
Defining Class
class Planet //class keyword is used to define a
class followed by its name. { //starting of class |
Access Modifiers are an important aspect of
object-oriented programming to implement data hiding. Think of a car as an
example of an object. It has attributes(characteristics) and
method(functionality). Notice that not all the functionality of the car are
accessible to us. Although it may be necessary for the functioning of a car,
the manufacturer often keeps it safe and hidden. For example, we can accelerate
a car by pressing an accelerator. Still, many mechanisms used for such car
acceleration are hidden from us, and we cannot modify them.
Similarly, Object Oriented programming
allow to hide certain attributes or methods. There are three types of access
modifiers in C++
1. private: data members, i.e., attributes
and methods, are not accessible outside the class
2. public: data members are accessible
outside the class
3. protected: like Private but can be
accessed with the help of a friend class.
private: double
Earth_orbit=149.6e6; double
Sun_mass=1.989e30; public: string Name; //to store the name of Planet string Star_name; //
to store the name of Star associated with Planet double
Orbit; // to store the radius of Orbit double
Star_Mass; // to store the mass of Star string Status; // to store habitable status |
Since
we are defining Earth_orbit and Sun_mass
as private, they will not be accessible outside the class.
public: double
relative_orbit()
//method to
calculate the relative radius of orbit { return
Orbit/Earth_orbit; } double
relative_mass()
// method to
calculate the relative mass of Star { return
Star_Mass/Sun_mass; } string check_habitable(double
rorbit, double
rmass) //checking the condition //if it is habitable or not. { if (rorbit>(rmass-0.1)
&& rorbit<((rmass-0.2)/0.1)) { Status="habitable"; } else { Status="non-habitable"; } return
Status; } void
print_status() // method to print the status on screen { cout<<"Planet "<<Name<<" may be "<<
Status<<endl; } }; //
end class with curly bracket and semicolon |
Part 2: using class in the main function
int main() { double
relative_orbit;
// to store the calculated relative orbit double
relative_mass;
// to store the calculated relative mass |
Planet Planet1; // creating an object called Planet1 |
We can access the attributes of an object using object.attributes. Note we cannot access
Planet1.Earth_orbit because it is set to private.
Planet1.Name="X3f5"; Planet1.Star_name="FF43"; Planet1.Orbit=150.8e6; Planet1.Star_Mass=6.7e29; // similarly, we can access the method as an object.method() relative_orbit=Planet1.relative_orbit(); relative_mass=Planet1.relative_mass(); Planet1.check_habitable(relative_orbit, relative_mass); Planet1.print_status(); Planet Planet2; Planet2.Name="H2k1"; Planet2.Star_name="XJ85"; Planet2.Orbit=150.8e7; Planet2.Star_Mass=6.7e27; relative_orbit=Planet2.relative_orbit(); relative_mass=Planet2.relative_mass(); Planet2.check_habitable(relative_orbit, relative_mass); Planet2.print_status(); } // end of main function |