C Vs C++

Sometimes it is easy to understand another programming if you are too good at one.





1) Class in C++ vs Structure in C

  • What is a class in C++? It is nothing but a structure in C with security.  Below link will explain better.

https://www.geeksforgeeks.org/structure-vs-class-in-cpp/

  • A class cannot contain itself as a member in C++, but a class with pointer can be the member to itself.  Structure can contain itself as a member in C.

class Shirley {  
 char arr[] name;
 Shirley s;        // Invalid in C++
};
struct Shirley { 
 char arr[] name;
 Shirley s;        //Valid in C
};

  • By default, all class members are private in C++ class whereas C structure members don't have this security. 
  • Template type parameter can be declared with class. 

2) Structures in C++

  • The default access specifier for the members of the struct is public. 
  • C++ structures can have constructors, private member functions. But, C structures don't have these. 
  • Private member variables/functions can be declared before or after the public member variables/functions in C++.
  • Template type parameter can't be declared with struct keyword.



3) There are so many syntax differences like below:

printf("");      cout << "";

\n                   endl

NULL           nullptr

free               delete <ptr> or delete [] <ptr>

malloc()        new

4) There is no format specifiers in C++ so getting rid of another range issues. 

5) There is no user-defined initialisation function in C++, but we can have the constructor and destructor in C++. Initialisation functions are automated in C++ with the use of constructor & destructors. 

6) Addition of two structures is not possible though it is legitimate in C. But, this shortcoming is addressed in C++ with the help of operator overloading.

7)  C++ prevents the memory leaks with help of smart pointers. If the pointer is pointing nowhere, it is deleted. 

8) C++ is an expressive language with the use of abstractions and classes.

9) C++ does have an exception handling but C has to handle it on its own. Click here to read more.

10) Variables can be initialised in three different ways in C++

int depth = 10; // = expression 
int depth(10);  // (expression list)
int depth{10}; // {initialiser list}

11) In case of C, we will allocate memory with malloc whereas C++ containers come with their own predefined memory allocation. 

12) C is generally used for desktop computers, while embedded C is for microcontroller based applications. C can use the resources of a desktop PC like memory, OS, etc. While, embedded C has to use with the limited resources, such as RAM, ROM, I/Os on an embedded processor. Embedded C is just a superset of C.

Limitations of C++ in Embedded

My personal opinion is If you want to try to control the language than not the language controls you, then always C is the ideal choice. 

a) C++ is not closer to portable assembly language. 

b) More Memory Consumption: 

Predefined heap allocated memory using vectors can kill the embedded application if used in any critical data structure. Most modern C++ programs ignores this arena.

Memory can get bloated through the excessive use of virtual, certian template usage and inline by the bad programmers. 

c) C++ can't adopted to fit in other frameworks. It lags in portability.

d) 

Will be kept updated...  


Comments