Skip over navigation

Contents

Classes in C++

Classes

Terms

Problems

Classes are fundamental components of a C++ program. Like structs, introduced earlier, they group related information together. They are essentially new user-defined data types, but they can also contain member functions that operate on their data members. Here is the general syntax of a class specification:


class MyClass		// "class" followed by the name of the class
{			// open brace

	protected:	// protected specification: like "private"
			// except subclasses have direct access.

	private:	// private specification:
			// write data members and/or functions
			// that are visible only inside an object
		int val;	// example of a data member

	public:		// public specification:
			// write data members (rare) and functions
			// accessible directly from the object.
		int get_val();		// example of a member 
					// function declaration
		MyClass(int data);	// declaration of class constructor
		~MyClass();	// declaration of class destructor

};			// close brace AND semicolon

void MyClass::fn(){return val;}	// member function defined outside
MyClass::MyClass(int data){val = data;}	// constructor definition
MyClass::~MyClass(){}	// destructor definition

void main(){
	MyClass NewObject(4);		// creates a new object of 
					// type ClassName
					// named NewObject, with 
					// instance data 4.
	int a = NewObject.get_val();	// calls the get_val() function 
					// of the new object and assigns 
					// the result to a variable
}

I have included above a few examples of functions defined outside of the braces that make up the rest of the class. This is typical for structuring the program if a function is more than just a line or two long. As an additional point, functions defined within the braces of the class definition are made inline by default. Note the use of the :: scope operator, which is used to specify which class a function (or a variable) belongs to when the function is defined outside of the class. In the example above, the int function get_val() is part of class MyClass, as indicated by the line void MyClass::fn(){return val;}

The second function declared inside the class definition curiously has the same name as the class itself. You should define such a function, called the "constructor," for every class you create. Constructors are called automatically when you create a new object. They are typically used to initialize the object's data members. You can overload constructors, just like any other function. The final function shown above is the destructor, which has the same name as the class, but preceded by a tilde (\~). The destructor is called automatically when an object goes out of scope. That is, if an object is declared within a function (or loop, etc.), the destructor is called upon exiting that function. The destructor need not always be defined, but is important if you need to release dynamically allocated memory. If you have pointers to things, you should delete them in the destructor.

As noted in the comments above, items declared under the private heading can only be accessed from within the class (by member functions), whereas items under the public heading are accessible anywhere the object is visible. In general, member functions are declared under the public heading so they can be called from functions outside of the object. Private functions are usually helper functions that are not needed outside of the object. Public data members are uncommon because good C++ style dictates encapsulation of data. That is, data member values are usually private, assigned and retrieved only through public member functions, rather than being directly accessible. In the above example, the variable val is private, and its value in the NewObject object can only be retrieved by calling the get_val() function, as demonstrated in main().

One exception to the visibility of member data is the friend keyword. Friend functions are functions are those granted access to the private data members of classes they don't belong to; that is, they can see all public, private, and protected items in the class. To declare a friend function, define it as you normally would outside of the class. Inside the class write the function declaration with the modifier friend:


class Nation {
	private: long GNP;
	public: friend long GNP_sum(Nation, Nation);
}

long GNP_sum(Nation n1, Nation n2){	// friend function of class Nation
	return n1.GNP + n2. GNP;
}

Static variables declared within a class are shared by every instance of the class. Thus, in the following example, the variable num_items is actually the same variable for all objects of type Item:


class Item {
	private: static int num_items = 0;
	public: Item() 
};

Follow Us