Classes and Objects in C++ – 1

Written by

Vaaruni Agarwal

C++ is an object-oriented based programming language, the OOPs features are what distinguish the C++ programming language from C. 

A class is used to specify the form of an object and it combines data representation and methods for manipulating that data into one neat package. The data and functions within a class are called members of the class.

Classes in C++:

Classes define nothing else but the blueprint of a data type in C++. A class definition does not define any data type but a blueprint of that data, that is the class definition tells, what members it will contain, what methods it will contain and ultimately what the function will that class perform.

Class Declaration:                 

You can declare any number of classes in C++ as per the needs of the program code. A class can contain a wide variety of data members and in order to manipulate these data members, the member functions are used.               

We can declare a class using a class keyword. The Syntax is :         

class class_name

Here, is an example showing the declaration of a class in C++:

class Box {

   public:

      double length;   // Length of a box

      double breadth;  // Breadth of a box

      double height;   // Height of a box

};

   

Here, a Box class is declared in C++, length, breadth and height are the three data members of double type. These three members are public in nature that is they can be accessed anywhere in the program. 

There are other access modifiers also, besides public, we will learn about them later on.    

 The semicolon after the closing curly brace ensures that the class definition ends here. 

Also, take note that here only the Box class is defined, which tells the compiler that Box is a used defined data type which will hold three double values, it does not reserve any space in memory.

 In order to reserve memory, we need to create an object of this class, we will learn about objects in our next chapter.                  

Classes and Objects in C++ – 1