Thread: Moving from C to C++

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    3

    Moving from C to C++

    Hi! I'm new to these forums, so first of all, hello!

    I just finished taking two first year university courses introducing me to C (We used The Art and Science of C and Programming Abstractions in C by Eric Roberts). Anyway, I'd like to "upgrade" to C++ because it seems to be better organized and easier to manage. Is there an article somewhere that can show for me the C++ counterparts of most common C items? One thing that's already confused me is the whole cout and cin thing. They seem to be functions, but aren't structured like a function. I know how to use the basics of them, but don't really understand what they are. If anyone could help me out, that'd be great.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Location
    Taxachusetts
    Posts
    10
    Quote Originally Posted by tweeres04
    Hi! I'm new to these forums, so first of all, hello!

    I just finished taking two first year university courses introducing me to C (We used The Art and Science of C and Programming Abstractions in C by Eric Roberts). Anyway, I'd like to "upgrade" to C++ because it seems to be better organized and easier to manage. Is there an article somewhere that can show for me the C++ counterparts of most common C items? One thing that's already confused me is the whole cout and cin thing. They seem to be functions, but aren't structured like a function. I know how to use the basics of them, but don't really understand what they are. If anyone could help me out, that'd be great.
    The tutorials right here on cprogramming.com are pretty informative. This one helped me quite a bit, also:

    http://www.cplusplus.com/doc/tutorial/

  3. #3
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    I made the same move earlier this year because of college (did
    C last year and C++ was on the course for this year). I did a
    quick search on google and i came up with this, the start of
    which lists some of the basic differences.

    The main things, as i see it, are:

    C++ introduces a concept called object orientation. Simply put,
    this is the combination of attributes (variables) and methods
    (functions) which describe and represent an object. The easiest
    and quickest example is modelling a bank account: Attributes
    might be the account balance, account holders name, interest
    rate, and then methods, which are a way to interact with the
    object, such as making a lodgement, cashing a check etc. These
    objects involve data encapsulation - the idea that for an object,
    the only way to manipulate the attributes is via the methods.

    Basically, an object is like a black box - you shouldn't be able to
    get into it, you don't know how it does what it does, but that
    shouldn't stop you from using it. The advantages of this style of
    programming are numerous: It allows companies to design code
    that performs logical functions, code becomes more portable,
    code can be reused, extended, or shortened, and also,
    programmers have more control over how their code is used.

    to make a crude comparison that is intuitively accurate from what
    i have described, in C speak, an object can be thought of as
    having attributes grouped together in a struct, but only a certain
    collection of functions are allows modify that stuct - the stuct
    will be out of scope as far as all other functions are concerned.
    That is OOP in a nutshell.

    Features - C++ versus C

    C++ is a much more detailed language than C. As above, OOP is
    allowed, dynamic memory allocation is greatly simplified, strong
    type checking is built in - assignments of incompatible types
    produce compilation errors, simplified file I/O, References - a
    partial alternative to pointers with a far simpler syntax, improved
    string support using the string class (i never mentioned that the
    word class is used in C++ when declaring an object - so i did it
    here), support for generic programming using the Standard
    Template Library (a group of classes that act as "containers" for
    any data type - sheer genius), built in error handling mechanisms
    (exception handling it's called), namespacing (another part of
    breaking code into logical divisions) and last but not least,
    inheritance - classes can inherite attributes and functionality
    from other classes, and functions can be redesigned as necessary
    which allows for a substantial amout of code reuse on large
    projects.

    Well i said it was a detailed language! The last, probably most
    important feature is the fact that because C++ was developed
    from C, the C++ language is ALMOST a complete superset of C,
    with the exception of a few items that would be legal in C but
    illegal in C++, for example, due to the strong typechecking
    features. Also, C++ is not a pure object oriented programming
    language due to the fact that it maintains compatibility with C,
    where as Java is much more object oriented.

    One more feature which deserves a special mention is operator
    overloading. This is where an object is defined, and the
    programmer can design how those objects behave when they
    are combined with arithmetic operators such as +,-, and even
    []. The great thing about this is that the person using the object
    can make intuitively sensible code - a brief example is the
    string class. You are probably familiar strcat and strcpy, but using
    operator overloading, "adding" one string to another acts like
    strcat, and using the equals sign to assign the result to another
    is like strcpy.

    Well thats all i have for you, so to answer your question on cout
    and cin, well they stand for console out and console in - they
    are actually objects, and the syntax of writing to the screen
    using cout using the binary shift operator << is actually an
    implementation of the overloading i just spoke about, and
    intuitively respresents "shifting" text into the object, resulting in
    it being displayed on screen, or shifting a value in from the
    console into your variable.

    Man thats a long post, but i hope you find it interesting and
    motivating, and detailed enough for you as well. Good luck
    on entering the world of C++. I leave you with this link to the
    man who designed C++ - Bjarne Stroustrup

    http://public.research.att.com/~bs/C++.html
    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

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I would consider starting fresh with your C++ learning, and not basing it on what you learned in C. Even though C++ is a superset of C, they are very different languages in how they are best used. IMO, too many C++ programmers write C++ code as if they are writing C code with classes. You will learn more techniques and tools if you don't try to map what you know in C to what you know in C++.

    One of the few books that teach C++ techniques from the beginning is Accelerated C++. It is called accelerated for a reason, so if you don't want something that moves too fast, it might not be right for you, but it does start you off with modern C++ instead of C based techniques.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    too many C++ programmers write C++ code as if they are writing C code with classes.
    Yup -- I'm guilty of that

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    3
    Awesome, thanks for all of that, especially richie. A lot of help. Now at least I have some sort of idea of what needs to be learned. I'll also keep in mind that I should try to not program in C using classes.
    Thanks again.

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by Ancient Dragon
    Yup -- I'm guilty of that
    I'd like to say I'm guilty of that, but unfortunately, I write both C and C++ much more comparably to a smoldering pile of poo.
    Sent from my iPadŽ

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    OO programming isn't restricted to any particular programming language. I know this C guy who uses a myriad of function pointers in structs to simulate member functions

    OO is a concept. Even the converse is not true. You don't need OO to program in C++.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  9. #9
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by tweeres04
    Awesome, thanks for all of that, especially richie. A lot of help. Now at least I have some sort of idea of what needs to be learned. I'll also keep in mind that I should try to not program in C using classes.
    Thanks again.
    And forget everything you think you know about i/o.. dynamic memory.. strings... arrays... casting.. C++ Does it all quite differently... You're really starting a whole new language, all from scratch
    Last edited by Bench82; 05-02-2006 at 11:43 AM.

  10. #10
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    In my opinion C++ is a waste of time and space for all but
    a few applications.
    I have the 'inventors' book and found it pretty incomprehensible.
    C++ is to programming what bicycles are to fishes.

    Looks for the laughs out loud smilie and notices it is not there, a bit like writing code in C++!!

  11. #11
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    C++ is actually a "multi-paradigm" language. There are bascially 3 seperate paradigms within C++:
    • C: plain 'ol procedural programming
    • Object Oriented: Classes, inheritance, polymorphism, encapsulation, etc
    • <Generic> programming: templates, containers, the STL, boost and (omg teh n0es) template metaprogramming (if you don't know what this is, now is not the time to learn)


    most beginners start with C and move to a mix of C and OO (c with classes). As they progress they move more to OO and eventually to a mix of OO and generic programming. Advanced programmers know when to use all 3 aspects.
    "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?

  12. #12
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    "multi-paradigm"
    C++ is a language for salesmen.

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> In my opinion C++ is a waste of time and space for all but a few applications.
    Do you know any C++?

    >> I have the 'inventors' book and found it pretty incomprehensible.
    Which book is that? It is true that there are not a lot of good beginner C++ books available, although I'm not sure how well that correlates to C++ being inappropriate for programming.

  14. #14
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by esbo
    "multi-paradigm"
    C++ is a language for salesmen.
    ok I didn't respond to your first post 'cos I thought you were joking, but in the grown up world we sometimes use words longer than 5 letters. Yes, C++ is a "multi-paradigm" language, or in words you can understand, C++ allows you to do things in different ways.

    Honestly, if you're going to post on a C++ board that "C++ is a waste of time and space", you should try to have something that resembles a rational arguement to back it up.

    >> I have the 'inventors' book and found it pretty incomprehensible.
    Your own stupidity does not make C++ a bad language.

    >> C++ is to programming what bicycles are to fishes.
    wow, what an insightful comment! I'm never going to write another line of C++ due to your intelligent, reasoned argument. Oh, hang on, there wasn't one.

    C++ has its faults (many of which it inherited from C). It's not a perfect language and it's not suited to every task (these days I generally prefer Python for most programming tasks), but as a highly efficient real-world systems level language, nothing comes close.
    "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?

  15. #15
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by ChaosEngine
    ok I didn't respond to your first post 'cos I thought you were joking, but in the grown up world we sometimes use words longer than 5 letters. Yes, C++ is a "multi-paradigm" language, or in words you can understand, C++ allows you to do things in different ways.

    Honestly, if you're going to post on a C++ board that "C++ is a waste of time and space", you should try to have something that resembles a rational arguement to back it up.

    >> I have the 'inventors' book and found it pretty incomprehensible.
    Your own stupidity does not make C++ a bad language.

    >> C++ is to programming what bicycles are to fishes.
    wow, what an insightful comment! I'm never going to write another line of C++ due to your intelligent, reasoned argument. Oh, hang on, there wasn't one.

    C++ has its faults (many of which it inherited from C). It's not a perfect language and it's not suited to every task (these days I generally prefer Python for most programming tasks), but as a highly efficient real-world systems level language, nothing comes close.
    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?
    Thats the question. All it appears to do is make have to write
    lots of code and learn lots of 'paradigms' which serve no
    purpose whatsoever.

    At the end of the day all C++ appears to be is nothing wrapped
    up in big words and any arguement to say otherwise usually
    consists of another wrapper of even bigger words.
    So sorry I just don't get it.

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