Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Jumping straight into C++ without programming knowledge

  1. Dec 11, 2012 #1

    I was looking at an Introduction to C++ class I might be taking next semester

    This course is an introduction to the basic principles of programming using
    C++ as the development tool. Topics include the structure and design of algorithms, input/output,
    branching structures, functions, recursion, built-in data types, arrays, structures, les, pointers
    and elementary operations on linked structures. The object-oriented programming paradigm will
    be introduced. Topics include encapsulation, polymorphism, libraries, stream, inheritance and
    abstract data types. Students will design algorithms, write external and internal documentation
    and design and write source code in C++.

    As someone with zero knowledge about any programming language, I was curious if it would be ok to start off with C++ without learning one of the easier languages, or will it be a huge struggle? I was going to try to get a head start this winter break and teach myself how to do stuff using the tutorials on this forum.
  2. jcsd
  3. Dec 11, 2012 #2
    No problem starting with C++, I did the same thing. Personally I've found the best ways to learn a language are to modify existing code to do something you want it to do, or to write an actual program you want to write from scratch. I never really learned anything beyond how to transcribe stuff when following along with the example programs for beginners.

    C# or Java may be easier to learn and develop with, and in the long run may be more marketable as well, if that is a concern.
  4. Dec 11, 2012 #3


    Staff: Mentor

    Following the java theme, there's groovy a scripting language that is relatively easy to use and that is a superset of java and even interoperates with java quite well. You can start out just writing scripts and then progress into full object oriented classes and programs. Similarly there is Scala, arguably known as a better Java than Java with many advanced programming features that is pushing the new edge of language design.

    As an aside:

    C++ is more difficult than java when you get into pointers and memory allocation issues. The base language C was one of my favorite languages but you can easily shoot yourself in the foot and even cause your machine to crash (PC DOS).

    C++ uses multiple inheritance which has some issues whereas Java uses a single inheritance model with interfaces that is easier to understand and use with less side-effects.

    In any event whatever language you choose, you should invest some time in learning how to use Eclipse or Netbeans. I use Netbeans primarily because it has all the plugins I tend to use as part of the loaded distribution (plugins for Java and C++ development). Eclipse is also a great choice although you may have to scout around and install some of the more arcane plugins. Lastly, there's BlueJ which is a kind of Eclipse for beginning programmers with just enough features to write java applications.
  5. Dec 11, 2012 #4
    To be clear if you stick to C++ you don't really have these problems. When you start mixing C and C++, usually when writing C++ with a C background, you do. There are a lot of things in C++ that are really a pain to use compared to other languages, but if you're coming from a clean background, I think you'll pick it up faster since you don't know what you're missing. ;)

    That said I think learning C is great too. If you don't understand some of the things the compiler is doing for you, when it starts behaving weirdly you may never understand why.
  6. Dec 11, 2012 #5
    So to start should I just read the tutorials on the top of the forum page? The most programming I did was to have ''Hello World'' typed in the black box
  7. Dec 12, 2012 #6


    Staff: Mentor

    I disagree. The problems that jedishrfu described don't come about because of mixing C++ with C - they can appear if you are using pointers incorrectly or are allocating heap memory on the heap that you forget to deallocate when you're done.

    If you do very simple programs, then you might not run into these problems, although pointer problems can crop up when you use C standard library I/O. Many of the functions, such as scanf and its variants, can offer some surprises to those who don't understand pointers.
  8. Dec 12, 2012 #7
    I noticed the tutorial guides on this page were written back in 2004...is it the case in the world of programming that the stuff that is presented in there is out of date and using other programs in lieu of what they suggest, etc?
  9. Dec 12, 2012 #8
    You can get by very well coding in C++ without ever touching a pointer or using any stdlib I/O from C is my point. In C++ you don't use scanf, you use the stream operators and related functions. e.g. this:
    Code (Text):

    int n;

    scanf("%d", &n):
    Code (Text):

    int n;
    cin >> n;
    You rarely have to touch pointers or any of the C standard library when writing pure c++.
  10. Dec 12, 2012 #9


    Staff: Mentor

    Real world C++ programs do use pointers and dynamic memory allocation as a fundamental part of their operation whereas in Java you are protected from these issues at the cost of Java garbage collection sometimes interfering with program flow. You can't always get away with avoiding pointers in C++.

    Another problem with C++ arises from multiple inheritance where say two classes A and B inherit from a third class C the variable x. Now in your program you need to write a new class D that inherits from A and B so when you refer to x which x do you get the D.B.A.x or the D.C.A.x they are both distinct and different. This situation doesn't arise in Java.

    Don't get me wrong both languages are widely used and programmers need to know both but in general Java is a safer language to use. Its just harder to shoot yourself in the foot with it but I've found ways of doing that.
  11. Dec 12, 2012 #10

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    Would you please stop trying to turn this thread into yet another one of those computer languages religious war threads?

    Regarding your comments, you are wrong. You can write modern C++ in which the C++ keyword delete is verboten; you have to get a waiver from the powers that be to use it. The C++ standard library provides a number of tools that go a long way toward eliminating the need for delete. With regard to multiple inheritance, there's no problem with multiple inheritance if you don't use it. There's nothing in the language that forces to use multiple inheritance. When you do use it, there are ways to solve the dreaded diamond pattern. Virtual inheritance, for instance. Multiple inheritance is a very useful capability when you need it. Java doesn't have it. IMO, that's a flaw, not a feature.

    Back on topic, there's nothing wrong with using C++ as the basis for an introductory computer science class. The goal of any introduction to programming is to teach students how to program. Many of those key concepts are common to C, Fortran, Java, C++, and a host of other languages. Simple I/O. Primitive types. Variables. Looping and conditional statements. Functions. Simple computer science structures such as linked lists. What C++ (and Java and C#) add to the basic procedural oriented programming paradigm is the object oriented programming paradigm. Just touching on that will take a good deal of time. There is no reason to go into multiple inheritance in an introductory class. Or templates, or metaprogramming, or functors, for that matter. An introductory programming class that uses C++ should not cover everything the language has to offer.
  12. Dec 12, 2012 #11


    Staff: Mentor

    Sorry, I was responding to an earlier comment about C++ and intended to stop at this juncture.

    I use these and several other languages on a daily basis for quite a long time and so have some familiarity with them and wanted to set the record straight.

    If someone asked me to choose I would say Java is better for OO introductory courses.
  13. Dec 12, 2012 #12
    I'm not saying that you *can't* use them, but that you do not have to, not even in "real world" programs. I can't think of a case off the top of my head where your only choice in some situation in C++ is to use a explicit pointer, beyond truisms like calling a stdlib function that requires a pointer, or otherwise hooking into C code. Often times it's easier to use pointers, especially for people coming from a C background, but you can certainly write useful programs in C++ completely devoid of any explicit pointers.

    Note I'm not talking about references here, which people often conflate with pointers. The difference can be subtle but is not unimportant, especially in the context of a beginning programmer.
  14. Dec 13, 2012 #13
    ok, well can I use the tutorials on this forum or are they out of date? You guys are arguing over something I don't even understand
  15. Dec 13, 2012 #14
    Just out of interest, what do you do when you need to allocate an object on the heap?
  16. Dec 13, 2012 #15
    Call the constructor for the class. I'm not sure if you're trying to ask something more specific.
  17. Dec 13, 2012 #16
    We're done arguing. :)

    You can use the tutorials. Language tutorials don't really have a shelf life, as long as the links still work that is.
  18. Dec 13, 2012 #17
    I'm asking how you handle creation of objects at runtime without using pointers.
  19. Dec 13, 2012 #18
    Heap objects are instantiated automatically for you by declaration so you don't have to get a pointer to one and then call new, you can just start using it, e.g.

    Code (Text):

    int main()
      CSomething foo;
      return 0;

    And of course you can pass in parameters to the constructor.
    Code (Text):

    int main()
      CSomething foo(1,2,3,4);
      return 0;
    This last bit is called RAII is a central concept in C++, though due to its roots in C, you're free to circumvent it at any time by using a pointer and calling new().
  20. Dec 13, 2012 #19


    User Avatar
    Homework Helper

    The real issue here is not the langauge (C++) but instead the class itself and the progression from the simplest of programs to more complex ones as students progress through the class, and how well the instructor and the textbooks explain what is going on (how these programs work).

    One issue is some students will want to know how a computer actually works internally, rather than just accept how everything works from a language (like C++) perspective. Those students may later decide to learn the basics of assembly programming, but I don't think this requires taking a full semester class on assembly programming unless there's really an interest in this (and if the school even offers such a class).
  21. Dec 14, 2012 #20

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    You meant the stack, not the heap. The stack is used for passing parameters, returning results, and for local variables. The heap is used for memory that is allocated dynamically (new/delete in C++, malloc/free in C).

    Strictly speaking, there is no stack or heap in C++, or in C for that matter. The stack and heap are how many, but not all, operating systems model memory.

    C and C++ have three types of storage:
    • Automatic storage. Your CSomething foo is an automatic variable. In most systems, these live on the stack.
    • Dynamic storage. Objects allocated with new and deallocated with delete have dynamic storage. In most systems, these dynamic objects live on the heap.
    • Static storage. Objects defined at file scope, static class members, and static local variables have static scope. The memory for these objects is created by the compiler; it's part of the executable. Non-primitive static objects (instances of some class or union) are constructed before main starts and destructed after main finishes.

    RAII is a very important concept. The acronym stands for the rather meaningless "resource allocation is initialization." Put simply, it means "Put away your toys. All of them." Allocated memory is but one of many resources that need to be "put away". Connections to I/O devices, connections to other computers, other processes also need to be "put away" when the thing that created those connections is done playing with them.

    The RAII concept addresses all resources, not just allocated memory. A class that allocates resources for an instance of that class should ensure that those resources are properly "put away" when that class instance goes out of scope. This places a heavier development burden on the author of a class but significant reduces the burden on a user of that class. RAII goes a long ways to solving the memory leak problem, along with a host of other resource-related problems as well.
  22. Dec 14, 2012 #21
    You're right of course D_H, my flub on heap/stack. I was trying to not get too deep into just what RAII means in order to answer the question.
  23. Dec 14, 2012 #22


    User Avatar
    Homework Helper

    What about _alloca() (dynamic allocation from stack, that is freed when a function returns to higher level)?

    I'm wondering if we're getting off topic from the original post which was asking if an introduction to programming class using C++ is a good idea. I think once the student gets into this class, either these aspects of programming will be explained, or the OP can ask questions here.
  24. Dec 14, 2012 #23

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    alloca is a non-standard function. It is not part of the C or C++ standard. It's not POSIX, While it is available on many machines, several discourage its use.

    There is zero need for alloca in C++. Use std::auto_ptr (C++03), boost::scoped_ptr (Boost), or std::unique_ptr (C++11) instead.

    Perhaps. The original question was answered quite readily (no problem), then we got dragged into the inevitable language holy wars, and now we're getting down in the technical weeds.
  25. Dec 14, 2012 #24
    For my part, I did not mean to start a 'holy war'. I use a lot of languages for different purposes, there aren't many I don't like or can't find a good use for from time to time. I just intended that you can (and people often do) write useful C++ code without ever touching an implicit pointer. I don't think that pointers are "bad" by any stretch of the imagination or even that you shouldn't use them in C++ when they are the best option, just that you don't ever really *need* to.
  26. Dec 14, 2012 #25


    User Avatar
    Science Advisor
    Homework Helper

    To get back to the OP's question, the key thing is the structure of the course, not the language it uses.

    What you should be learning on a first course IMO is programming. To do any practical work, you have to use some computer language, but it doesn't really matter what language it is.

    On the other hand, a lot of books, courses, and web tutorials don't teach programming, they teach a programming language. That's fine if you already know how to program and just need to learn another language, but it's a bad way to start.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook