[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CompareCandCpp
Reasons are:
Note that in C++ there is the option to use a template function (note that the arguments are passed by reference, which is not possible in C):
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CompareCandCpp
(C FAQ and C++ FAQ) What are the differences between C and C++?
The main difference
The main difference between C and C++ is that C++ enables Object Oriented Programming.More differences
Both Bjarne Stroustrup and C++ FAQ LITE believe it is easier to learn C++ without learning C first.Reasons are:
- C++ is safer [1], e.g. it has increased type safety, which makes the use of Hungarian notation unnesessary.
- C++ is more expressive [1]. This is due to that C++ enables Object Oriented Programming.
- C++ reduces need to focus on low-level techniques as there are better libraries [1](the STL). E.g. using a std::vector saves you a lot of time managing and writing functions for arrays. See here to make a C analog of a std::stack.
- C uses malloc instead of new, the latter being safer [3].
- C uses printf instead of cout, the latter being safer [3].
- C unnecessary uses type-dependent switch statements, where C++ can solve these using polymorphism [3].
- C++ has error-code exception handling, as it supports the keywords try and catch [3].
- C++ makes #define macros unnecessary, using template or inline functions [3].
- C++ can overload functions with the same name, if the compiler can distinguish them from the functions' arguments.
Code differences
Comments
/* A C comment that can span multiple lines */ // A C one-line comment that not a compilers support /* A C++ comment that can span multiple lines */ // A C++ one-line comment that all compilers support
Function declarations of a function that takes no arguments
void sayHello(void); /* Correct C style */ void sayHello(); //Preffered C++ style
Function definitions
/* C code */
void doSomething(void)
{
/* Declare all variables at start of function */
int i, j;
/* Initialize variables later */
for (i = 0; i<10; i++) { /* STUFF */ }
j = doSomethingElse();
}
//C++ code
void doSomething()
{
//Always declare and immediatly initialize variables
for (int = 0; i!=10; ++i) { /* STUFF */ }
const int j = soSomethingElse();
}
Global constants
/* C code *///C++ code const double pi = 3.14159265;
- define M_PI 3.14159265
Function overloading
/* C code */
/* Needs different name for each function */
int add2(int a, int b) { return a + b; }
int add3(int a, int b, int c) { return a + b + c; }
int add4(int a, int b, int c, int d) { return a + b + c + d; }
//C++ code
int add(int a, int b) { return a + b; }
int add(int a, int b, int c) { return a + b + c; }
int add(int a, int b, int c, int d) { return a + b + c + d; }
/* C code */
/* Needs different name for each function */
int add2ints(int a, int b) { return a + b; }
double add2doubles(double a, double b) { return a + b; }
//C++ code
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
Note that in C++ there is the option to use a template function (note that the arguments are passed by reference, which is not possible in C):
//C++ code
template <typename T> T add(const T& a, const T& b) { return a + b; }
Variables that can be of any type
/* C style */
void doSomething(void * anything)
{
int alpha;
MyParameters myParams;
myParams = (*anything); /* A cast is needed */
alpha = myParams.alpha;
}
//C++ style
template <class T>
void doSomething(const T& anything)
{
const int alpha = anything.alpha;
}
Casting
/* C style */ int x, y; double z; x = 22; y = 7; z = (double) x / (double) y; //C++ style const int x = 22; const int y = 7; const double z = static_cast<double>(x) / static_cast<double>(y);
Passing values by reference
/* C style */
void swap(int * a, int * b)
{
int temp;
temp = *a;
- a = *b;
- b = temp;
Allocating memory
/* C style */ double * myArray = malloc(sizof(double) * 10); /* myArray has size 10 */ free(myArray); //C++ style double * myArray = new double[10]; //myArray has size 10 delete[] myArray;
Dynamically creating structs
/* C style */
typedef struct
{
int mX;
} MyStruct;
void initMyStruct(MyStruct * myStruct)
{
myStruct->mX = 0;
}
MyStruct * myStruct = malloc(sizof(MyStruct));
initMyStruct(&myStruct);
free(myArray);
//C++ style
struct MyStruct
{
MyStruct() : mX = 0 {}
int mX;
};
MyStruct * myStruct = new MyStruct; //Calls constructor
delete myStruct;
C++ Benefits
Fate of function arguments
In C++ the fate of function arguments is clearer
/* C code */
typedef struct { /* STUFF */ } MyStruct1;
typedef struct { /* STUFF */ } MyStruct2;
void MyFunction(MyStruct1 * m1, MyStruct2 * m2)
{
/* Does MyFunction change the values of m1, m2, both or neither? */
/* This cannot be specified */
}
// C++ code
struct MyStruct1 { /* STUFF */ };
struct MyStruct2 { /* STUFF */ };
void MySetFunction(MyStruct1 * m1, MyStruct2 * m2)
{
//If programmer is const-correct,
//porbably both structs are modifed!
}
void MyGetFunction(const MyStruct1 * m1, const MyStruct2 * m2)
{
//Both structs cannot be modified
}
Other comparisons
- What are the differences between C and other languages?
- What are the differences between C++ and other languages?
References
- 1) Bjarne Stroustrup. The C++ Programming Language (3rd edition).ISBN: 0-201-88954-4, chapter 1.2 'Learning C++'
- 2) http://new-brunswick.net/workshop/c++/faq/how-to-learn-cpp.html, topic [28.2]
- 3) http://www.research.att.com/~bs/bs_faq.html#prerequisite
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
