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

 

Series Navigation<< C++: Voiding a function
C++: The this keyword >>