Classes and objects in C++ with Examples

0

In this article we will review the classes and objects in C++ with real world and theoretical examples.

Classes in C++:-

In object oriented programming, classes are basically the user defined data type which consists of data member and member functions. The data members are also called, the fields of a class, these fields are the attributes of this custom defined data type. The member functions are used to define the behavior of a class. The functionality of a class is defined in these member functions.

The C++ classes are also known as the collection of objects with the same attributes and functionality.

In OOP classes are used to define the characteristics and functionality of the different objects of the same type. They are used as a model to create different objects, For example, a class Vehicle can be used to define the attributes and functions of different types of vehicles. The engineers could make as many different types of vehicles from the blueprint of a vehicle class.

For example car, truck and crane are the different objects of a class vehicle. Every vehicle will have its own set of attributes and functions allocated in a memory.

classes and objects in C++

For example:-

Every vehicle has its own color, wheels, registration number and Model number. In class we define them as fields, there values could be different for different objects.

Each of these has their own functions like move forward, backward and stop.

So we could make many objects of class vehicle.

Objects in C++:-

The object of a class is also known as its instance and the process of creating an object is called instantiation of a class. For example car, truck and crane are the three objects of a class Vehicle, values could be assigned to objects after their declaration.

Declaring a Class:-

In C++ classes are declared by a keyword class which is written before its name. We could make as many objects as we want and each objects have its own space of fields and functions in a memory.

Data Members: The variables declared in a class.

Member Functions: The functions declared in a class.

Syntax of class declaration in C++:-

Syntax is as follows:

class Identity

{

Class body

};

class: It is a keyword which is used to declare the classes in C++.

Identity: This is the name of our declared class. The rules for declaring a class name is same as declaring a simple variable.

The declaration of a class always ends with the semicolon. All data members and member functions are declared within the braces which are called the body a class.

For Example:-

Class Vehicle
{
int number;
string color;
};

The above declared class has only two data members declared in it. A class may have both fields and functions.

Access Specifiers in C++:-

Access Specifiers are the keywords which are used to specify the access level of the data members and functions. There are three types of access specifiers used in C++ language, which are as follows:

1. Private Access Specifier:-

The private access specifier is the default specifier for in C++. This specifier restricts the use of class variables to the scope of a class, so that they could be only accessed within the class by the member functions. Therefore, the private access specifier is used to implement the method of encapsulation (information hiding), so, private data could not be accessed from outside the class. In normal routine the data members are declared as private to protect the important data contains inside the fields of a class.

2. Public access specifier in C++:-

Public access specifier allows the programmer to access a class fields from inside as well as outside the class. Any data member or function of a class declared with public access specifier can be accessed from anywhere in the program.

/* */

In normal routine class member functions are declared with public access specifier so user could access them from outside and could perform the functionality on the objects. The class cannot be used directly if both data members and member functions are declared as Private. Therefore in that case we could use friend functions and friend classes to access data of a class.

3. Protected access specifier in C++:-

Protected access specifier and function overriding are used in case of inheritance, when the user has to access a data of different classes in the inheritance hierarchy.

Creating Objects in C++:-

As we have described and properly explained above with examples that the class is a blueprint or prototype for creating objects of same type. It is a user defined data-type which contains both data members and functions. The syntax of creating objects is same as creating simple variables. When we create an object of a class then the space for all data members and member functions defined in the class is allocated in the memory according to their data types, except static data members and static member functions because they are the part of a class therefore the space is allocated to them when we declare a class.

Syntax of creating an object in C++:-

Class_name object_name;

Class_name: It is the name of a class whose object we are creating.

Object_name: It is the name of an object we are creating. The rules for object name are same as the rule for declaring the simple variable.

Example:-

Vehicle v1;

The above example will declare an object v1 of class vehicle. This object contains all data members and functions that are defined in a class.

Executing Member Functions in C++:-

The object of a class has data members and member functions, these data members hold the attributes of an object and functions are used to change the attributes of data members. The member functions can be executed only after creating the object except static member functions. The dot operator is used to access fields and functions of an object.

Syntax

The syntax of executing member functions is as follows:

Object_name.function();

Object_name: It is the name of the object whose member function is to be executed.

Function: It is the name of a member function to be executed. Any required parameters are also passed to the member functions in parenthesis.

The object name and member function are separated by dot operator.

Example

Vehicle v1;

v1.forward();

Above is an example is used to access the forward functions of an object v1.
The below example will completely illustrate the declaration of objects and classes, it also illustrates how to access data members and member functions.

#include<iostream.h>
#include<conio.h>
#include<string.h>
class Vehicle
{
int model;
public:
int registration;
Vehicle()
{
registration = 111;
}
void forward()
{
cout<<" Vehicle number: "<<registration<<" is moving forward : \n";
}
void reverse()
{
cout<<" Vehicle is moving backwards :";
}
};
main()
{
Vehicle v1;
v1.forward();
v1.registration= 333;
v1.forward();
getch();
};

Explanation:-

classes and objects in C++ exampleThe above program will create a class vehicle, in this class it will declare two data member model and registration. The data member model is declared with private access specifier and data member registration is declared with public access specifier. When the above program started to execute first output is displayed by calling the forward member function.

Then public data member is directly accessed from the main, Therefore the value of registration is changed to 333. Then again member function forward is called and 2nd output is displayed.

[yop_poll id=”15″] If you like my article on classes and objects in C++ with examples and wanted to read my other articles then visit my Homepage.

@ 2014 HellGeeks.com

Classes and objects in C++ with Examples
5 (100%) 1 vote

(royal52) is a software engineer. He is good at authoritative link building, Content Writing, SEO, Web Development, Wordpress, HTML, CSS, JAVASCRIPT, Game Development, Unity3d, Graphic Designing, Logo Designing, Android Development and Ios Development. Last but not least, he is passionate about development and gaming.

We will be happy to hear your thoughts

Leave a Reply

Grab our Partner's Top Rated web hosting deal - Upto 90% off