Lesson Home

Properties

 

Accessory Methods

 

The Role of Accessory Methods

The members of a class are divided in two main categories with regards to their roles. The variables provide storage of data for the object; they also define the characteristics of an object. The methods perform related assignments of an object. In fact they receive requests from other objects or functions and provide the appropriate results that the other objects and functions are expecting. For this reason, the other objects and functions from outside the class do not need to have access to the member variables of an object; if they need anything, they would call a method member of the object and let the object know "what they want".

To protect information of an object, you can hide its member variables, after determining which member variables the other objects would not need access to. To hide a member variable, you can put it in a private section. Here is an example:

// This is the main project file for VC++ application project 
// generated using an Application Wizard.

#include "stdafx.h"

#using <mscorlib.dll>

using namespace std;
using namespace System;

public __gc class CTriangle
{
private:
    double base;
    double height;
public:
    CTriangle();
	CTriangle(double b, double h);
    double Area();
};

CTriangle::CTriangle()
	: base(0.00), height(0.00)
{
}

CTriangle::CTriangle(double b, double h)
	: base(b), height(h)
{
}

double CTriangle::Area()
{
    return base * height / 2;
}

CTriangle *GetTriangleDimensions();
void ShowCharacteristics(CTriangle *trio);

int _tmain()
{
    	// TODO: Please replace the sample code below with your own.
    	CTriangle *threeSides = new CTriangle;

	threeSides = GetTriangleDimensions();
    	Console::WriteLine();
	ShowCharacteristics(threeSides);

    	Console::WriteLine();
    	return 0;
}

CTriangle *GetTriangleDimensions()
{
	double b, h;

	Console::WriteLine("Enter the dimensions of the triangle");
	Console::Write("Base:   "); cin >> b;
	Console::Write("Height: "); cin >> h;

	CTriangle *Tri = new CTriangle(b, h);
	return Tri;
}

void ShowCharacteristics(CTriangle *trio)
{
	Console::WriteLine("Triangle Characteristics");
	Console::WriteLine("Area:   {0:F}", __box(trio->Area()));
}

Here is an example of running the program:

Enter the dimensions of the triangle
Base:   -24.44
Height: 20.12

Triangle Characteristics
Area:   -245.87

Press any key to continue

Sometimes, other objects or functions from the outside world will need the information those hidden members have, such as when performing calculations or to complete their own assignments. Since you still want to protect your data, stored in variables, you can create special methods that would be the intermediaries between the member variables and the outside world. There are two categories of member functions to apply this concept: methods used to read the values of member variables and those used to write values.

 

Introduction to Properties

A property is a special member of a class that communicates with the clients of a class. As opposed to the regular member variables made private to protect their values, a property is also a type of member variable but is public. To make sure that the clients of a class don't violate the rules of value protection, a property uses one or two types of method accessories. Unlike other member variables, the compiler creates this member variable for you if you respect some rules to create a property.

To create a property, you start with the __property keyword.

 

Method Readers

One of the categories of methods of a class allows the other objects and functions from outside to access or retrieve the value of a particular member variable of the class. As the other classes or functions are referred to as clients of the object, this object makes necessary values available. This type of method is referred to as a read method of a method reader.

To create a method reader, by convention, you start its name with get. For example, a method reader that transmits the value of the base of the above CTriangle class can be called getBase(). By convention, a get method doesn't take any argument but returns a value for the particular member variable it relates to. Since get methods serve as access methods to the outside world, they are referred to as interface methods. Here is an example of a method reader:

public __gc class CTriangle
{
private:
    double base;
    double height;
public:
    CTriangle();
	CTriangle(double b, double h);
    double Area();

public:
    double getBase();
};

To implement a get method, you usually will simply return the value of the corresponding member variable. You can implement the method inline in the body of the class. Otherwise, you can implement it outside. Here is an example:

double CTriangle::getBase()
{
    return base;
}

This is how this type of method is created and used in C++. Managed C++ solves this issue otherwise.  To create a property, you can type the __property keyword to the left of the method reader when declaring it. Also, the name of the method must start with get_ instead of get. Therefore, the above method can be changed as follows:

public __gc class CTriangle
{
private:
    double base;
    double height;
public:
    CTriangle();
	CTriangle(double b, double h);
    double Area();

public:
    __property double get_Base();
};

The implementation of the method is the same as above. If you define it in the body of the class, you can let it be preceded by the __property keyword:

public __gc class CTriangle
{
private:
    double base;
    double height;
public:
    CTriangle();
    CTriangle(double b, double h);
    double Area();

public:
    __property double get_Base() { return base; }
};

If you implement it outside of the class, remove the __property keyword and define the function as you would any other:

double CTriangle::get_Base()
{
    return base;
}

After creating a property by defining its method reader, the compiler internally creates but physically hides a member variable using the name on the right side of get_. This means that a member variable is created but doesn't display in the body of the class. In this case, the new member variable is called Base. The clients of the class can access it to retrieve the value of the corresponding member variable.

