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!

close

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.

About author

Dr. Yilmaz Yoru has 33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He was born in 1974, Eskisehir-Turkey, started coding in college and graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. He worked as a research assistant at the same university for more than 10 years. He received his MSc and PhD degrees from the same department at the same university. Since 2012, he is the founder and CEO of Esenja LLC Company. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17Learn C++Templates

Learn How To Eliminate Pointers In Templates in C++

C++C++11C++14C++17Learn C++Templates

Learn To Export And Import Template Members From A DLL Or A Package In C++

C++C++11C++14C++17Learn C++Templates

What Is A Function Template In C++?

C++C++11C++14C++17Learn C++Templates

What Is A Class Template In C++?