C++ Classes and Objects


The classes are the most important feature of C++ that leads to Object Oriented programming.

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.

The variables inside class definition are called as data members and the functions are called member functions.

More about Classes

  1. 1. Class name must start with an uppercase letter. If class name is made of more than one word, then first letter of each word must be in uppercase. Example,
    class Study, class fastlearning.in etc
  2. 2. Classes contain, data members and member functions, and the access of these data members and variable depends on the access specifiers (discussed in next section).
  3. 3. Class's member functions can be defined inside the class definition or outside the class definition.
  4. 4. Class in C++ are similar to structures in C, the only difference being, class defaults to private access control, where as structure defaults to public.
  5. 5. All the features of OOPS, revolve around classes in C++. Inheritance, Encapsulation, Abstraction etc.
  6. 6. Objects of class holds separate copies of data members. We can create as many objects of a class as we need.
  7. 7. Classes do posses more characteristics, like we can create abstract classes, immutable classes, all this we will study later.

Objects

Class is mere a blueprint or a template. No storage is assigned when we define a class. Objects are instances of class, which holds the data variables declared in class and the member functions work on these class objects.

Each object has different data variables. Objects are initialised using special class functions called Constructors. We will study about constructors later.

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. C++ doesn't have Automatic Garbage Collector like in JAVA, in C++ Destructor performs this task.

class Abc
{
 int x;
 void display(){} //empty function
};
int main()
{
 Abc obj;  // Object of class Abc created
}

Share this article on