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++: Reusing a method

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

The original designer of C++ reasoned that OOP is well suited for building large scale project.

Some of the core ideas of OOP such as encapsulation and inheritance are all meant to make it easy for us to build large program and to reuse the code we have built.

This example shows how the same method is used.  We actually need to create a second instance to hold the data and to call the method under this second instance.

#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 " << name << " Singing " << track;
	cout << endl;

	return 0;
}

Rock::Rock(string name2, string track2)
{
	name = name2;
	track = track2;
}

Rock::~Rock()
{
	cout << "And This Is The End";
	cout << endl;
}

int main()
{

	string name1 = "Rod Stewart";
	string track1 = "I was only Joking";

	Rock rockstar(name1,track1); // instance declaration
	rockstar.Display();

	string name2 = "Metallica";
	string track2 = "Enter The Sandman";

	Rock rockstar2(name2, track2); // instance declaration
	rockstar2.Display();

	return 0;
}

reuse

 

C++: Implementing a method

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

In our earlier examples, there is only data members in our class.  Using our previous example, notice that in the main, we have a few cout statements to print out the strings.

Say we are working on a program where we need to retrieve data and display similar statements.  Developing a method in our class will make a lot of sense.

In this example, we work out a method called Display for the class.

#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 " << name << " Singing " << track;
	cout << endl;

	return 0;
}

Rock::Rock(string name2, string track2)
{
	name = name2;
	track = track2;
}

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

int main()
{

	string name1 = "Rod Stewart";
	string track1 = "I was only Joking";

	Rock rockstar(name1,track1); // instance declaration

	rockstar.Display();

	return 0;
}

Once the method is created, we will first have to declare an instance for the class Rock.  In our case, we use rockstar as the name of the instance.

In line, we have the actual method implementation.  The method needs to be scoped under the class Rock since the method belongs to this class.

To access the method, we  need to use the dot(.) operator as shown below.

rockstar.Display();

method

C++: Methods of a class

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

Earlier, we mentioned that a class consists of mainly the data members and methods.  As was shown, constructor or destructor are not something that is needed in order for a class to work.  You can choose not to declare them.

If they are not declared, the C++ program or the compiler will lazily create them for us.

In OOP, we sometimes talk about the characteristics of an object.  Those characteristics are the data members and the methods of a class.

Let’s consider using the train as an object.  We might have a data related to its speed and we might have a method that help us to calculate the no of days it takes to travel from one city to another.

Sometimes it is hard to work out the methods that are needed.  There were some who said this is designing the noun before coming out with the verb and it is something that we need to get use to.

When it comes to OOP, we talk about the re usability of a class.

At times, we might have designed a method that we use only once in our program.  This is actually ok as we can always improve them in our development process.