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#: Automatic Properties

This entry is part 29 of 71 in the series C#

A variable or a method inside a class could be called properties of a class or object in other object-oriented programming languages such as C++.

A property in C++ has a slightly different meaning.  It provides a simple way to access a field, called the backing field.

The backing field can be publicly available while still allowing the internal details of that field to be hidden.

Although fields declare variables, which require storage in memory, properties do not.

Instead, properties are declared with accessors that enable you to control whether a value can be read or written and what should occur when doing so.

The get accessor enables the property value to be read, whereas the set accessor enables the value to be written.

using System;

namespace Automatic
{
    class Employee
    {
        public string Name
        {
            get;
            set;
        }
    }

    class Program
    {
        static void Main()
        {
            Employee e1 = new Employee();
            e1.Name = "John";  //This will set name to John
            Console.WriteLine(e1.Name);  //This will get the name
            Console.ReadLine();
        }
    }
}

Now we look at the code here:

class Employee
{
   public string Name
     {
        get;
        set;
     }
}

It shows the simplest way to declare a property.

When using this syntax, known as automatic properties, you omit the backing field declaration and must always include both the get and set accessor without a declared implementaion, which the compiler provides.

In fact the compiler transforms the code into code that looks like below shown in the previous example.

class Employee
{
   private string name;

   public string Name
   {
      get
      {
         return this.name;
      }
      set
      {
         this.name = value;
      }
   }
}

You can refer to a later post about array where I have used the length property of an array to easily retrieve the array length.

 

PHP & OOP: Properties of a class

This entry is part 6 of 11 in the series PHP & OOP

Class member variables are called ‘properties‘.

When I first started learning OOP using C++, I thought this is odd.  It sounds abstract.

But look at the following declarations for the 2 class members and you might be able to understand.

<?php

class MovieStar
{
    public $person = 'Tom Cruise';
    public $salary = 10000;
    public $movie = 'Mission Impossible';
    public $no_of_movies = 20;
}

class PopStar
{
    public $person = 'Madonna';
    public $salary = 8000;
    public $album = 'Crazy For You';
    public $no_of_album = 12;
}

?>

From the class properties such as $movie and $album, we could guess what the 2 classes are about.