×
>
<

Aptitude

C++ Classes and Objects

To begin with, C++ is an object oriented programming language. Thus, as the name suggests, the entire functionality of C++ revolves around the concept of objects and classes. Classes are one the pivotal features of the C++ language, and these are often user-defined.

A class can be defined, in simple terms as a collection of objects having a similar set of characteristics. A class holds its objects, which are the building blocks of the class, and also the subsequent functions within it. The data and functions within a class are called members of the class.

A class essentially defines the outline for it’s objects. Thus, basically an object is created from a class. Objects of a class are declared with exactly the same sort of declaration that is used to declare variables of basic types.

What is a Class ?

When we define a class, we are simply defining a structure for a data type. This doesn’t actually define any data, but it does define the meaning of the class name, that is, what the object of the class will contain and what operations can be performed on such an object.

A class definition is initiated with the keyword class which precedes the class name; and the class body, encased in a pair of curly braces. A class definition must be followed either by a semicolon or a list of declarations. For example, the Car data type has been using the keyword class in the below code :

  
class class Cars
{
public:
  // to store whether a car is SUV, Sedan or hatchback
  String carType;	
  
  // to store the engine capacity
  int engineCapacity;
  
  // to store the model name of the Car
  String modelName;		
};
  

Some pointers to remember:

  • A Class is a user defined data-type which consists of data members and member functions.
  • Data members are the variables and member functions are the functions used to manipulate these variables and together these data members and member functions defines the properties and behavior of the objects in a Class.
  • In the above example of class Car, the data member will be carType, engineCapacity and modelName .

Now, Let us learn How do Objects work ?
  
Cars Cars1; // Declare Cars1 of type 
Cars Cars Cars2; // Declare Cars2 of type Cars
  
Accessing the Data Members:

The public data members of objects of a class can be accessed using the direct member access operator (.) . Let us look at the following example for a better understanding.

  
#include 
using namespace std;

class Car
{
public:
  double length;		// Length of a Car
  double breadth;		// Breadth of a Car
};

int
main ()
{
  Car Car1;			// Declare Car1 of type Car
  Car Car2;			// Declare Car2 of type Car
  
  float Area = 0.0;		// Store the area of a car

// Car 1 specs
  Car1.breadth = 3.0;
  Car1.length = 7.0;

// Car 2 specs
  Car2.breadth = 10.0;
  Car2.length = 15.0;

// Area of Car 1
  Area = Car1.length * Car1.breadth;
  cout << "Area of Car1 : " << Area << endl;

// Area of Car 2
  Area = Car2.length * Car2.breadth;
  cout << "Area of Car2 : " << Area << endl;
  return 0;
}
  
output
  
Area of Car1 : 21
Area of Car2 : 150