C++: Assessing class members from main

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

We have always say that a public member of a class can by assessed by any functions.

A good way to look at it is in accessing it from main function.

In main, only public members can be accessed.  It would generate an error if you try to access a private or a protected member.

#include <iostream>

using namespace std;

class Music {
    
public:
    int x;
protected:
    int y;
private:
    int z;
    
};

int main() {
  
    Music music;
    
    music.x = 6;
    cout << music.x;
    
    // Main will not be able to access y & z.
    // music.y = 8;   //error
    // music.z = 10; //error
    
}

public

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++: 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.

 

 

C++: The actual constructor

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

As shown the previous example, we do not actually need a constructor to initialize member data.

Constructor is created to better manage memory allocated for an object.  There is also a Destructor meant for releasing the object back to the memory stack and we will go through that in the later posts.

An example of a constructor is as follows.  Class Country has three member variables. The declaration of constructor can be done inside the class and definition outside the class.

#include <iostream>
#include <string>

using namespace std;

class Country
{
public:
	string name;
	int noAlbum;
	string latest;

	Country();  //This is the constructor
};

Country::Country()
{
	name = "Taylor Swift";
	noAlbum = 5;
	latest = "1986";
}

int main()
{

	Country ts; // instance declaration
	 
	cout << "Country Singer = " << ts.name << endl;
	cout << "No of Album = " << ts.noAlbum << endl;
	cout << "Her latest album = " << ts.latest << endl;
	cout << endl;

	return 0;
}

Line 16 is the actual function of the constructor.  Notice how it is defined with the scope operator ::

The constructor function is called when the ts instance is declared in in line 26.  This is done implicitly without the need to specify a calling function.

constructor