Thread: Moving from C to C++

  1. #16
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by esbo
    I am not stupid, I just don't like things being made overly
    complex or verbose?
    Why don't I like C++? Well because basically I don't need it.
    Other than // for comments it has no benefits for me, I don't
    need to reinvent the wheel because I already have round wheels.

    Yes C++ allows me to do things I don't need to do in the first
    place, but what is the point of that?
    What benefits does C++ give me that I actually want or need?
    perhaps if you took a little time to study the concepts behind C++, you'd understand that you gain a lot more than "// for comments"

    To take a simple example: let's say you have a function that adds 2 "things" together and prints the result.

    in C you have to write a function for every type you want to use
    Code:
    void AddAndPrint_Int(int arg1, int arg2)
    {
         printf("%d", arg1 + arg2);
    }
    
    void AddAndPrint_Floatfloat arg1, float arg2)
    {
         printf("%f", arg1 + arg2);
    }
    
    void AddAndPrint_String(const char* arg1, const char*arg2)
    {
         char buffer[1024]; /* hope have enough space here */
         strcpy(buffer, arg1); /* hope arg1 is null terminated */
         strcat(buffer, arg2); /* hope arg2 is null terminated */
         printf("%s", buffer);
    }
    
    struct Date
    {
        int Day;
        int Month;
        int Year;
    };
    
    void AddAndPrint_Date(const Date* arg1, const Date*arg2)
    {
         Date result = SomeDateAddingFunc(arg1, arg2);
         printf("%d/%d/%d", result);
    }
    if we decide we want to AddAndPrint any other type (complex numbers for instance) we need to write another AddAndPrint function.

    in C++:
    Code:
    template <typename T>
    void AddAndPrint(const T& arg1, const T& arg2)
    {
        std::cout << arg1 + arg2;
    }
    that code will work for ints, float, strings and any other type that supports op << and op +

    now tell me which is more verbose?
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  2. #17
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Firstly I would not want to add and print, I might want to add or print,
    but not both.
    A google on AddAndPrint returns 70 entries so it doesn't exist does it?
    You have to write that first and you left that out.
    It returns 1 entry from the UK, a lot of the 70 are not in English.

    a=b+c;
    printf("%d",a);
    printf("%c",a);
    printf("%f",a);
    printf(%s%s",b,c);

    The above covers most of your examples in 5 lines, and less characters than your
    example which omits the 90% of the code you will have to write to make your
    program work!!

    It's like me saying, in C, program to play mp3 file:-

    playthismp3file(mp3file);

    I think there is a bit more to it than that!!!

  3. #18
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    OK maybe I misunderstood that a bit, but as I say
    a=b+c;
    printf("%d",a);
    printf("%c",a);
    printf("%f",a);
    printf(%s%s",b,c)

    covers most of it. And basically you are going back to BASIC.
    I admit there may be some benefits in the compiler or the compiled
    prog doing some of the extra work for you (with a slow down in speed)
    but its all the classes, objects and other garbage I have a major problem with.

  4. #19
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I'm sure AddAndPrint was just an example; there's plenty of other reasons why templates can be useful. For another example, what if you want to have a list of objects? In C, you'd need to rewrite your list functions for each type you needed to store. In C++, you can do this:
    Code:
    std::list<std::string> stringList;
    std::list<int> intList;
    std::list<MyType> myTypeList;
    And then, when you're done with the list, you don't have to remember to call a cleanup function to release the memory you've allocated; the destructor gets called implicitly when the object goes out of scope.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #20
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by esbo
    Firstly I would not want to add and print, I might want to add or print,
    but not both.
    A google on AddAndPrint returns 70 entries so it doesn't exist does it?
    You have to write that first and you left that out.
    It returns 1 entry from the UK, a lot of the 70 are not in English.

    a=b+c;
    printf("%d",a);
    printf("%c",a);
    printf("%f",a);
    printf(%s%s",b,c);

    The above covers most of your examples in 5 lines, and less characters than your
    example which omits the 90% of the code you will have to write to make your
    program work!!

    It's like me saying, in C, program to play mp3 file:-

    playthismp3file(mp3file);

    I think there is a bit more to it than that!!!
    are you deliberatly misunderstanding? AddAndPrint was a simple example of a function, it's not a real world function!

    I admit there may be some benefits in the compiler or the compiled
    prog doing some of the extra work for you (with a slow down in speed) but its all the classes, objects and other garbage I have a major problem with.
    and what is your problem with it? in a lot of cases C++ "classes and garbage" can be FASTER than c!

    let's take the string class as an example. let's say we want to concatenate strings in a loop
    Code:
    /* cat.c */
    
    int main()
    {
        char buffer[1024]; /*again praying we have enough space*/
        int numStrings = 0;
        char loopString[20]; /* just a short string */
        int i =0;
        printf("please enter a string to repeat\n");
        scanf("%s", loopString); 
    
        /* have to initialise our buffer */
        memset(buffer, 0, 1024);
    
        /*hopefully it'll be less than 20 chars otherwise we
        a) crash, the best result or
        b) we get one of those lovey buffer overrun security holes that plague IE */
    
        printf("enter a number of times to loop\n");
        scanf("%d", &numStrings);
    
        for (; i < numStrings; i++)
        {
             strcat(buffer, loopString);
        }
    
        printf("%s", buffer);
    }
    now leaving aside the verbosity, insecurity and general buggyness of such code, let's analyse the performance characteristic of the loop.
    Code:
        for (; i < numStrings; i++)
        {
             strcat(buffer, loopString);
        }
    what exactly is happening here? strcat first searchs for buffer's null terminator (NT), so it loops from 0 to NT. then it copies from loopString. with each loop iteration buffer gets longer and strcat takes longer and longer to execute.

    now let's look at the C++ version
    Code:
    int main()
    {
        // don't need to initialise or worry about size  
        std::string buffer;
        std::string loopString;
        int numStrings = 0;
    
        cout << "please enter a string to repeat"\n;
        cin >> loopString;
    
        cout << "enter a number of times to loop\n";
        cin >> numStrings;
      
        for (int i = 0; i < numStrings; i++)
        {
             buffer += loopString;
        }
    
        cout << buffer;
    }
    it's cleaner, terser, more secure and easier to change.
    but again, let's analyse the performance characteristic of the main work, the loop.

    Code:
        for (int i = 0; i < numStrings; i++)
        {
             buffer += loopString;
        }
    because buffer is now an object, it internally knows it's size. so when we do += the code generated simply starts copying at the end of the string, there's no searching for the NT, so it's actually FASTER!!

    if you still don't understand, there's no hope for you.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  6. #21
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    There is nothing like a programming argument.

    C has much easier syntax and can do lots of things that C++ can, so I really don't see why esbo's preference is a problem for anyone. Moreover, you're judgement of C++ being such an improvement is a little misfounded. There isn't that much that's totally new.

    "C++: creating an octopus by nailing extra legs to a dog."

    >> if we decide we want to AddAndPrint any other type (complex numbers for instance) we need to write another AddAndPrint function.
    Imitating a template is possible: you can just create a function with void pointers in C.

    >> let's take the string class as an example. let's say we want to concatenate strings in a loop
    A fine choice, classes at their best! However, a C programmer probably would not take the risks you are suggesting, and do most of the work in file I/O.

    Not that I care or anything. ¬.¬ Don't totally bash C because it is a little dated. It still works very well.

  7. #22
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    C shines more in embedded development. I happen to work on motorola processor now and I was thinking how a switch to C++ would benefit us. It just doesnt at all, even after removing all the initial larger memory footprint problems. This board has 256k working memory, try putting all your objects that keep track of of their own state in there....

    C++ shines more in application development for bigger projects, but tbh, I'd use java or another more modern language there

  8. #23
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    >> C has much easier syntax and can do lots of things that C++ can, so I really don't see why esbo's preference is a problem for anyone.
    esbo didn't state a preference. He simply came on here trolling and made a stupid post that C++ is a waste of time and space. I've tried to educate him or at least get him to present a reasonable argument. I now see this is a total waste of time.

    >> Moreover, you're judgement of C++ being such an improvement is a little misfounded.
    I'm not saying C++ is perfect or even better than C in every environment (I actually work developing an emulated embedded platform in C++ that runs embedded C code). If you read my previous posts you'd see I agree that C++ has it's flaws and I actually use python for most tasks now.

    That said, if you come on this board and post that "C++ is a waste of time and space" without backing it up and if you can't see the benefits of C++ over C, you're an idiot. Most of the best embedded C programmers I know (and I know some top-notch guys with 20 years c programming behind them) end up emulating C++ features in C. Hell one of them even wrote virtual function tables to emulate polymorphism. You CAN do these things in C, but it's easier and cleaner in C++ so why not let the compiler do the work? that's what computers were invented for! esbo talks about not reinventing the wheel, and then goes out of his way to rewrite in code something C++ gives you for free!

    >> Imitating a template is possible: you can just create a function with void pointers in C.
    and that is exactly the kind of hacky unsafe code that gives C such a bad reputation.

    >> This board has 256k working memory, try putting all your objects that keep track of of their own state in there....
    as the monty python guys would say...
    luxury, our board has 96k! Of course C is the language for that environment, but that's an external constraint. Given the choice, most intelligent developers will use the highest level language possible for the job. If you don't, you're wasting your employers time and consequently, money and you should be fired.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  9. #24
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Is C++ the reason software upgrades run 10-100 times slower than the did prior
    to 'upgrade'?

  10. #25
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by esbo
    Is C++ the reason software upgrades run 10-100 times slower than the did prior
    to 'upgrade'?
    yes it is. you should stick to writing C. in fact, write assembly. This will magically make your programs run faster.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  11. #26
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by ChaosEngine
    >> C has much easier syntax and can do lots of things that C++ can, so I really don't see why esbo's preference is a problem for anyone.
    esbo didn't state a preference. He simply came on here trolling and made a stupid post that C++ is a waste of time and space. I've tried to educate him or at least get him to present a reasonable argument. I now see this is a total waste of time.

    >> Moreover, you're judgement of C++ being such an improvement is a little misfounded.
    I'm not saying C++ is perfect or even better than C in every environment (I actually work developing an emulated embedded platform in C++ that runs embedded C code). If you read my previous posts you'd see I agree that C++ has it's flaws and I actually use python for most tasks now.

    That said, if you come on this board and post that "C++ is a waste of time and space" without backing it up and if you can't see the benefits of C++ over C, you're an idiot. Most of the best embedded C programmers I know (and I know some top-notch guys with 20 years c programming behind them) end up emulating C++ features in C. Hell one of them even wrote virtual function tables to emulate polymorphism. You CAN do these things in C, but it's easier and cleaner in C++ so why not let the compiler do the work? that's what computers were invented for! esbo talks about not reinventing the wheel, and then goes out of his way to rewrite in code something C++ gives you for free!

    >> Imitating a template is possible: you can just create a function with void pointers in C.
    and that is exactly the kind of hacky unsafe code that gives C such a bad reputation.

    >> This board has 256k working memory, try putting all your objects that keep track of of their own state in there....
    as the monty python guys would say...
    luxury, our board has 96k! Of course C is the language for that environment, but that's an external constraint. Given the choice, most intelligent developers will use the highest level language possible for the job. If you don't, you're wasting your employers time and consequently, money and you should be fired.
    I backed up my my reasons for beleiving C++ is largely a waste of time and space, without being insulting, which I believe is
    against the rules here.

    You can read this supposedly 'joke' interview with C++'s
    creator.
    http://www-users.cs.york.ac.uk/susan/joke/cpp.htm

    A joke maybe, but many a true word is spoken in jest.
    And you can write absolute rubbish code in any language so
    even if it were true it would give C++ no advantage over C.

  12. #27
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Are you arguing that C++ is a waste of time and space because it is difficult to learn? What if you have already learned how to use C++'s extra features to your advantage? IMO, using C and adding the safeguards and flexibility that comes automatically with C++ would be a waste of time and space if you already knew how to utilize those tools in C++, I don't know how you could argue otherwise. So that would mean that C++ is a waste of time to learn, in your opinion.

    That is fine, of course, if you feel it is a waste of your time, but that doesn't mean it is a waste of time and space for everybody, or in the general case.

  13. #28
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    That article started this whole debacle? esbo, I think it's a safe wager that neither of us were alive in the 70's. A great deal of that article does not apply to us. C's a great language, but C++ is important now these days too. At least tolerate the language and the people who write it.

  14. #29
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Bzzzzzzzzt! "esbo" is clearly just trolling, save your typing. He/she probably also trolls christian forums quoting satanic worship material too

  15. #30
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    I don't think that anybody is here to argue that C is rubbish and
    C++ is fantastic - neither is true. The truth is, every language
    has its virtues and flaws - esbo gives out about the speed of C++
    programs - take a look at java and come back then!

    Seriously, the development of C++ has led to improvements in
    C, as ANSI attempts to make the two languages more in line
    with each other - i mean, strong type checking is bound to be
    intoduced into standard C eventually - i don't see anything
    wrong with that, do you?

    Don't dismiss something because it doesn't suit you - advice that
    applies in all facets of life, but not the easiest to implement. C++
    is almost a complete superset of C - you don't have to write
    code using the string class or the STL - you don't even have to
    use objects if you don't want to - C++ gives you a choice. What
    can be said of C in that context? If you want versatility of style,
    then C++ is the way to go.

    The joke article was most probably written by a C++ programmer.
    Stroustrup defends C++ staunchly - heck i would too because
    it is something to be proud of.

    As already mentioned, well written C++ programs can be just
    as efficient as C - if you have performance issues, perhaps
    you are using an outdated microsoft compiler, or have no concept
    of optimisations.

    The system of classes is pure genius in my opinion. If you
    disagree on the basis of performance, then that's fine, but
    really - what can you possibly find wrong with the idea of
    emulating real world objects? Just earlier on i was thinking about
    physics, and the importance of modelling a system in a
    mathematically accurate way - breaking it into well defined
    portions, and understanding how those portions affect each
    other? C++ approaches programming in that method. I can't
    think of any way to simplify it - grouping functions and data just
    makes sense to me - and clearly to a lot of other people too, C++
    is probably more popular than C on this site.

    That's my view on the subject, i don't try and force it down
    anyones throat, i just accept the two languages, good and bad,
    and be glad that i have a choice of methods to approach a
    programming task - C++ is by no means the be all and end all
    of the programming world, and thank God for that!

    Now one more thing - this thread has been rudely hijacked as
    a result of esbo's initial post - thats not very fair now is it? Just
    a thought.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  2. moving median function
    By supermeew in forum C Programming
    Replies: 0
    Last Post: 05-04-2006, 02:37 PM
  3. Replies: 4
    Last Post: 01-16-2006, 05:58 PM
  4. 3D moving
    By bluehead in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2005, 05:46 AM
  5. Simple program i cant get (dot moving)
    By Lupusk9 in forum C++ Programming
    Replies: 4
    Last Post: 09-14-2004, 08:04 PM