Features in C++ but not in C

Features in C++ but not in C

If you are transitioning from learning C to C++, you will encounter many new features that may be unfamiliar and confusing. I have also experienced this issue, so I have compiled a list of features in C++ that are not present in C:

Object-Oriented Programming and Classes:

C++ was originally called C with classes, it was built to have the modern features of object-oriented programming such as classes, objects, encapsulation, inheritance, and polymorphism.

On the other hand, C is a procedural language, where code runs function by function. You will realize the advantage of C++ when you build the same program using both C and C++.

Casting of variables:

In C, if you want to cast an int to a long int, for example, you'd use

         int i=0;
         long l = (long) i;

In C++, you can use a function-like call to make the cast.

         long l = long(i);

It's easier to read. Since it's possible to create functions to perform casts involving user-defined types, this makes all the casts look consistent.

Function Overloading:

In C, as in most other programming languages, every function must have a unique name. At times, it can be annoying. Imagine you want to have a function that returns the abs value of an integer.

         int abs(int i);

If you need to figure out the abs value of every possible available data type, you then have to write a function for each of the possible types:

         long labs(long l);
         double dabs(double d);

All those functions do the same thing -- return the abs value of the argument. Thus it seems silly to have a different name for each of those functions. C++ solves this by allowing you to create those functions with the same name. This is called overloading. For example, you can do the above in C++:

         int abs(int i);
         long abs(long l);
         double abs(double d);

And depending on the type of parameter you pass into the 'abs' func. C++ will select the right one.

References:

C can be clumsy sometimes. When you write a function to swap two integers, you have to pass the two integers into the function by reference:

         void swapint(int *a, int *b)
         {
            int temp;

            temp = *a;
            *a = *b;
            *b = temp;
         }

Here's the function call:

         swapint(&i1, &i2);

C++ supports a special type of identifier known as 'reference' &. It makes changing the parameter values in a function relatively painless. The above function can be rewritten in C++ as follows:

         void swapint(int &a, int &b)
         {
            int temp = a;
            a = b;
            b = temp;
         }

Function call:

         swapint(i1, i2);

This is much simpler.

The "const" keyword

In C we used to declare constant values as below:

#define MAX_CUSTOMERS   10
#define ITEMS 20

In C++ you have the const keyword :

        const int ArraySize = 100;
         int Array[ArraySize];

"this" keyword

"this" keyword acts as a pointer 👉 to an object whose member function is when called.

Example:

class Rectangle {
   private:
      int length;
      int width;
   public:
      void setDimensions(int l, int w) {
         this->length = l;
         this->width = w;
      }
};

"new" and "delete"

In C, all dynamic memory allocation is handled via library calls, such as 'malloc' and 'free'.

In C++, there are new ways of dynamically allocating memory using operators called 'new' and 'delete', where 'new' replaces 'malloc' and 'delete' replaces 'free' in C.

Here is how we can use it:

            int *i = new int; // new will give a pointer variable which will be stored in i.
            *i = 10;
            cout << *i;
            delete i;

Operator Overloading

C++ allows operators such as +, -, *, and / to be overloaded, which means that their behaviour can be customized for user-defined types. This feature is not available in C.

Conclusion

That's it for now. There are still a few features such as Namespaces, scope operator, STL(Standard Template Library), etc.

I hope this helped. If yes then comment your views below and also you can subscribe to my newsletter by putting your email below, where you will be notified whenever I make a new post.

Thanks for reading ❤️.