Classes & Objects in C++

In this tutorial chapter, we will study and understand the concept of Classes & Objects in C++.
One of the most important feature and advantage of C++ programming language over C programming language is that C++ supports Object Oriented Programming paradigm.

 

classes and objects in c++

Classes & Objects in C++

Classes are the most important feature of C++ that leads to Object Oriented programming.
Classes in C++ are user defined data types, which hold multiple variables(data members) of different types and functions(member functions) that access & perform operations on those variables. When we define any class, we are not defining any data, we just define a structure or a blueprint, as to what the object of that class type will contain and what operations can be performed on that object.

Objects in simple terms are variables of type class, so classes can be considered as data types(user defined) of these variables(objects). 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 initialized 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.

C++ Class Definitions

A class definition starts with the keyword class followed by the class name; and the class body, enclosed by a pair of curly braces. A class definition must be followed either by a semicolon or a list of declarations.

The keyword public / private / protected determines the access attributes of the members of the class that follow it. A private member cannot be directly accessed by the object and in order to access the data members, you have you have to define member functions which generally have public access specifier. You can also the data members and member functions as public, private or protected.

Define C++ Objects

A class provides the blueprints for objects, so basically an object is created from a class. We declare objects of a class with exactly the same sort of declaration that we declare variables of basic types. Following statements declare two objects of class Box:

Sample Program of Classes & Objects in C++

Run Online

Output
Length of Box: 10
Breadth of Box: 6
Height of Box: 5
Explanation of the program

We first defines the Box class using the class keyword. Inside the class we created 3 data members. the length and breadth are private while the height is public. We then defined 2 member functions : setData to set the values of the data members and getLength, getBreadth to retrieve those values. Since height is public member, we didn’t have to create a member function to retrieve its value and we can use a (.) operator directly to set or get its value. In the main() function we first created an object(b1)of Box type. We then assigned values to its data members using the setData function. Lastly we accessed the values of length and breadth using getLength and getBreadth respectively and for accessing the value of height we simply used the (.)operator.

Keywords: private and public

You may have noticed two keywords: private and public in the above example.
The private keyword makes data and functions private. Private data and functions can be accessed only from inside the same class.
The public keyword makes data and functions public. Public data and functions can be accessed out of the class.
Here, length and breadth are private members & height is public data members where as all functions are public.
If you try to access private data from outside of the class, compiler throws error. This feature in OOP is known as data hiding.

More about Classes
  • 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 Person, class SimpleSnippets etc
  • Classes contain, data members and member functions, and the access of these data members and variable depends on the access specifiers.
  • Class’s member functions can be defined inside the class definition or outside the class definition.
  • 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.
  • All the features of OOPS, revolve around classes in C++. Inheritance, Encapsulation, Abstraction etc.
  • Objects of class holds separate copies of data members. We can create as many objects of a class as we need.
  • Classes do posses more characteristics, like we can create abstract classes, immutable classes, all this we will study later.
Watch it on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *