C++Introduction to C++Language FeatureLearn C++

Learn about Object Oriented Programming, Introduction to OOP

When learning about C++ programming a lot of programmers have many questions. Let’s answer them 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. In this post we will explain Object Oriented Programming, In another term this is an Introduction to OOP.

Object Oriented Programming (OOP)

Object Oriented Programming is a way to integrate with objects which can contain data in the form of attributes or properties of objects, and code blocks in the form of methodsfunctions of objects. These attributes and methods that belong to the class 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.

Before learning about Classes and Objects it is important to learn about Object Oriented Programming.

Benefits of Object-Oriented Programming are,

  • faster and easier to execute
  • provides a clear structure for the programs
  • helps to keep the C++ code DRY “Don’t Repeat Yourself”, and makes the code easier to maintain, modify and debug
  • makes it possible to create full reusable applications with less code and shorter development time

Procedural programming is about functions ( procedures, sub-functions, algorithms ) that perform operations on variables (properties). While OOP is about objects with a defined class that contain both properties and functions. OOP is a modern way to use both variables and functions safely.

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. 

For example; a Human can be a class that has human properties (age, weight, gender, salary, id number, …) and has human functions reading(), writing(), working() …, while engineers, workers, managers, athletes,… can be objects that holds many names and other human properties and functions.

Understanding Object Oriented Programming

Today, we have many variables, and functions running in our applications, and in our application user interface frameworks (buttons, labels, text boxes, drop down comboboxes). We also have many variables (integers, floating points, strings, Booleans) in tables with enormous size of databases. We have to integrate all of this in harmony. The best way is to categorize each of these in their logical meaning in our real life. So if we are developing a software for a real world, and if we want our apps to work in similar ways, we should define variables and functions accordingly.

For example, we can do an application for a zoo with a database, we may store each animal type with their name, age, weight, .. etc. In our real life, all animals in the zoo have names or ids, age, weights, etc. In this zoo, animals comes, births, deaths and sometimes they can be moved from one section to another section of the zoo.

In Object Oriented Programming, for this zoo app example, we can define a Animal Class with a name, age, weight variables and these animals comes to zoo from somewhere so we should add them too zoo, births, they can be moved from one place to another place. As an Object of this Animal class , we can create Elephant, Tiger, Penguin … objects with this Animal class. So each animal type will have same attributes as Animals. We can move their place, we can add them or we can report them as dead. In a way, our objects work similar to real life.

Note that a class is a template for objects, a definition; an object is an instance of a class. They have the same attributes and functions. If an individual object is created, they inherit all the variables and functions from the class. Variables of objects can be set and functions of objects runs with their parameters and statements inside as in described in class..

Examples of Object Oriented Programming

Here are some class and object definition examples in C++;

Simple example of an Animal class and objects for 3 kinds (elephant, penguin, tiger);

Here elephant, penguin and tiger are single class objects. For a zoo which has 5 elephants, 10 tigers and 100 penguins we can define objects for this class as below;

Simple example of Staff class and it’s 3 objects;

Simple example to Customer class (TCustomer) and it’s object customer[] for 1000 customers;

Simple example to a Student class (TStudent) and it’s object students[] for 1000 students;

Simple example to a Item class (TItem) and it’s objects ( computers[], televisions[], refrigerators[] ) for 1000 each item groups;

We will explain more details about classes and objects in future posts.

 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.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++Syntax

Learn Default Constructors Of Classes In Modern C++

Artificial Intelligence TechC++C++11C++14C++17Learn C++

How To Develop Special AI Activation Functions In C++?

Artificial Intelligence TechC++C++11C++14C++17Language FeatureLearn C++

Learn How To Read The WAV Waveform Audio File Format In C++

C++C++11Learn C++

What Is Assignment Operator Overloading?

Worth reading...
Learn Constructors in C++ Classes