C++ Classes and Objects with Example

In this tutorial, you will learn C++ classes and objects with examples.

C++ Classes

The classes are the most valuable point of C++ which begins with Object-Oriented programming. A collection of objects with the same function and properties. A class is used to set the properties of the objects. It is used as a model for building different objects of the same type. For Example, a class Marks can be used to define the properties and function of a mark. It can be used to create many objects of type marks such as 6, 51, 18, etc. All objects of the Marks class will have the same properties and functions. However, the values of each object can be complex. The values are of the objects are specified after creating an object.

Every object of a class is known as an instance of its class. For Example, 3, 7, and 5 are three instances of a class Marks. Similarly, my-book and your-book can be two instances of a class Book.

Syntax:
The Syntax of declaring a class is,
class Identifier
{
Body of the class
};

  • class: It is a keyword that is used to declare a class.
  • Identifier: It is the name of the class.
    Example:
#include<iostream>
#include<conio.h>
class Softcodeon
{ private:
  int n;
  public:
  void in()
  { cout<<"Enter a value";
    cin>>n;
  }
  void out()
  { cout<"The value of n is="<<n;
  }};
int main()
{ clrscr();
  Softcodeon obj;
  obj.in();
  obj.out();
  getch();
}   

C++ Objects

C++ is an object-oriented language, everything in it is associated with the class and object. The class will compare to the blueprint of something similar to the real-life object and it will define it. However, The objects can be considered as the real real-life entity of blueprint. An object performs a very vital role in the C++ language, it will be used most everywhere while programming. Everything in C++ performs almost the Object so, it is important to understand the object in C++.

So, now let’s create an object of a class that is used in the above program.

int main()
{
Softcodeon obj; //defining an object of type Cellphone
return 0;
}

The class is a user-defined data type and in the above example, it is a Softcodeon. However, As we can see the syntax of specifying an object is simple in manner. It begins with the name of the class for which we are creating an object supported by the name of an object which is of user choice. In this example, we have described the object of class Softcodeon with the name as an obj in the main method. For Instance, We can also define the object anywhere else in the program following the scope.

Conclusion

Finally, we learn C++ Classes and Objects. An object performs a vital role in C++. Everything in C++ turns around Objects. To declare an object, it is important to set the class of it. Each object will have two types of field methods and properties. However, Properties belong to data variables and methods compare to functions playing upon the data members.

Leave a Reply

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