C++: Another example on creating an object

This entry is part 32 of 61 in the series C++

Consider this example where we want to create a warning when a plane is travelling too fast.

We will call this method SpeedCheck.  As we are using this method on a plane, we put this method under the Plane class.

#include <iostream>

using namespace std;

class Plane
{
public:

	double speed;

	double SpeedCheck(double);

	Plane();  //This is the constructor
	~Plane();  //This is the destructor

};

double Plane::SpeedCheck(double sp)
{

	double speed = sp;

	if (speed >= 1000)
	{
		cout << "You are going interstellar";
	}
	else
	{
		cout << "You are doing just fine";
	}

	return 0;
}

Plane::Plane()
{}

Plane::~Plane()
{}

int main()
{

	double speed = 1001;

	Plane Plane1; // instance declaration
	Plane1.SpeedCheck(speed);
	cout << endl;

}

C++: Work out the method first

This entry is part 31 of 61 in the series C++

Methods of a class is just like functions in a procedural language.  In a procedural language, we just have to think of the procedures and it makes procedural language such as C to be rather straight forward.

In C++, a method is just a part of an object.  Once you have thought of a method, you still need to work out the bigger picture, that is, the object associated with this method.

Creating an object is like another layer that we need to deal with.  This is what knocks us off when we first started learning object-oriented programming.

As mentioned, designing an OOP program is like working out a sentence with the noun to be in front of a verb.

We are led to believe that we need to first have a clear concept of the object before designing the class.

I have like to suggest that just like conventional procedural programming language,  we first work the method rather then the object.

Let’s go back to our previous example.  The example calculate the number of days needed for a train to travel from one city to another.  We obviously would need a method called Days that can just do that.

Once the method is worked out, it is easy for us to see that we need a train class with 2 data members distance and speed for this class.

 

 

 

 

 

 

C++: Creating an object

This entry is part 30 of 61 in the series C++

We will use the train as an example and create a train object with relevant method associated with the object.

Lets say we need to calculate the number of days that we need to travel between 2 cities. We would need the speed of the train and the distance between the 2 cities.

#include <iostream>
#include <string>

using namespace std;

class Train
{
public:

	double speed;
	double dist;


	double Days(double, double);

	Train();  //This is the constructor
	~Train();  //This is the destructor

};

double Train::Days(double sp, double dist)
{

	double speed = sp;
	double distance = dist;

	return (dist/sp)/24;
}

Train::Train()
{}

Train::~Train()
{}

int main()
{

	double speed = 10;
	double distance = 1000;

	Train Train1; // instance declaration
	cout << "No of days: " << Train1.Days(speed,distance);
	cout << endl;

}

In this example, there are 2 data members speed and train.  In our constructor, we did not initialize them within the constructor.  We are assuming that the values can change.

So in line 42, we declare the instance Train1 without passing in any parameters.  As a result, in our method Days, we would need to pass in the 2 parameters speed and distance.

createobject

C++: An instance of an object

This entry is part 22 of 61 in the series C++

It is easy for us to understand that an object is an entity.

When it comes to an object-oriented program, the emphasis now is on the object.  In a procedural programming such as PASCAL or Fortran, we would exchange data using variables.

Since an object is an entity, can we pass an object from one object to another?

Well, can can do that using an instance of the object.  Let’s consider the example below.

#include <iostream>
#include <string>

using namespace std;

class Country
{

public:
	string name;
	int noAlbum;
	string latest;

};

int main()
{

	Country ts; // instance declaration

	ts.name = "Taylor Swift"; // member initialization
	ts.noAlbum = 5;
	ts.latest = "1986";

	cout << "\n Country Singer = " << ts.name;
	cout << "\n No of Album = " << ts.noAlbum;
	cout << "\n Her latest album = " << ts.latest;
	cout << "\n";

	Country ts1;

	ts1 = ts;

	cout << "\n Country Singer = " << ts1.name;
	cout << "\n No of Album = " << ts1.noAlbum;
	cout << "\n Her latest album = " << ts1.latest;
	cout << "\n";

	return 0;
}

 

In the example above, we have used the first instance ts to hold the first set of data.  ts is an object in itself but in OOP, we would call it an instance of the class Country.

Now after we have created another instance of the Class Country ts1, we are able to use an assignment operator = to assign an object from ts to ts1.

Notice when we print out the data members of ts1, it has the same set of data as ts.instance

C++: Classes and objects

This entry is part 19 of 61 in the series C++

A class contains data and functions. A class could be understood as the object in an object-oriented program.

When we are defining a class, we are actually defining an object.

In object-oriented concept, or in any other object-oriented programming languages, we would usually call a function a method of the class.

As was mentioned, a class contains data members and methods.  The reason that we called them methods is mainly because it is through the methods that we act on the data in the class.

Data members could just be any integer or string which is similar to a variable in a procedural language.