Here is a program that uses a class with two properties. Notice that a Base and a Height non-declared member variables are accessed outside of the class:

// This is the main project file for VC++ application project 
// generated using an Application Wizard.

#include "stdafx.h"

#using <mscorlib.dll>

using namespace std;
using namespace System;

public __gc class CTriangle
{
private:
    double base;
    double height;
public:
    CTriangle();
	CTriangle(double b, double h);
    double Area();

public:
    __property double get_Base();
    __property double get_Height();
};

CTriangle::CTriangle()
	: base(0.00), height(0.00)
{
}

CTriangle::CTriangle(double b, double h)
	: base(b), height(h)
{
}

double CTriangle::Area()
{
    	return base * height / 2;
}

double CTriangle::get_Base()
{
    	return base;
}

double CTriangle::get_Height()
{
	return height;
}

CTriangle *GetTriangleDimensions();
void ShowCharacteristics(CTriangle *trio);

int _tmain()
{
    	// TODO: Please replace the sample code below with your own.
    	CTriangle *threeSides = new CTriangle;

	threeSides = GetTriangleDimensions();
    	Console::WriteLine();
	ShowCharacteristics(threeSides);

    	Console::WriteLine();
    	return 0;
}

CTriangle *GetTriangleDimensions()
{
	double b, h;

	Console::WriteLine("Enter the dimensions of the triangle");
	Console::Write("Base:   "); cin >> b;
	Console::Write("Height: "); cin >> h;

	CTriangle *Tri = new CTriangle(b, h);
	return Tri;
}

void ShowCharacteristics(CTriangle *trio)
{
	Console::WriteLine("Triangle Characteristics");
	Console::WriteLine("Base:   {0:F}", __box(trio->Base));
	Console::WriteLine("Height: {0:F}", __box(trio->Height));
	Console::WriteLine("Area:   {0:F}", __box(trio->Area()));
}

Here is an example of running the program:

Enter the dimensions of the triangle
Base:   -84.12
Height: 65.46

Triangle Characteristics
Base:   -84.12
Height: 65.46
Area:   -2753.25

Press any key to continue

 

Method Writers

The method readers we used above allow clients of the class to retrieve the value of a member variable of the class. This was done by creating a property using a get_ method. After creating it, the property can only be read from. The clients of the class cannot modify its value. This is fine for some classes. If you want to be able to change the value of the corresponding member variable, you can create another or additional type of property.

Another category of methods of a class allows the other objects and functions from outside to provide values to a particular member variable of a class. This is done so that, if or since you would not give direct access of a certain member variable of a class to the outside world, you can use such an accessory method to receive a value from outside and then "transmit" it to the member variable whose values needs to be changed. Such a member function is referred to as write method because it writes the value of its corresponding member variable.

By convention, a method writer has a name that starts with set. For example, a method that provides a value of the base of the above CTriangle class can be called setBase(). Also, by convention, a set method takes one argument that is a copy of the corresponding member variable. By convention, this method does not return a value; it is mainly used to assign the right value to the appropriate member. A method writer can be declared as follows:

public __gc class CTriangle
{
private:
    double base;
    double height;
public:
    CTriangle();
    CTriangle(double b, double h);
    double Area();

public:
    void setBase(double b);
};

In Managed C++, to create a property that can modify the value of a member variable, the name of a method writer must start with set_. The word on the right side of set_ is the actual name of the new property. Here is an example:

public __gc class CTriangle
{
private:
    double base;
    double height;
public:
    double Area();
    void ShowCharacteristics();

public:
    __property void set_Base(double b);
};

After this declaration, the compiler internally creates a member variable called Base but it is not displayed in the body of the class. To implement a method writer, you can simply assign its argument to the member variable it is referring to:

public __gc class CTriangle
{
private:
    double base;
    double height;
public:
    CTriangle();
    CTriangle(double b, double h);
    double Area();

public:
    __propert void set_Base(double b);
};

void CTriangle::set_Base(double b)
{
    base = b;
}

The job of a set_ method is to let outside functions or other objects assign values to, or change values of, the corresponding member variable of its class. Here are examples:

// This is the main project file for VC++ application project 
// generated using an Application Wizard.

#include "stdafx.h"

#using <mscorlib.dll>

using namespace std;
using namespace System;

public __gc class CTriangle
{
private:
    double base;
    double height;
public:
    CTriangle();
	CTriangle(double b, double h);
    double Area();

public:
    __property double get_Base();
	__property double get_Height();
	__property void   set_Base(double b);
	__property void   set_Height(double h);
};

CTriangle::CTriangle()
	: base(0.00), height(0.00)
{
}

CTriangle::CTriangle(double b, double h)
	: base(b), height(h)
{
}

double CTriangle::Area()
{
    return base * height / 2;
}

double CTriangle::get_Base()
{
    return base;
}

double CTriangle::get_Height()
{
	return height;
}

void CTriangle::set_Base(double b)
{
    base = b;
}

void CTriangle::set_Height(double h)
{
    height = h;
}

void GetTriangleDimensions(CTriangle *t);
void ShowCharacteristics(CTriangle *trio);

