Site icon Learn 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,

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);

[crayon-66067e7675da3740198913/]

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;

[crayon-66067e7675daf051179314/]

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

[crayon-66067e7675db2447896564/]

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

[crayon-66067e7675db4925332250/]

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

[crayon-66067e7675db8297330867/]

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

[crayon-66067e7675dba514149095/]

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

 Get started building powerful apps with C++Builder!

Exit mobile version