[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
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: After having learned the common subset of C and C++, it is easier to find out the trickier parts of C [1,2]. See C++ keywords for a list of C++ keywords and if these have an equivalent in C.

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 */
  1. define M_PI 3.14159265
//C++ code const double 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;
} int main() { int x, y; x = 5; y = 10; swap(&x,&y); } //C++ style void swap(int& a, int& b) { const int temp = a; a = b; b = temp; } int main() { int x = 5; int y = 10; swap(x,y); }


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

References



last edited (December 24, 2006) by bilderbikkel, Number of views: 3857, Current Rev: 11 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.