Delving into C++
Commenced as a part of our curriculum, we, TY E&TC students of VIT, Pune have started this activity. Till now, we have seen the most basic concepts encompassing the Object-Oriented nature of C++. In this continuation to series of our blogs, we’ll take a deep dive into more of the different types of techniques allowed to us by this promising programming language.
To start with, we have an amazing concept of Data Binding in C++. It’s of two types:
· Static Data Binding
· Dynamic Data Binding
‘Binding’ refers to the concept of assigning one thing to another. When a function is called by a program in C++, the execution handler, during the compile time, binds the program control to the memory address where the function is defined. Let’s see each one the binding types in brief –
Static Data Binding
This type of binding occurs when all the information/data needed by the function is available at run-time. As all the information is available during the compile-time, this results in faster execution of the program. But this results in less flexible style of programming. Also known as early binding.
Dynamic Data Binding
This binding happens when all the information required by the function is not available at the compile time and needs to be decided at the run-time. Here, the function calls are not resolved until the run-time of the program. This results in comparatively slower execution of the program. Flexibility is most important advantage in this case as this allows single function to handle different objects at the run-time. This reduces the size of code base. Also known as late binding.
Static Binding happens during usual function calls, function overloading, etc. but for the Dynamic Binding, Virtual Functions are used.
Thus, we come to the concept of Virtual functions
Virtual Functions
During inheritance if base class and derived class have a method by same name, then by default, the method of base of class is called. But, if we add the keyword “virtual” to the method of base class (having the same name), then we can have what we call function overriding and hence, the method of derived class can be accessed efficiently. These are mainly used to achieve the objective of run-time polymorphism, in which, resolving the function call is done at the run-time (dynamically).
There are furthermore details and the concept of vptr and vtable in this context but, we will not dig into that as far as the purpose of this blog is concerned. You may read more about it, as per your will.
Some rules for virtual functions:
1. Virtual functions can never be static and can’t be friend functions
2. Prototype of this function should be same in base as well as in derived class
3. They are always defined in base class and they override the same methods of the derived class. If defined in both base and derived, method in the base class is called
4. A class may have a virtual destructor, but it cannot have a virtual constructor
Another point to note is that, whenever virtual function is called using base class reference or pointer, then, it can’t be inlined (as the call is resolved at run-time), but when it is called using the object (without reference or pointer) of that class, then it can be inlined because compiler knows the exact class of the object at compile time.
The concept of the inline functions is that, whenever an inline function is called, the lines of code of inline function gets inserted/substituted at the point of code where the inline function is called at compile time.
Now, to end this blog, I will share some amazing hidden tips that we can do in C++. Do try out!
Variable declaration inside a conditional statement
struct Books { virtual ~Books() {} };
struct Drama : Books { int x, y; };
struct Fantasy : Books { int key; };
void log(Books *books) {
if (Drama *drama = dynamic_cast<Drama *>(event))
cout << "Drama " << drama->x << " " << drama->y << endl;
else if (Fantasy *fantasy = dynamic_cast<Fantasy *>(event))
cout << "Fantasy " << fantasy->key << endl;
else
cout << "Books" << endl;
}
See this code snippet…
This doesn’t run into any compile error and does give output!
Also, in this code snippet, all the things we discussed are covered in an excellent way, so do try to investigate on this!
Redefining Keywords
Also known as an ‘Evil Programmer’s Friend’, yes, this is possible in C++.
#define int float
#define float char
As written in the code snippet, redefining key words is allowed to do using special tools and compilers and not all the versions of compilers may support it. But this lets you do fun bug-introducing stuff like #define true false or #define else.
#define public private
#include “mylibrary.h"
#undef private
Here, to protect the header file from unauthorized access, we redefined the keywords and then we can write our program. This helps to protect our data and then, at the end turn off the protection when we are all done (using #undef)!
At last, try out -
int main()
{
char s[5] = “hello”;
cout << s[0] << endl;
cout << 0[s] << endl;
return 1;
}
YES, this is valid!
- Saransh Kulkarni
TY – K, 43
Very well written blog
ReplyDeleteGood job 👍👍
Thanks brother!
DeleteCopacetic Explanation !
ReplyDeleteThanks for the 'copacetic' comment!
DeleteNicely done!
ReplyDeleteThank You!
DeleteExcellent
ReplyDeleteThanks
Delete