0
Posted November 25, 2012 by Victor in C/C++ Programming
 
 

How C++ Classes Function

classes_0
classes_0

Many programmers often fear learning about classes due to their perception that classes are complex and cannot be understood. This is a false perception and classes are some of the easiest aspect of programming to grasp. By definition, classes refer to a group of data that are related to an object type. Classes include all the information about an object, the functions for accessing the data and the variables.

For you to better understand class design in C++, consider a class design as an entire vehicle while the C++ functions as the doors to the vehicle and C++ variables as your seats and other objects within the vehicle. The interesting thing about classes is that they have the capability to inherit from other classes.

Class Design Syntax

The class syntax begins with the ‘class’ keyword. The class often has three levels of restriction on its variables. These restriction levels are:

  •   Public restriction: This allows all the variables in a C++ program including the variables outside a class to have access to all functions that have been specified as public.
  •  Protected restriction: This prevents the functions from outside the class from accessing the variables within the class.
  •   Private restriction: This functions in the same manner as protected restriction and prevents the functions from outside the class from accessing the variables within the class. However, there are some privileges in the private restriction that are not similar to protected restriction especially when dealing with inheritance.

Why do you need the public, protected or private restrictions? This is because there are some functions that offer access to the outside use of a program (public functions) while there are others that are restricted to the functioning of the program (private & protected functions).For instance, the vehicles engine is often protected and private because it operates the vehicle. The vehicles seats, gas pedals, mirrors are public because they are used on a daily basis. Declaring these restrictions is easy and simply needs you to have the restriction name followed by a semi colon e.g.:


  public:

  protected:

  private:

Here is the example program that we shall use for this tutorial:


#include <iostream>
using namespace std;

class victor // Defining the class called Victor
{
  public:
  //This indicates that all functions and variables here below can be accessed outside the class. Use a colon after class restriction.
  // are accessible to the rest of the program.
 
    myage ();
    yearofbirth();

  protected:
  // This indicates that all functions and variables here below and until another restriction is specified in the class will only be accessible to 
  // functions within the class.

    Pin ();

};

int victor::myage ()
{

	int z;

	z = 20;

	cout<<"\n\nMy age is\n" <<z;
}

victor::yearofbirth()  //the two colons are a simple message to the compiler that the named function belongs to the class Victor.
{

	int b;

	b = 1992;

	cout<<"\n\nMy year of birth is\n" << b;
}

int main()
{
//To call a function in a class put the class name, a period and then the function name.

	victor details;// creates an object of class victor named details that we will use to call our class functions
	details.yearofbirth();
	details.myage();
	return (0);
}

Declaring variables and Functions within a C++ Class

Once you have declared the type of restriction you want, you can now use the variables and functions that are desired as part of your class. In C++, classes must always have two types of functions named:

  •  Constructor Functions: This initializes variables and allocates memory for your variables.
  •  Destructor Functions: These are used to clean up after the class by freeing any memory that had been allocated.

If there is no initialization of constructor and destructor functions needed within your class, the compiler can create a default constructor and destructor functions. To initialize a constructor, type your class name while if you have the symbol ~ before the class name, then, you have initialized a destructor. As soon as a class instance is declared, a constructor is called automatically. When the program ends and you no longer need your class, you call a destructor to clear off the memory that had been allocated to the class. Constructors should always be public to enable them to be called and therefore enable the creation of classes.

To call your class function and variables in the main function, here is an example:

int main()
{
//To call a function in a class put the class name, a period and then the function name.

	victor details;// creates an object of class victor named details that we will use to call our class functions
	details.yearofbirth();
	details.myage();
	return (0);
}

If you want to define a function outside its given class definition, you will have to start off by the return type, then type the class name, place the two colons, and finally the function name. This will be an indicator to the compiler that a given function belongs to the stated class.

#include <iostream>
using namespace std;

class victor // Defining the class called Victor
{
  public:
    //This indicates that all functions and variables here below can be accessed outside the class. Use a colon after class restriction.
    // are accessible to the rest of the program.

    myage ();
    yearofbirth();

  protected:
    // This indicates that all functions and variables here below and until another restriction is specified in the class will only be 
    // accessible to functions within the class.

    Pin ();

};

int victor::myage ()
{

  int z;
  z = 20;
  cout<< "\n\nMy age is\n" << z;
}

victor::yearofbirth()  //the two colons are a simple message to the compiler that the named function belongs to the class Victor.
{

  int b;
  b = 1992;
  cout << "\n\nMy year of birth is\n" << b;
}

int main()
{
  //To call a function in a class put the class name, a period and then the function name.

  victor details;// creates an object of class victor named details that we will use to call our class functions
  details.yearofbirth();
  details.myage();
  return (0);
}

Victor