The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
ksh: difference between $* and $@ JamesByars Shell Programming and Scripting 1 12-30-2007 10:08 AM
Difference between $* and $@ saneeshjose Shell Programming and Scripting 1 01-19-2006 08:03 AM
What is the difference thunderfastfox UNIX for Dummies Questions & Answers 1 12-07-2005 11:22 PM
difference rajashekaran UNIX for Advanced & Expert Users 1 04-23-2002 01:59 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-24-2005
hytechpro hytechpro is offline
Registered User
  
 

Join Date: Sep 2005
Posts: 3
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 (permalink)  
Old 11-25-2005
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
Check google. Some tips:
1
2
  #3 (permalink)  
Old 11-29-2005
Corona688 Corona688 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 1,928
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..
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:38 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0