Object-Oriented Programming (C++)-The Easy Way (Part-II)

Rahul Sharma
6 min readApr 13, 2020

--

OOPS!! — Photo by Jelleke Vanooteghem on Unsplash

What’s up fellas? This what you see is the second part of my Object-Oriented Programming concepts series. I would be covering some interesting stuff including Copy Constructors, Friend functions, Structs and many more.
If you missed the first part, nothing to worry, I am leaving the link below:

We covered really cool concepts including classes, objects, constructors and destructors in the above part and aim to cover a lot more in this one. Let’s get going!

  • Did you know that we could copy our constructors?

If we go by the literal definition, a Copy Constructor simply means an overloaded constructor used to declare and initialize an object of a particular class from another object of the same class.
Puzzled? Let’s try to clear out the confusion in the room with the help of a code:

Default Copy Constructor

In the above program, we created a copy of the object c1 using the assignment operator (=) and called its member function, printCountryDetails() to display the member fields. Here, the assignment operator calls the Default Copy Constructor given by the compiler which shallow copies all the member fields. Wait, shallow copy?

Well, a shallow copy occurs when the data of all the member fields of a class instance is copied as it is to another. Simple? But, where would a pointer point if it were also a class member? Would it also have two copies? What would happen if I update/delete one of the pointer’s data?

Well, this is a drawback of the shallow copy, it doesn’t dynamically allocate a separate memory location for the copied pointer. This simply means, when the copy is done, there would be two pointers pointing to the same memory location and any update to it will also affect the other pointer.

Pardon my drawing skills :P

So how do we deal with this? Creating a User-defined Copy Constructor can help! Let’s digest it with the help of a code:

User-defined Copy Constructor

Above, we used our own copy constructor which dynamically allocates different memory locations for the class members and copies data onto them.
This behavior is also called Deep Copy, which is possible when we write our own copy constructor.

As we now are, a little bit versed with the concept of copy constructors, moving forward, I had a question in my mind!

Did we know we don't always need to create an instance object in order to access the methods and fields within a class?

Yes! We can simply call a particular method or a field by making it Static. Static is a keyword that doesn’t belong to any particular class or its instance.

Let’s understand the use of static with the help of an example:

Static Keyword

A static member function can only access static data member, other static member functions and any other functions from outside the class. Moreover, it can only be accessed with the class name and scope resolution (::) operator as we could see in the above code.

Static member functions have a class scope and they do not have access to this pointer of the class. If at all you intend to do so, don't be surprised when the below error pops!
‘this’ may only be used inside a nonstatic member function.”

Using the static keyword also has an advantage! We don't need to create a class instance now to access the static members of the class. Also, the static member will not be assigned a separate memory next time when a new class instance is created. What a memory-efficient move!

Since we are so much into classes, how about we make a stride back, investigate Structs and perceive how well it does its job compared to a class!

  • C++ Structs

Generally, structs are used for storing light-weight objects. Some people often use struct when they want it to contain only data members and not member functions, although it supports both! But, people generally have a feeling of the struct as a plain-old-data (POD) type and don’t often write functions within it!

Let’s try to grasp this concept with the help of a code:

Structs in C++

Above, I tried to create the exact same scenario as we had in our class example. Both have a constructor method and other member functions as well. Some more examples might include storing the dimensions of a polygon within a struct and then using it to calculate its area.

Till now, we haven’t really compared structs with classes? So, how do they differ?

Well, a major difference between them is, by default the members of a class are declared with a private access modifier whereas, in a struct, the default access modifier is public. Similarly, when inherited, if not specified explicitly, a struct will inherit publicly whereas, a class will inherit privately.

That’s all for structs, I guess! You can definitely experiment with different use cases of a struct going forward!

Since we are discussing data structs, let’s not leave our enum behind!

  • C++ Enumeration (enum)

We use enum when we want a variable to have a value from a specific set of predefined values. This can be helpful in switch statements where we don’t want the switch variable to have any other value than our defined cases. Well, obviously we can rely on the Default for such cases.

For e.g. a variable Direction can have only four concrete values as {“North”, “South”, “East”, “West”} and any other value should throw an error! If you feel the direction could also be, “North-West”? Feel free to add it to your custom enum! ;-)

Let’s grasp this concept quickly through a small piece of code:

To add, enum variables are static and final implicitly. I didn’t tell you final right? Well, an enum class can’t be inherited and hence is final. The final specifier in C++ when added to a function, prevents it from being overridden by a base class.

Don’t forget to notice that I used two separate ways to declare the enum variable. Furthermore, enum variables also iterable! Isn’t it great?

Since you have read (I assume!) the blog till here, we are friends now! :-P

  • Friend Functions

Like real-life friends, C++ functions/classes being a friend to another class can access its private/protected members. We just need to declare the function as a friend function within the scope of the class. The function definition itself is not in the class scope and hence, can not be called using a class object.

Let’s clear the confusion through an example:

C++ Friend Functions

Here, the function getPopulationCount() is declared as a friend function and its definition lies outside the scope of the class Country. Another point to note is that a friend function can be invoked like a normal function without using the object. It cannot access the class member names directly and has to use an object name and dot membership operator (.) with the member name.

It’s not like only a function can be a friend to a particular class. But, a class can also be a friend to another.

That’s it for this blog, I guess. We were able to cover copy constructors, static, structs, enums, and friend functions in this blog.

Next up is understanding the pillars of OOP. I have covered Inheritance in my next article. Feel free to read, I am leaving the link down below:

I would be posting a few more blogs that would cover Abstraction, Encapsulation, and Polymorphism. Don’t worry, I will update the links here once I am done! :)

Meanwhile, did you know you can make me smile by clapping for this post?

About me? I am a tech lover! I love to read, implement and explain :)

--

--

Rahul Sharma

An enthusiastic full stack web developer and motivator!