Introduction

Class is a user defined data type, which holds its own data members and member functions, which can be accessed and used by creating instance of that class. It is the building block of Object-Oriented programming. Variables defined inside class definition are called as data members and functions are called member functions.

Defining Class

A class is defined using keyword class followed by the name of class. Body of class is defined inside the curly brackets and terminated by a semicolon at the end.

class class_name {
  access_specifier_1:
    data_member;
  access_specifier_2:
    member_function();
};

Where class_name is a valid identifier for the class. The body of the declaration can contain members, which can either be data or function declarations, and optionally access specifiers.

Objects of Classes

No storage is assigned when a class is defined. Objects are instances of class, which holds the data variables declared in class and the member functions work on these class objects. Objects are initialised using special class functions called Constructors. And whenever the object is out of its scope, another special class member function called Destructor is called, to release the memory reserved by the object.

class Rectangle {
  int width, height;
public:

  void setwidth(int);
  void setHeight(int);

  int area(void);
};

int main()
{
  // Object of class Rectangle
  Rectangle  rect; 

  return 0;
}

 

Access Specifier

An access specifier is one of the following three keywords:

  • private : private members of a class are accessible only from within other members of the same class.
  • public : public members are accessible from anywhere where the object is visible.
  • protected : protected members are accessible from other members of the same class, but also from members of their derived classes.

Class in C++ are similar to structures, the only difference being, class defaults to private access control, where as structure defaults to public.

class Rectangle {
  int width, height;
public:

  void setwidth(int w) {
    width = w;
  };

  void setHeight(int h) {
    height = h;
  }

  int area(void) {
    return width * height;
  }
};

int main()
{
  Rectangle  rect;

  // Private data member, invalid access
  rect.width = 10;
  // Public member function, valid access
  rect.area();
}

Above example declares a class Rectangle with two data members and three member functions.  The only members of rect that cannot be accessed from outside the class are width and height, since they have private access.

Member Functions

There are two ways to define a member function:

  • Inside class definition : Member functions defined inside the class definition.
  • Outside class definition : Use the scope resolution :: operator along with class name and function name.
class Rectangle {
  int width, height;
public:

  void setwidth(int);
  void setHeight(int);

  // Inside class definition
  int area(void) {
    return width * height;
  }
};

// Outside class definition
void Rectangle::setwidth(int w) {
  width = w;
};

// Outside class definition
void Rectangle::setHeight(int h) {
  height = h;
};

int main()
{
  Rectangle  rect;

  rect.setHeight(10);
  rect.setwidth(12);
  rect.area();

  return 0;
}

setWidth() and setHeight() are declared with its prototype within the class, but its definition is outside it. In this outside definition, the operator of scope (::) is used to specify that the function being defined is a member of the class Rectangle.

Member functions defined inside the class definition are an inline member function. Member functions defined outside the class definition are normal (not-inline) member function.

Reference

Classes (I)