int _tmain()
{
    	// TODO: Please replace the sample code below with your own.
    	CTriangle *threeSides = new CTriangle;

	GetTriangleDimensions(threeSides);
    	Console::WriteLine();
	ShowCharacteristics(threeSides);

    	Console::WriteLine();
    	return 0;
}

void GetTriangleDimensions(CTriangle* tri)
{
	double b, h;

	Console::WriteLine("Enter the dimensions of the triangle");
	Console::Write("Base:   "); cin >> b;
	Console::Write("Height: "); cin >> h;

	tri->Base   = b;
	tri->Height = h;
}

void ShowCharacteristics(CTriangle *trio)
{
	Console::WriteLine("Triangle Characteristics");
	Console::WriteLine("Base:   {0:F}", __box(trio->Base));
	Console::WriteLine("Height: {0:F}", __box(trio->Height));
	Console::WriteLine("Area:   {0:F}", __box(trio->Area()));
}

Here is an example of running the program:

Enter the dimensions of the triangle
Base:   -38.24
Height: 24.68

Triangle Characteristics
Base:   -38.24
Height: 24.68
Area:   -471.88

Press any key to continue

Notice in the above program that the values passed to the properties are hardly validated. For example, one property is given a negative value. Since a set_ method allows an object to control what value is assigned to its corresponding member variable, you can write a control statement to validate such a value. This also allows you to validate the value assigned to a member variable so the outside world doesn't just assign and impose any value. Therefore, a set_ method can check, then validate, reset, or reject a value that comes from the outside. As stated already, this can be taken care of with a conditional statement. Here are two examples:

// This is the main project file for VC++ application project 
// generated using an Application Wizard.

#include "stdafx.h"

#using <mscorlib.dll>

using namespace std;
using namespace System;

public __gc class CTriangle
{
private:
    double base;
    double height;
public:
    CTriangle();
    CTriangle(double b, double h);
    double Area();

public:
    __property double get_Base();
    __property double get_Height();
    __property void   set_Base(double b);
    __property void   set_Height(double h);
};

CTriangle::CTriangle()
    : base(0.00), height(0.00)
{
}

CTriangle::CTriangle(double b, double h)
    : base(b), height(h)
{
}

double CTriangle::Area()
{
    return base * height / 2;
}

double CTriangle::get_Base()
{
    return base;
}

double CTriangle::get_Height()
{
    return height;
}

void CTriangle::set_Base(double b)
{
    	// Don't allow a negative value
	// If the value that is being passed to the base is negative...
	if( b < 0.00 )
	    base = 0.00; // then reject it and reset the base to 0
	else 	        // Otherwise, assign the new value to the base
	    base = b;
}

void CTriangle::set_Height(double h)
{
    	// Don't allow a negative value
	// If the value that is being passed to the height is negative...
	if( h < 0.00 )
	    height = 0.00; // then reject it and reset the height to 0
	else 		 // Otherwise, assign the new value to the height
	    height = h;
}

void GetTriangleDimensions(CTriangle *);
void ShowCharacteristics(CTriangle *trio);

int _tmain()
{
    	// TODO: Please replace the sample code below with your own.
    	CTriangle *threeSides = new CTriangle;

    	// First Triangle
	threeSides->Base   = -35.55;
	threeSides->Height = -30.72;
	ShowCharacteristics(threeSides);
	Console::WriteLine();
	
	// Second Triangle
	threeSides->Base   = -35.55;
	threeSides->Height =  30.72;
	ShowCharacteristics(threeSides);
	Console::WriteLine();

	// Third Triangle
	threeSides->Base   =  35.55;
	threeSides->Height = -30.72;
	ShowCharacteristics(threeSides);
	Console::WriteLine();
	
	// Fourth Triangle
	threeSides->Base   = 35.55;
	threeSides->Height = 30.72;
	ShowCharacteristics(threeSides);

    	Console::WriteLine();
    	return 0;
}

void GetTriangleDimensions(CTriangle* tri)
{
	double b, h;

	Console::WriteLine("Enter the dimensions of the triangle");
	Console::Write("Base:   "); cin >> b;
	Console::Write("Height: "); cin >> h;

	tri->Base   = b;
	tri->Height = h;
}

void ShowCharacteristics(CTriangle *trio)
{
	Console::WriteLine("Triangle Characteristics");
	Console::WriteLine("Base:   {0:F}", __box(trio->Base));
	Console::WriteLine("Height: {0:F}", __box(trio->Height));
	Console::WriteLine("Area:   {0:F}", __box(trio->Area()));
}

This would produce;

Triangle Characteristics
Base:   0.00
Height: 0.00
Area:   0.00

Triangle Characteristics
Base:   0.00
Height: 30.72
Area:   0.00

Triangle Characteristics
Base:   35.55
Height: 0.00
Area:   0.00

Triangle Characteristics
Base:   35.55
Height: 30.72
Area:   546.05

Press any key to continue

 

 

Previous Copyright © 2004-2010 FunctionX, Inc. Next