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++: 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++: Voiding a function

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

As was mentioned, a function could have a type such as int or string.

In the previous example, the SpeedCheck function was declared as an Integer type.  As you can see, this function only prints out a statement and we do not need it to return a value.

We can declare the function as void as shown in the example below.

In the class declaration, we need to specify that this is a void function as well.

#include <iostream>

using namespace std;

class Plane
{
public:

	double speed;

	void SpeedCheck(double);

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

};

void Plane::SpeedCheck(double sp)
{

	double speed = sp;

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

}

Plane::Plane()
{}

Plane::~Plane()
{}

int main()
{

	double speed = 1001;

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

void

C++: Properties of an object

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

For the plane, we have a data member called the speed and a method called SpeedCheck.  These are actually the properties of the object Plane.

There might be other properties that we use to describe a plane.  Consider the following example where we have the model of the plane.

We will develop a method where we make use of the model of the plane to determine the highest altitude that the plane could go.

#include <iostream>
#include <string>

using namespace std;

class Plane
{
public:

	double speed;
	string model;
	double height;

	void SpeedCheck(double);
	void HeightCheck(string, double);

	Plane(double, string, double);  
	~Plane();  

};

void Plane::HeightCheck(string plane_model, double ht)
{
	if (plane_model == model)
	{
		if (ht >= height)
		{
			cout << "You are going out of bound.";
			cout << endl;
		}
	}
}

void Plane::SpeedCheck(double sp)
{

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

Plane::Plane(double sp, string mod, double ht)
{
	speed = sp;
	model = mod;
	height = ht;
}

Plane::~Plane()
{}

int main()
{

	Plane iplane(1000, "Falcon", 8000); // instance declaration

	double sp = 2000;
	iplane.SpeedCheck(sp);

	string plane_model = "Falcon";
	double ht = 10000;
	iplane.HeightCheck(plane_model, ht);

	cout << endl;
}

 

So in line 62, we have declared an instance of the class called iplane.

This iplane has the following properties: speed at 1000km/h, model is Falcon and it can only go to a maximum height of 8000m.

properties

 

C++: The this keyword

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

The this keyword points to the object for which the member function is called.  I have always used it to explicitly point to the data members of the class.

Consider the following function where a variable name occurs in another part of the program.

If you remove the this pointer from the program below, it will still work correctly.

#include <iostream>
#include <string>

using namespace std;

class Rock
{
public:

	string name;
	string track;

	int Display();

	Rock(string, string);  //This is the constructor
	~Rock();  //This is the destructor

};

int Rock::Display()
{

	cout << "This is " << this->name << " Singing " << track;
	cout << endl;

	return 0;
}

Rock::Rock(string name, string track)
{
	this->name = name;
	this->track = track;
}

Rock::~Rock()
{
	cout << "This is Rod Stewart Signing Off";
	cout << endl;
}

int main()
{

	string name= "Rod Stewart";
	string track = "I was only Joking";

	Rock rockstar(name, track); // instance declaration

	rockstar.Display();

	return 0;
}

this