This article is part of a series which teaches you the basics of how to write C++ software. One of the biggest differences between the C and C++ programming languages is that C++ is an Object-Oriented Programming (OOP) language that supports using Classes. In this post we will explain Object Oriented Programming and give you a basic introduction to what OOP is, why it is so popular and useful, and how OOP is an integral part of the C ++ programming language.
Table of Contents
Introduction To Object Oriented Programming
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. Some of the benefits of Object-Oriented Programming are,
- OOP helps you represent real-world objects and data in an abstract way in your code
- It can make coding faster and easier to execute by breaking logic and functionality into smaller, testable blocks of functionality
- OOP helps provide a clear structure for your C++ programs and encourage the concept of “separation of concerns” which is a key ideal
- 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). Conversely, 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’.
A Complete Guide To Programming Classes and Objects In C++
Classes are defined in C++ using the keyword class followed by the name of the class. Classes are the blueprint or baseline representation 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 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, hair type, number of eyes) and has human functions reading()
, writing()
, working()
, while engineers, workers, managers, athletes, and so on, can be objects that hold other additional class properties and functions such as “can run very fast
“, “is called John Smith
“, “has the nickname Smithy The Quick
” none of which make sense to set at the base class level since not all humans can run, or even run very fast. You might have a “nickname
” property though on the base or ancestor class with a default value of an empty string which only gets set in classes which descend from it.
I’m sure you can see that it’s important to put a lot of thought into your class hierarchy to make sure you write as a little code as possible and that the code you do write is implemented in the appropriate place.
- Learn about Object Oriented Programming, Introduction
- Learn Classes and Objects in C++
- Learn about Access Specifiers in C++ Classes
- Discover Class Methods in C++
- Learn to Use Object Arrays and Object Pointer Arrays in C++
- Learn About Explicit Specifier in C++ Classes
A Complete Guide To Programming Constructors In C++
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 (methods, functions of objects). These attributes and methods are variables and functions that belong to the class, they are generally referred to as a class member. In C++, Constructors are one of the members of classes.
Everything you need to know about C++ Constructors
The Constructor in C++ is a function, a method in the class, but it is a ‘special method’ that is automatically called when an object of a class is created. We don’t need to call this function. Whenever a new object of a class is created, the Constructor allows the class to initialize member variables or allocate storage. This is why the name Constructor is given to this special method.
- Learn Constructors in C++ Classes
- Learn what is Trivial Default Constructor in C++
- Learn about Declaration of a Default Constructor Inside of Class Definition
- Learn Definition of the Default Constructor Outside of a Class Definition
- Learn to use Deleted Implicitly-Declared Default Constructor
- Learn about Deleted Default Constructor in C++
- Learn Defaulted Default Constructor in C++
- Learn about Deleted Default Constructor in C++
- Learn about Defaulted Default Constructor Outside of a Class Definition
- Learn Implicitly Declared Default Constructor in C++
- Learn Implicitly Defined Default Constructor in C++
- Learn about Eligible Default Constructor in C++
- Learn about Forced Copy Constructor (Default Copy Constructor) in C++
- Learn about Deleted Copy Constructor (Avoiding Implicit Generation of the Copy Constructor)
- Learn about Implicitly-Declared Copy Constructor
- Learn about Declaration of a Copy Constructor
- Learn about Deleted Implicitly-Declared Copy Constructor in C++
- Learn about Declaration of a Copy Constructor
- Learn about Deleted Copy Constructor (Avoiding Implicit Generation of the Copy Constructor)
- Learn about Implicitly-Declared Copy Constructor
- Learn About Implicitly-Defined Copy Constructor
- What Is An Eligible Copy Constructor In C++ Software?
- This Is How Trivial Copy Constructors Work In A C++ App
A Complete Guide To Programming Destructors In C++
In C++ Object Oriented Programming, when you create a class, it has constructor and destructors automatically. The Destructor in a class is a special member function to delete objects and allow the class tidy up anything which was created and needs to be properly managed. The Destructor is called when the lifetime of an objects ends. The purpose of the destructor is to perform necessary operations when destructing or destroying the object. An Object may have acquired or allocated data on memory during the functioning run time of the app. If this is the case they need to be freed too when objects are being deleted. In C++ the destructor is the function that frees the resources of the object to which the destructor belongs.
Everything you need to know about C++ Destructors
When we construct an object, sometimes we need operations to deconstruct. Destructors are not only used in classes but also used with struct and union data types.
- What Is The Typical Declaration Of A Destructor In A C++ App?
- How To Force A Class Destructor In C++ Software
- What Is The Implicit Destructor Created By The C++ Compiler?
- What Is An Implicitly Declared Destructor In A C++ App
- Learn About Disabling The Implicit Destructor In A C++ App
- Learn About Deleted Implicit Destructors in A C++ App
- Learn About Deleted Implicit Destructors in A C++ App
- What You Need To Know About Virtual Destructors In C++ Apps
- Learn About Pure Virtual Destructors In C++ App Classes
- What Is The Implicit Destructor Created By The C++ Compiler?
A Complete Guide To Programming Advanced Class Features In C++
Encapsulation, Polymorphism and Inheritance are just some of the other great features of classes.
Encapsulation in C++ is used to cover attributes (properties) and methods (functions) inside a single statement called Class. In other words, it is wrapping up data and information under a single logical unit. Encapsulation is needed to be sure that sensitive data is hidden or protected from other parts of the program. It is to prevent access to the data directly. The access to the class’s data and functionality is provided through the public methods and properties of the class rather than by exposing ‘raw’ variables and private routines.
Inheritance is one of the most important concepts in object-oriented C++ programming. Inheritance allows us to define a class in terms of another class, and it makes easier to create and maintain an application. This also provides an opportunity to reuse the code functionality which means we write less code overall. Inheritance implements the relationship between classes. For example, a rectangle is a kind of shape, and an ellipse is also a kind of shape. So, the ‘ancestor class‘ would be ‘shape‘ and both a rectangle would inherit the properties and methods of what it means to be a shape. The traditional way of explaining class inheritance is describe a bird:
- A bird has wings.
- A bird has a beak.
- A bird can lay eggs.
- Some birds don’t fly so we need to have a property of the bird class which indicates whether or not it can fly. We will call that “
can fly
“. - A crow is a bird. So, we can inherit from the bird class.
- A penguin is a bird and can also inherit from the bird class.
- We don’t need to specify that crows or penguins have wings, beaks or lay eggs – that is the basic functionality of the ancestor class (i.e. all birds have these attributes, compared to a cow or a helicopter, which do not), and we don’t have to write any extra code – we only write that once for the “
bird
” ancestor class. The crow and the penguin automatically inherit the properties of having wings, a beak, and the ability to lay eggs. - Crows can fly so we set their “
can fly
” property to true in the constructor. - Penguins can’t fly so we set their “
can fly
” property to false in the constructor. - Note that if some injury occurs to a specific crow might no longer be able to fly – in which case we would set the “
can fly
” property of that specific instance of bird tofalse
. Theoretically we could give a jet pack to a penguin, and we could then set its “can fly
” property to true too. I’m pretty sure you’d also have to set a “penguin is extremely unhappy
” and “human goes to jail
” properties too though!
Polymorphism in C++ allows one class to inherit the attributes and methods from another class’s methods to perform different or additional tasks. Polymorphism means ‘multiple forms’, and this method is used when we have many classes that are related to each other by inheritance.
Advanced Class Features
- Learn about C++ Encapsulation
- Learn about Polymorphism :: Pointers to Base Classes in C++
- Learn C++ Inheritance :: Base Classes and Derived Classes
- Learn C++ Inheritance :: Multilevel Inheritance
- Learn C++ Inheritance :: Multiple Inheritance
- Learn C++ Inheritance :: Hierarchical Inheritance
- Learn C++ Inheritance :: Hybrid Inheritance
- Learn C++ Inheritance :: Ambiguity in Multiple Inheritance
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS and Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
You can download the free C++ Builder Community Edition here: https://www.embarcadero.com/products/cbuilder/starter.
Professional developers can use the Professional, Architect or Enterprise versions of C++ Builder. Please visit https://www.embarcadero.com/products/cbuilder.
Download RAD Studio 11 now
See what’s new in RAD Studio 11
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition