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 and it’s objects for 1000 customers;

Simple example to a Student class and it’s objects for 1000 customers;

Simple example to a Item class and it’s objects 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!

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

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
C++Introduction to C++Language FeatureLearn C++

How To Use The Comprehensive Form Properties In C++ VCL Apps

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

This Is How To Add Characters to Wide Strings in Modern C++

C++ComponentsLanguage FeatureLearn C++

How To Create Gorgeous Transparent App Windows With C++

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

This Is How To Get A Substring of a Wide String in C++

Worth reading...
Learn Classes and Objects in C++
en_USEnglish