C++ Classes

In this C++ programming tutorial we take a look at classes. A class is a mechanism for creating user-defined data types. It is similar to the C language structure data type.

A class can not only hold data variables, but can also hold functions. These variables and functions are members of a class. The variables are called data members and functions are called member functions.

In C++, a class type can be declared with the keywords union, struct, or class. A union object can hold any one of a set of named members. Structure and class objects hold a complete set of members. Each class type represents a unique set of class members including data members, member functions, and other type names. The default access for members depends on the class key:

  • The members of a class declared with the keyword class are private by default. (A class is inherited privately by default.)
  • The members of a class declared with the keyword struct are public by default. (A structure is inherited publicly by default.)
  • The members of a union (declared with the keyword union) are public by default. (A union cannot be used as a base class in derivation.)

The keyword class is generally used when declaring a class type.

The general form of a class looks like this:


	class class_name
	{
		access_specifier:
			member1;
		access_specifier:
			member2;
	};

As you can see it looks very similar to the declaration of structures. (Difference with a structure is that we now can include functions and members). Class_name is the identifier for the class.

The access_specifier can be one of the following keywords: private, public or protected. With these specifiers the access right of the members that will follow are set.

  • private members of a class are only accessible by other private members of the same class.
  • protected members are accessible by members of the same class.
  • protected members are also accessible by members of derived classes.
  • public members are accessible from anywhere where the object is used.

Note: all members of a class (declared with the class keyword) have private access by default, unless another specifier overrules it.

Take a look at the following example:


	class CAdd
	{
			int x , y;
		public:
			int add(int,int);
	};

Note: C before Add is used to indicate that it is a class.

In the example above the integers x and y are private members. The function add() is public. To be clear it is not necessary to use the private specifier, but to be clear you better use it. Then the example will become:


	class CAdd
	{
		private:
			int x , y;
		public:
			int add(int,int);
	};

Member functions can access the private/protected data members of their class and manipulate them. No external functions can access the private/protected data members of a class.

Let’s look at a complete example:


	#include <iostream>
	using namespace std;

	class CAdd
	{
		private:
			int x, y;
		public:
			void add(int,int);
			int ret() { return x+y; }
	};

	void CAdd::add(int a, int b)
	{
		x = a;
		y = b;
	}

	int main()
	{
		CAdd my_object;

		my_object.add(4,4);
		cout << my_object.ret() << endl;

		return 0;
	}

So let’s look at the program we created. In the class CAdd we declared two private members of the type integer with the name x and y. We also declared two functions (both are public members): add() and ret().

In the function main() we declared the object my_object. To access the functions, which are defined in the class, we can use the object followed by a dot (.) and then the name of the member (function). For instance: my_object.add(4,4); (the same as we did with structures).

You may notice the difference between the two functions add() and ret(). The body of the function ret() is filled. For the function add() there is only a prototype defined. As you can see we defined the member function add() outside the class declaration:


	void CAdd::add(int a, int b)
	{
		x = a;
		y = b;
	}

In the code above we say that function add() is member of the class CAdd by putting the operator of scope (two colons (::)) between them. By using the scope operator we getting access to other members in the class. So we can use the integers x and y. (This are private members thus only accessible by members of the class.)

The only difference between defining a class member function completely within its class and to include only the prototype (later on its definition), is that in the first case the function will automatically be considered an in-line member function by the compiler, while in the second it will be a normal (not in-line) class member function, which in fact supposes no difference in behavior.

A great advantage of a class is that we can declare several objects of it. Note: when using different objects each object will get its own variables x and y. So each object will get its own variables to operate with.

That is all for this tutorial. In the next tutorial we will talk some more about classes.

This entry was posted in C++ Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! Tweet This! or use to share this post with others.

Comments are closed.