Cpp Classes Objects
# C++ Classes & Objects
C++ adds object-oriented programming to the C language. C++ supports object-oriented program design. Classes are a core feature of C++, often referred to as user-defined types.
Classes are used to specify the form of an object. They are a user-defined data type that encapsulates data and functions. The data in a class is called member variables, and the functions are called member functions. A class can be thought of as a template that can be used to create multiple objects with the same properties and behaviors.
## C++ Class Definition
To define a class, you use the keyword `class`, followed by the name of the class. The body of the class is enclosed in a pair of curly braces and contains the class's member variables and member functions.
Defining a class is essentially defining a blueprint for a data type. It defines what an object of the class contains and what operations can be performed on that object.
!(#)
In the following example, we use the keyword **class** to define a `Box` data type, which contains three member variables: `length`, `breadth`, and `height`:
class Box{public: double length; // Length of the box double breadth; // Breadth of the box double height; // Height of the box};
The keyword **public** determines the access attributes of the class members. Within the scope of a class object, public members are accessible from outside the class. You can also specify class members as **private** or **protected**, which we will discuss later.
## Defining C++ Objects
A class provides the blueprint for objects, so basically, objects are created based on a class. Declaring an object of a class is similar to declaring a variable of a basic type. The following statements declare two objects of the class `Box`:
Box Box1; // Declare Box1 of type Box Box Box2; // Declare Box2 of type Box
Objects `Box1` and `Box2` each have their own data members.
## Accessing Data Members
The public data members of a class object can be accessed using the direct member access operator `.`.
!(
YouTip