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.
Table of Contents
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 methods, functions 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);
1 2 3 4 5 6 7 8 |
class TAnimal { public: string name; int age, weight; }; class TAnimal 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;
1 |
class TAnimal elephants[5], tigers[10], penguins[100]; |
Simple example of Staff class and it’s 3 objects;
1 2 3 4 5 6 7 8 |
class TStaff { public: string name; int age, salary; }; class TStaff manager, engineer, worker; |
Simple example to Customer class (TCustomer) and it’s object customer[] for 1000 customers;
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class TCustomer { public: string name; int age; String adress; String phone1; String phone2; String phone3; }; class TCustomer customer[1000]; |
Simple example to a Student class (TStudent) and it’s object students[] for 1000 students;
1 2 3 4 5 6 7 8 9 10 11 |
class TStudent { public: string name; string department; char gender; int age; String adress; }; class TStudent students[1000]; |
Simple example to a Item class (TItem) and it’s objects ( computers[], televisions[], refrigerators[] ) for 1000 each item groups;
1 2 3 4 5 6 7 8 9 10 11 12 |
class TItem { public: int itemid; string name; string model; char type; int weight; int price; }; class TItem computers[1000], televisions[[1000], refrigerators[1000]; |
We will explain more details about classes and objects in future posts.