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.

Related posts
C++Learn C++Videos

Learn C++ With Monoids, Monads, and Applicative Functors: Repeated Software Patterns (CPPCon 2020) Video

C++Learn C++Videos

Learn C++ With Pipes: How Plumbing Can Make Your C++ More Expressive by Jonathan Boccara (CPPCon 2020) Video

C++C++11C++14C++17Introduction to C++Learn C++

Learn about Access Specifiers in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++

Learn to Use Object Arrays and Object Pointer Arrays in C++

en_USEnglish