C++ is a powerful general-purpose and object-oriented programming language created by Bjarne Stroustrup, released in 1985. It can be used to create small programs or large applications. It can be used to make CGI scripts or console-only DOS programs. C++ allows you to create programs to do almost anything you need to do. The C and C++ programming languages are closely related.
The C and C++ programming languages are closely related. Basically, C++ produced out of C. But C++ introduces many new features that are not available in C. This article mainly focuses on differences between C and C++.
Differences:
1. C++ is a Compiled Language whereas C is an Interpreted Language
Compiled languages are translated to the target machine's native language by a program called compiler. Only once program needs to be compiled then it resides in complied form and results in Fast execution especially if the compiler is effective at optimizing.
Interpreted languages are read by a program called an interpreter and are executed by that program. Interpreted languages are usually much slower than an equivalent compiled program because interpret need to translate code to that machine code every time program executes.
So, C++ is faster than C Language.
2. C++ is an Object Oriented Language while C is a Procedural Language
C follows the procedural programming paradigm while C++ is a multi-paradigm language (procedural as well as object oriented) which means functions are the building blocks of a C program while objects are building blocks of a C++ program.
3. Syntax Differences:
Both C and C++ have different syntax. Let's look at a simple working "Hello World" program which says "hello" to the users of your software or application:
//C hello world example
#include <stdio.h>
int main()
{
printf("Hello World\n");
return 0;
}
//C++ hello world example
#include <iostream>
using namespace std;
int main()
{
cout<<" Hello World!\n";
cin.get();
}
4. The “NAMESPACE” feature in C++ is not available in C
C++ uses NAMESPACE which avoids name collisions. For instance, two courses offered in the same university cannot have the same course code while two courses in different universities might have the same course code. The universities are two different namespaces and hence contain the same course codes (identifiers) but the same university (one namespace) cannot have two courses with the same code (identifier).
5. C++ is Type-Safe but C is not
Unlike C, C++ is a strongly typed called a type-safe language. Type Safety is the extent to which a programming language avoids type errors. A type error is flawed or undesirable program behavior caused by the inconsistent data types for the program's constants, variables or functions for example, assigning a floating variable (float) to an integer variable (int) in C++ is not allowed.
6. C++ allows using functions inside Structures while C does not
In case of C++, functions can be used inside a structure while structures cannot contain functions in C.
7. C++ supports function overloading while C does not
Overloading means two functions having the same name in the same program. This can be done in C++ with the help of Polymorphism whereas C does not support this feature of OOP.
8. C++ allows the use of reference variables while C does not
Reference variables allow two variable names to point to the same memory location. We cannot get benefit of this feature in C language.
9. Exception Handling is not supported by C but C++ provides support for it
C does not support exception handling “properly” but it can always be implemented by other methods.
10. new[] and delete[]
In C, there's only one main memory allocation function “malloc()”. You use it to allocate memory to both single elements as well as arrays. Here is sample code:
int *x = malloc( sizeof(int) );
int * TestArray = malloc( sizeof(int) * 10 );
and you always release the memory in the same way as below:
free( x );
free(TestArray);
In C++, however, memory allocation for arrays is somewhat different than for single objects; you use the new[] operator and you must match calls to new[] with calls to delete[].
int *x = new int;
int *TestArray = new int[10];
delete x;
delete[] TestArray;
The short explanation is that when you have arrays of objects, you need to use delete[] to properly call the destructor for each element of the array, whereas delete will not work for arrays.
No comments:
Post a Comment