Site icon Embarcadero RAD Studio, Delphi, & C++Builder Blogs

A Complete Guide To Programming In C++ – Object Oriented Programming

slide5 2

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.

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 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. Some of the benefits of Object-Oriented Programming are,

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.

  1. Learn about Object Oriented Programming, Introduction
  2. Learn Classes and Objects in C++
  3. Learn about Access Specifiers in C++ Classes
  4. Discover Class Methods in C++
  5. Learn to Use Object Arrays and Object Pointer Arrays in C++
  6. 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 (methodsfunctions 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.

  1. Learn Constructors in C++ Classes
  2. Learn what is Trivial Default Constructor in C++
  3. Learn about Declaration of a Default Constructor Inside of Class Definition
  4. Learn Definition of the Default Constructor Outside of a Class Definition
  5. Learn to use Deleted Implicitly-Declared Default Constructor
  6. Learn about Deleted Default Constructor in C++
  7. Learn Defaulted Default Constructor in C++
  8. Learn about Deleted Default Constructor in C++
  9. Learn about Defaulted Default Constructor Outside of a Class Definition
  10. Learn Implicitly Declared Default Constructor in C++
  11. Learn Implicitly Defined Default Constructor in C++
  12. Learn about Eligible Default Constructor in C++
  13. Learn about Forced Copy Constructor (Default Copy Constructor) in C++
  14. Learn about Deleted Copy Constructor (Avoiding Implicit Generation of the Copy Constructor)
  15. Learn about Implicitly-Declared Copy Constructor
  16. Learn about Declaration of a Copy Constructor
  17. Learn about Deleted Implicitly-Declared Copy Constructor in C++
  18. Learn about Declaration of a Copy Constructor
  19. Learn about Deleted Copy Constructor (Avoiding Implicit Generation of the Copy Constructor)
  20. Learn about Implicitly-Declared Copy Constructor
  21. Learn About Implicitly-Defined Copy Constructor
  22. What Is An Eligible Copy Constructor In C++ Software?
  23. 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.

  1. What Is The Typical Declaration Of A Destructor In A C++ App?
  2. How To Force A Class Destructor In C++ Software
  3. What Is The Implicit Destructor Created By The C++ Compiler?
  4. What Is An Implicitly Declared Destructor In A C++ App
  5. Learn About Disabling The Implicit Destructor In A C++ App
  6. Learn About Deleted Implicit Destructors in A C++ App
  7. Learn About Deleted Implicit Destructors in A C++ App
  8. What You Need To Know About Virtual Destructors In C++ Apps
  9. Learn About Pure Virtual Destructors In C++ App Classes
  10. 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:

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

  1. Learn about C++ Encapsulation
  2. Learn about Polymorphism :: Pointers to Base Classes in C++
  3. Learn C++ Inheritance :: Base Classes and Derived Classes
  4. Learn C++ Inheritance :: Multilevel Inheritance
  5. Learn C++ Inheritance :: Multiple Inheritance
  6. Learn C++ Inheritance :: Hierarchical Inheritance
  7. Learn C++ Inheritance :: Hybrid Inheritance
  8. 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

Exit mobile version