CPP Tutorials: Classes in C++ and OOP Concept

CPP Tutorials: Classes in C++ and OOP Concept

The main purpose of C++ programming is to add object orientation to the C programming language and classes are the central feature of C++ that supports object-oriented programming and are often called user-defined types.

A class is used to specify the form of an object and it combines data representation and methods for manipulating that data into one neat package. The data and functions within a class are called members of the class.

Class Syntax:

Classes in C++ has a simple syntax as below:

"Where class_name is a valid identifier for the class, object_names is an optional list of names for objects of this class. The body of the declaration can contain members, which can either be data or function declarations, and optionally access specifiers."

How to Define a C++ Class:

When you define a class, you define a blueprint for a data type. This doesn’t actually define any data, but it does define what the class name means, that is, what an object of the class will consist of and what operations can be performed on such an object.

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. For example, we defined the Box data type using the keyword class as follows:

"The keyword public determines the access attributes of the members of the class that follow it. A public member can be accessed from outside the class anywhere within the scope of the class object. You can also specify the members of a class as private or protected."

  • private members of a class are accessible only from within other members of the same class (or from their “friends”).
  • protected members are accessible from other members of the same class (or from their “friends”), but also from members of their derived classes.
  • Finally, public members are accessible from anywhere where the object is visible.

How to 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:

or

Both of the objects Box1 and Box2 will have their own copy of data members.

How to Access Class Members and Data Members:

The public data members of objects of a class can be accessed using the direct member access operator (.). Let us try the following example to make the things clear:

When the above code is compiled and executed, it produces the following result:

class c++

Caution: It is important to note that private and protected members can not be accessed directly using direct member access operator (.). We will learn how private and protected members can be accessed.

Keep following codejow for more tutorials!

Previous PHP Tutorials 3: PHP Print "How to Print" and Descriptions
Next Function-Overloading in CPlusPlus

About author

Raza Bayani
Raza Bayani 44 posts

Raza Bayani studies BCE (Bachelor in Computer Engineering) in Poly-technical University of Kabul. He is the CEO of CODEJOW group, a web designer, coder and recently a blogger. Raza thinks, he can help others and also boost-up his own programming skills being on codejow.com.

View all posts by this author →

You might also like