C++Introduction to C++Learn C++

Learn Classes and Objects in C++

At the beginning of C++ programming mostly programmers ask what does Object Oriented means? What is Object Oriented Programming ? or What is the difference between Classes and Objects?. Let’s answer all these in this topic. One of the biggest difference between C and C++ programming languages is, C++ is an Object Oriented Programming (OOP) language that supports using Classes. 

Object Oriented Programming (OOP)

Object Oriented Programming is a way to integrate with objects which can contain data in the form (attributes or properties of objects), and code blocks in the form of procedures (methodsfunctions of objects). These attributes and methods are variables and functions that belong to the class, they are generally referred to as class members. Object-Oriented Programming (OOP) is a good way to work on data and work with functions in memory. Classes and Objects are the best way to work on properties and functions. Object-Oriented Programming has many advantages over procedural programming and it is the most characteristic feature of the C++ programming language. 

Classes and Objects

Classes are defined in C++ using keyword class followed by the name of the class. Classes are the blueprint for the objects and they are user-defined data types that we can use in our program, and they work as an object constructor. 

Objects are an instantiation of a class, In another term. In C++ programming, most of the commands are associated with classes and objects, along with their attributes and methods. 

The Class is a type (T word in front of most classes represents Type i.e. TButton class), and an object is a clone block of that class (i.e. Button1), that has the same properties and methods of its class. We can give an example in real life, a Human can be a class which has attributes like name, age, weight, height properties, etc. and methods like walking, swimming, seeing, smelling, etc. and Worker is an Object of this class and it has all same attributes and methods of this Human class.

Creating a Class
How to create a new class in C++ ? As in this human example, a simple Human class can be defined and create as below,

Creating an Object from a Class

Then, how i can create a object from a class in C++? Here, we created THuman Class and now we can easily define a worker Object from this Class as below,

Here worker has all name, age and height features that can be set by variables.

Setting a Object from a Class

Now if you are asking how i can set variables of an object in C++?, we have worker object in the memory with it’s attribute and methods fields. We can set set its each variables in their datatypes as below,

Let’s see all in this full example below,

Object Pointers

Objects can be defined as pointers as below too,

They should be allocated in memory, please read our post about object arrays.

Get started building powerful apps with C++Builder!


Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
CC++Introduction to C++Learn C++

How To Run A C Program in Command Prompt

CC++C++17Learn C++

How To Compile And Run A C Program

CC++Introduction to C++Learn C++

What Kind Of Programming Language Is C?

CC++Introduction to C++Learn C++

What Does C Mean In Programming?