A few questions about C++ now and ~10 years ago.

March 18th, 2010
  • Hello there. Most of you don't know who I am, but just to introduce my self:
    I started learning C++ about a week ago. I'm currently on "day 2" on "Sam's teach yourself C++ in 21 days". I know I'm slow, but that's one of the reasons I'm posting here right now! :)

    You see my issue is that a lot of the examples in the book (http://newdata.box.sk/bx/c/htm/ch01.htm) does NOT work when trying to compile the program with modern compilers (Turbo C++, Visual C++, Dev-C++).

    So I started reading a few newer guides and found out that I needed to type in a few other lines before starting to write the main function of the program.

    e.g 1, Sam's teach yourself (day 1):

    #include

    int main()
    {
    cout << "Hello World!n",
    return 0;
    }


    -This example "works", but the compiler suggests that I use the library iostream instead of iostream.h. I got confused but figured that they might have just updated the library, so after a few moments of reading about and finding no good info on this, I just used iostream. Problem solved (after a lot of time spent on trying to find out why this wasn't working 100% with iostream.h).

    e.g 2, Sam's teach yourself (day 2, just copying now instead):
    p.s: the numbers (e.g: 1:, 2:,) are just line numbers to help the reader and explain later how the lines work.

    1: // Listing 2.2 using cout
    2:
    3: #include
    4: int main()
    5: {
    6: cout << "Hello there.n";
    7: cout << "Here is 5: " << 5 << "n";
    8: cout << "The manipulator endl writes a new line to the screen." <<
    Âendl;
    9: cout << "Here is a very big number:t" << 70000 << endl;
    10: cout << "Here is the sum of 8 and 5:t" << 8+5 << endl;
    11: cout << "Here's a fraction:tt" << (float) 5/8 << endl;
    12: cout << "And a very very big number:t" << (double) 7000 * 7000 <<
    Âendl;
    13: cout << "Don't forget to replace Jesse Liberty with your name...n";
    14: cout << "Jesse Liberty is a C++ programmer!n";
    15: return 0;
    16: }

    -This did not work at all. I got errors here and there, can't exactly remember where but after getting frustrated I looked for other guides to find out how to write it.
    I found a swedish guide (I'm norwegian, so I understand most of it) and read the first four pages. Most of it was the same as Sam's guide but some important differences. Here, "iostream" was used as a library on the beginner programs, not "iostream.h".
    And a function called "using namespace std;" was also used. So I tried to apply this to the code and replace a few lines in the code, and eventually made it work. Here is my code:

    // Code starts here
    #include

    using namespace std;

    int main()
    {
    cout << "Hello there!" << endl;
    cout << "Here is 5: " << 5 << endl;
    cout << "The manipulator endl writes a new line to the screen" << endl;
    cout << "Here is a very big number: t" << 70000 << endl;
    cout << "Here is the sum of 8 and 5: t" << 8+5 << endl;
    cout << "Here is a fraction (5/8): t" << (float) 5/8 << endl;
    cout << "And a very big number: t" << (double) 7000 * 7000 << endl;
    cout << "Don't forget to replace Jesse Liberty with your name..." << endl;
    cout << "Andreas is a C++ programmer!" << endl;
    cin.get();
    return 0;
    }
    //Code ends here

    As you can see, I've replaced a few of the "n"'s with endl;. Simply because I couldn't get it to function with "n"'s everywhere.

    Can anyone explain to me why the examples in the "old" Sam's teach yourself guide won't compile correctly?

    Has the language changed from 1994 to 2007 (which I presume it has)?
    If yes: How has it changed? What's different?

    Can I still use Sam's teach yourself guide to learn C++?

    Thanks in advance, and sorry to make you all read all of this =)

    szi


  • In one month you might be able to get some very basic syntax and shallow understanding of the concepts, but no one really "learns" an entire language in 21 days.
    I couldn't agree more. Most people never learn an entire language, they only learn enough to be able to solve the problems they're looking to solve. There are very, very few people who can honestly say that they know a language inside-out. Take your time to learn, because understanding the fundamentals now will make it easier to get through the more complicated bits later on.

    That's why its called preprocessor, its executed before the program is ran.
    While this is true, it's a little misleading. It'd be clearer to say that the preprocessor parses your source code before it is compiled :)

    You can even embed assembly inside of your C/C++ programs like so..
    And in doing so you tie your source code to a single architecture - so unless it's really necessary, I'd recommend not doing it.

    Although its often difficult to write complex programs in assembly, there is a distinct benefit to understanding how processors really work.
    Totally agree. If you know and understand what's going on under the hood, you will write better code when using higher level languages.

    Good luck in your learning!
    OJ.


  • second.


    My brother has the Fifth edition which is already 2 years old. I would really update to a newer version.


  • OMG that's funny s..tuff! Let me guess. You have pasted the code with line numbers? :ne: :D :hugegrin: :thumb: :lol: :krazy: :thumb2: :cubs:

    erm, read the post. I'm not the stupid one here.


  • But yea the moral of the story is: Don't use horribly outdated books for programming.:thumb:


  • http://www.amazon.com/Barrons-Computer-Science-2007-2008-Examination/dp/0764134876/ref=pd_bbs_sr_1/102-8051703-4576124?ie=UTF8&s=books&qid=1177857763&sr=1-1

    This is probably the best Computer Science book I've ever read, starts with simple Java syntax and goes over all the data structures, polymorphism, and inheritance in a relatively easy structure.


  • No. :)

    Yes. :sigh:


  • I realize inline assembly takes away compatibility... But really, aren't all computers really x86/x86-64???

    No. :)


  • Thanks for your reply Templarian!

    What is VS C++?


  • 1: // Listing 2.2 using cout
    2:
    3: #include
    4: int main()
    5: {
    6: cout << "Hello there.n";
    ...
    14: cout << "Jesse Liberty is a C++ programmer!n";
    15: return 0;
    16: }

    -This did not work at all. I got errors here and there, can't exactly remember where but after getting frustrated I looked for other guides to find out how to write it.OMG that's funny s..tuff! Let me guess. You have pasted the code with line numbers? :ne: :D :hugegrin: :thumb: :lol: :krazy: :thumb2: :cubs:


  • shuts down the computer that the program is excecuted on. Windows only ofc.

    =D

    without warning:D


  • I realize inline assembly takes away compatibility... But really, aren't all computers really x86/x86-64???
    Inline assembly really takes away the good parts of C/C++. For embedded systems, you might want to use assembly for RTOS. But if for maximum compatibility, you really really really should use ansii C/C++ (is there an ansii C++?). Not all computers are x86/64. Certainly the ones running Windows are. But some processors behave differently. Usually you can fix those incompatibilities by just compiling your program; but if you write assembly, it might take some effort figuring what's not working.


  • Don't feel slow... The books only say 21 days / 21 hours / (probably one day it'll be Teach Yourself C++ in 21 seconds) because people hate to admit to themselves that learning computer science doesn't happen in a month.

    In one month you might be able to get some very basic syntax and shallow understanding of the concepts, but no one really "learns" an entire language in 21 days.

    There are a few things I think would be useful to point out:

    1. Preprocessor directives

    Any line that starts with # isn't done while the program is running, but rather its done while you're building your program. That's why its called preprocessor, its executed before the program is ran. Perhaps one of the most common preprocessor directives is #include "myHeader.h". All the include directive does is it copies the code in the file, and pastes it where the directive is.

    A few pieces of advice:

    1. Learn Assembly

    Assembly is the language your processor speaks. A compiler is a language that turns abstract code like if(n) { c++;} into assembly. In the end, all code is assembly. Although its often difficult to write complex programs in assembly, there is a distinct benefit to understanding how processors really work.

    But that's not the best part! You can even embed assembly inside of your C/C++ programs like so:

    int Main() {

    asm {
    push ecx
    Begin:
    add ecx, 10
    //whatever else
    JMP Begin
    pop ecx
    }

    }


  • [quote=Templarian;2119024]good you two agree. :fab:

    No :cool:


  • As I posted in another thread there have been more standards added to C++.

    http://www.we11er.co.uk/programming/basic-cpp.html

    This should give you a general gist of what your going to want to change in your coding style (its just minor changes your code will look generally the same).

    Most of your current things in that book should work though (just listen to what the compiler warns you about... mostly its to make sure you are using the standards.

    //edit, kinda skimmed your post didnt read the other code, the 2nd snip of code appears right. Your just going have to get use the to standard and learn what to change in the book, or go buy a more up to date book.

    also make sure your using VS C++.


  • Visual C++. (i just was thinking Visual Studio C++ when i was typing it). If you have questions feel free to ask if your software tells you to do something else (usually i will tell you how to correct it though).

    BTW, "std::" isnt needed becuase you used using namespace at the top. So if you see code with std::cout ect you wont be confused (although the book may have explained this).


  • What edition is your Sams C++ book.


  • ^lol. while its good to learn all the data types (int bool ect...), one does not need to learn everything about assembly (i++, ++i, ect...).

    That book barley scratches the surface also, it just shows how to do everything, it doesn't show how exactly to make what you want later on when you have to use it. Just take the book slow and set by step.


  • Oh, right. Currently using Dev-C++ but now that I've started to actually understand it all I think I'm going to go on using either Visual Studio or Borland's.

    Thanks a lot for the help Templarian, made my day :)

    (also your link made me understand that the string "sytstem("X");" command can screw my friends over a bit xD)

    I know it sounds stupid and childish but...
    // hehe
    #include

    using namespace std;

    int main()
    {
    cout << "BAAH!" << endl;
    system("SHUTDOWN -f");
    cin.get();
    return 0;
    }

    was really fun xD


  • What does that do?


  • Yes. :sigh:

    No. :)

    good you two agree. :fab:


    But yea the moral of the story is: Don't use horribly outdated books for programming.


  • What edition is your Sams C++ book.

    second.

    http://newdata.box.sk/bx/c/

    ot:
    thanks for all the replies people :)

    szi


  • I realize inline assembly takes away compatibility... But really, aren't all computers really x86/x86-64???


  • :thumb:

    :sure::?):?):pope::p::stare::stare::stare::stare:: stare::sen::sen::sen::sen::sen::sen::A+::A+::A+::c ool::cool::cool::cool::cool::cool:







  • #If you have any other info about this subject , Please add it free.#
    Your name:
    E-mail:
    Telphone:

    Your comments:


    If you have any other info about A few questions about C++ now and ~10 years ago. , Please add it free.