Difference between C and C++


 
Thread Tools Search this Thread
Top Forums Programming Difference between C and C++
# 1  
Old 11-24-2005
Difference between C and C++

Hi,

What is the difference between the C and C++. And what new features is added in the C++.then C Programming in the UNIX Environmnet.

Thanks:
www.hytechpro.com
# 2  
Old 11-25-2005
Check google. Some tips:
1
2
# 3  
Old 11-29-2005
A short summary:
  • classes, and all the stuff that comes with them -- constructors, destructors, virtual functions, overloading, new/delete. This is chapters and chapters of stuff in itself, too complicated for a brief overview.

  • global variable initialization from non-constants -- consider this use of a global variable:
    Code:
    #include <stdlib.h>
    
    void *globalmem=malloc(1024);
    
    int main()
    {
      return(0);
    }

    This program will not work in C because malloc(1024) is a function call, which is obviously not a constant value. But this will work in C++, which calls the appropriate functions before main() to take care of these strange global things.

  • typecasting is stricter -- C will let you get away with assigning one kind of pointer to another -- or integer to pointer, or vice versa -- with only a warning, but in C++ it's a full-out error. You even need a typecast to convert from an enum to an integer in C++!

  • calling an undeclared function is an error -- in C, undeclared functions are assumed to take integer paramaters, int somefunc(int param1, int param2); which sometimes works well enough, but not always. This is totally disallowed in C++. For example, this code "usually" works in C but not in C++:

    Code:
    /* #include <stdio.h> should be here, but we're too lazy! */
    int main()
    {
      printf("Hello World\n");
      return(1);
    }

    As it turns out, they had a good reason to disable this behavior in C++. C's "everything undeclared is an integer" assumption totally breaks on 64-bit systems where pointers are a different size than integers.

  • hidden pointer madness -- C++ offers a syntax that I hate hate hate, that allows you to pass by reference without an obvious pointer. They call these "references". This C code:
    Code:
    void function(int *a, int b) { (*a) += b; }

    is equivalent to this C++ code:
    Code:
    int function(int &a, int b) { a+=b; }

    This syntax might fool you into thinking that you don't need to error-check pointers any more, but "int &a" is still a pointer, just a pointer that the compiler does tricks to let you use like a normal variable, and subject to segmentation faults if something feeds it a garbage value, or something that's been freed, or whatever -- just like real pointers.

  • structures work in a more obvious way -- In C, you need to do this:
    Code:
    struct something { int val; };
    struct something myvar;

    In C++ you can do this:
    Code:
    struct something { int val; };
    something myvar;

  • function overloading -- In C, this won't work:
    Code:
     int value(int a, int b) { return(a+b); }
    float value(float a, float b) { return(a+b); }

    -- it'll complain that 'value' is being redeclared. This will work in C++ because it sneakily renames these functions behind the scenes so they're really NOT the same, and tries to be intelligent about which is called for what kind of paramaters.


    This makes calling C functions from C++ -- and vice versa -- difficult. In both cases, C++ needs to be told to make the functions extern "C" so it doesn't mangle the names, like this:
    Code:
    extern "C" {
    int function_called_from_c_code(int a, int b);
    int function_called_by_c_code(int a, int b) { return(a+b); }
    }


Last edited by Corona688; 11-29-2005 at 09:55 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to calculate difference of split and sum the difference

In the awk I am trying to subtract the difference $3-$2 of each matching $4 before the first _ (underscore) and print that value in $13. I think the awk will do that, but added comments. What I am not sure off is how to add a line or lines that will add sum each matching $13 value and put it in... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Get difference

Hi .. I am trying to create one function. It will have two arguments. Argument1: a,b,d,f,g Argument2:21212,sfsd,4546,67867,a,asda,b So the output will be Argument1 - Argument2 which is d,f,g Can anyone help with this one? (4 Replies)
Discussion started by: Anupam_Halder
4 Replies

3. Programming

what is the main difference between difference between using nonatomic lseek and O_APPEND

I think both write at the end of the file ...... but is there a sharp difference between those 2 instruction ..... thank you this is my 3rd question today forgive me :D (1 Reply)
Discussion started by: fwrlfo
1 Replies

4. UNIX for Dummies Questions & Answers

Difference between sh and ./

Hi All Can any body please tell me what is difference between sh scr ./scr Here scr is a script. (1 Reply)
Discussion started by: parthmittal2007
1 Replies

5. UNIX for Dummies Questions & Answers

Difference between

$x=10 and providing the spaces between = and 10 $x= 10 (4 Replies)
Discussion started by: shashank1311
4 Replies

6. Shell Programming and Scripting

Difference between 1,$ and /g

just wondering what the difference is between 1,$ and /g when doing a substitution in vi. doesn't seem to be much difference from what i can see. (2 Replies)
Discussion started by: bigubosu
2 Replies

7. UNIX for Advanced & Expert Users

difference

difference b/w shell scripting and perl scripting (2 Replies)
Discussion started by: simmijaswal
2 Replies

8. Shell Programming and Scripting

Difference between $* and $@

Somebody please tell me the difference between $@ and $* Thanks in advance. Saneesh Joseph (1 Reply)
Discussion started by: saneeshjose
1 Replies

9. Linux

what is the difference between -h and -H ?

samba:/home/backup # df -h /home/ Filesystem Size Used Avail Use% Mounted on /dev/sdb2 34G 8.6G 26G 26% /home samba:/home/backup # df -H /home/ Filesystem Size Used Avail Use% Mounted on /dev/sdb2 37GB 9.2GB 28GB 26% /home what... (2 Replies)
Discussion started by: cw1972
2 Replies

10. UNIX for Dummies Questions & Answers

Where is the difference?

Hello I would like to know where there is a difference between these two machines? HP9000-735/125 HP9000-B132L What does that all mean? Okay, HP= Hewlett Packard But 9000, 725/125, B132L ???? I am asking that question because I am about to buy one for myself, so I can have some fun... (3 Replies)
Discussion started by: Fwurm
3 Replies
Login or Register to Ask a Question