Why is C++ so much harder than Java?

  • C/C++
  • Thread starter uperkurk
  • Start date
  • Tags
    C++
In summary, C++ is much harder than Java. The lecturer provides headers after week 2, which is too accelerated. The syntax is confusing and pointers can be used similarly to arrays.
  • #1
uperkurk
167
0
I'm into my second year now at uni and after learning Java for the past year, the course is now introducing us to C++. We're still doing java but now C++ aswell.

I don't know if I'm the only one but C++ is sooooo much harder than java, today after just our second lesson we're being introduced to header files and coding our own. it took my 2 hours just to figure out how to right a program that read from a file, command line arguments and then added up the number of arguments and printed out the sum of those arguments.

The thing is our lecturer uploads the solutions to the problems 1 week after the problem was given to us, so I can always wait until the solution is provided and then try to piece together what is going on, but I just don't understand it.

Everything seems to be moving at such a fast past and I'm just slipping behind, I'm 1 week behind so far and tomorrow is the next exercise and I still can't complete last weeks one :/
 
Technology news on Phys.org
  • #2
Is there a question in here somewhere or did you just need to get that off your chest? :smile:
 
  • #3
Mainly just to get it off my chest lol. Also for someone to tell me that they were in my position and it's just a hurdle that you need to overcome and once you start doing more C++ you'll get used it it?

I don't know really =(
 
  • #4
uperkurk said:
It took me 2 hours just to figure out how to write a program that reads from a file, command line arguments and then added up the number of arguments and printed out the sum of those arguments.
Seems the main issue here is pointers, and in this case, a pointer to an array of pointers to the arguments, often declared as a pointer to a pointer to a character ( ... , char **argv ) or sometimes as an array of pointers to characters ( ... , char *argv[] ). The end result is basically the same, since C / C++ treats arrays and pointers similarly.
 
Last edited:
  • #5
Pointers can be non-intuitive to the Java programmer.

But headers already after week 2? That's a little too accelerated.
 
  • #6
Whovian said:
Pointers can be non-intuitive to the Java programmer.

This has always mystified me since Java has pointers too: Java references are, in fact, just pointers.

In my experience there are a couple of things in particular that seem to blow Java programmers minds when they come to programming in C++:

  • the lack of a garbage collector and the resultant necessity to engage in manual memory management for anything allocated on the heap;
  • the ability to store actual objects - not just pointers or references to objects - as members in other objects.

The first isn't actually a problem since the advent of smart pointers and RAII mean that nobody beyond library writers should really need to worry about memory management. The second is fixed simply with practice.
 
  • #7
Whovian said:
But headers already after week 2? That's a little too accelerated.

You can't write any C++ (or even C) programs without using the predefined headers, and you can't write any non-trivial code that is sensibly stuctured without writing your own headers.

If you already know Java, this probably isn't a first programming course that spends two weeks explaining what "i = i + 1" means. I would have thought you would be meeting headers within in the first two hours, not the first two weeks.
 
  • #8
coalquay404 said:
The first isn't actually a problem since the advent of smart pointers and RAII mean that nobody beyond library writers should really need to worry about memory management.
Of course, that doesn't stop some people from teaching/learning C++ from a 25-year old perspective on using the language.

Because of this, "I'm learning C++" can mean a very wide variety of things. :frown:
 
  • #9
Whovian said:
Pointers can be non-intuitive to the Java programmer.

coalquay404 said:
This has always mystified me since Java has pointers too: Java references are, in fact, just pointers.

I think the syntax is part of the issue, since it doesn't consistently flow left to right:

char achar[]; // array of characters
char * pchar; // pointer to character
char ** ppchar; // pointer to pointer to character
char * papchar[]; // pointer to array of pointers to characters

Then there's the issue that pointers can be used similar to arrays:

... *(pchar + 1) is the same as ... pchar[1]

The syntax for pointer to function isn't obvious either:

int (*pfun)(void); // ptr to function wtih no parameters and returning integer
 
  • #10
Hurkyl said:
Of course, that doesn't stop some people from teaching/learning C++ from a 25-year old perspective on using the language.

Because of this, "I'm learning C++" can mean a very wide variety of things. :frown:

Ah, the old days of programming when people said "I program in C++ because Java is too slow". I was just talking to a programmer who was saying "I program in Java because Ruby is too slow" :rofl:
 
  • #11
Yesterday I got lost in my own C++ code. I totally forgot WTF I was doing. So I hear ya... My two semesters of Java was a walk in the park compared to my first semester of C++!

But that means you're learning, too. :p
 
  • #12
rcgldr said:
I think the syntax is part of the issue, since it doesn't consistently flow left to right:

char achar[]; // array of characters
char * pchar; // pointer to character
char ** ppchar; // pointer to pointer to character
char * papchar[]; // pointer to array of pointers to characters

Then there's the issue that pointers can be used similar to arrays:

... *(pchar + 1) is the same as ... pchar[1]

The syntax for pointer to function isn't obvious either:

int (*pfun)(void); // ptr to function wtih no parameters and returning integer

Things like
Code:
[color=#B00040]char[/color] [color=#666666]*[/color]([color=#666666]*[/color]c[[color=#666666]10[/color]])([color=#B00040]int[/color] [color=#666666]**[/color]p);
get interesting...
 
  • #13
rcgldr said:
I think the syntax is part of the issue, since it doesn't consistently flow left to right:
That's because it doesn't flow left to right. For the most part, it flows right to left. In general it swings from center to right to left to right to left to ... You just have to get into the swing of things.

Code:
char const * foo;
// foo is a pointer to a const char.
// Simply read from right to left.

char * const bar;
// bar is a const pointer to a char.
// Once again, just read right to left.

int *(*baz(double red_herring))[];
// baz is a pointer to a function that takes a double and
// returns a pointer to an array of pointers to integers.
// Now we really do need to get into the swing of things.
// How to read this:
//   1. Find the identifier: It's baz. That "red_herring" is a red herring.
//      baz is ...
//   2. Read to the right: ... a function that takes a double.
//   3. Stop at the unbalanced close paren and read to the left: 
//       ... a pointer to a function that takes a double ...
//   4. Stop at the open paren, swing back to reading to the right:
//      ... that returns an array ...
//   5. Stop at the semicolon, swing back to reading to the left:
//       ... of pointers to integers.
// Easy-peasy! :)
 
Last edited:
  • #14
jhae2.718 said:
Things like
Code:
[color=#B00040]char[/color] [color=#666666]*[/color]([color=#666666]*[/color]c[[color=#666666]10[/color]])([color=#B00040]int[/color] [color=#666666]**[/color]p);
get interesting...
c is an array of 10 function pointers each of which takes a pointer to a pointer to an int as an argument and returns a pointer to a char.
 
  • #15
D H said:
Code:
char const * foo;
// foo is a pointer to a const char.
// Simply read from right to left.

Except this is also allowed:

Code:
const char * foo;
// foo is a pointer to a const char.

There's also two ways of accessing members via pointers to structures or classes:

(*pobject).member or pobject->member

Then C++ added another alternative with function parameters accessed by reference

using pointer:

Code:
    fun(&object);
// ...
void fun (OBJECT * pobject)
{
...  pobject->member  ...
}

using reference.

Code:
    fun(object);
// ...
void fun (OBJECT &object) // parameter is actually pointer to object
                          // allowing object to be modified by fun
{
...  object.member  ...
}

With reference method, you can't tell from the call if the function can modify object (pass by reference instead of pass by value), you have to look at the function or it's prototype, which could be located in a separate header file.

The classic mistake if(x = 1)... when you meant if(x == 1) ...

One of the cases where whitespace matters:

a+*ptr2b (a plus (*ptr2b))
a-*ptr2b (a minus (*ptr2b))
a**ptr2b (a times (*ptr2b))
a/*ptr2b (/* comments out all remaining code until */ if there is any)
 
Last edited:
  • #16
Here's a strange pointer-related "feature" of C++. Suppose we define a five-element array of ints, a, to store the integers from 0 to 4. Everyone knows that the following prints '4' to stdout:

Code:
std::cout << a[4] << std::endl;

If you want to mess with someone's head, the same result can be achieved using the following code:

Code:
std::cout << 4[a] << std::endl;

You're well on your way to understanding pointers and arrays if you can wrap your head around why this works.
 
Last edited:
  • #17
That's a misfeature, not a feature, and it's an inherited misfeature from C. It's due to the extremely weak support in C for arrays. Unfortunately, this is one of the misfeatures that makes C a bit poor for scientific computing.

All in my opinion, of course.
 
  • #18
coalquay404 said:
Code:
std::cout << 4[a] << std::endl;

Simple - here's the definition of that array.
Code:
 int 4[a] = {0, 1, 2, 3, 4};

:wink:
 
  • #19
No. It's worse than that, Mark. 4[a] and a[4] as an lvalue or rvalue refer to exactly the same thing. It's because C treats the operator [] as syntactic sugar for pointer addition and deferencing. a[4] is just a sugar coated version of *(a+4), and 4[a] is just a sugar coated version of *(4+a).
 
  • #20
If you have a copy of Expert C Programming: Deep C Secrets, the commutativity of the subscript operator is discussed some on p. 243.
 
  • #21
D H said:
In general it swings from center to right to left to right to left to ... You just have to get into the swing of things.
Code:
int *(*baz(double red_herring))[];
My favorite post in this thread.

Getting back to the OP, once you get past the terse syntax of C / C++ and perhaps the STL (standard template library) that your class may get into later on, you'll find the actual programming to be similar to what you would do with any programming language.
 
  • #22
C is a logical, if complex, language. Everyone complains about pointers, but once you get the concept, really it is just a simple idea that can be chained together into more complex things.

C++, on the other hand, is an unholy beast. It's a mashed-together conglomeration of both procedural and object-oriented ideas, and there are way too many fiddly details about the implementation of the language that must be memorized in order to write in it effectively. Seriously, what is the need for both references and pointers?

If speed is critical in your application, use plain C. If you like doing things object-oriented, use Java or some other high-level language.
 
  • #23
Ben Niehoff said:
... there are way too many fiddly details about the implementation of the language that must be memorized in order to write in it effectively.

Such as?

Ben Niehoff said:
Seriously, what is the need for both references and pointers?

There are several reasons why references are a good idea. Here's two off the top of my head:

  • For safety reasons, when using pointers you need to be obsessive about checking for nulls; references are guaranteed to always be non-null.
  • References give you a straightforward way of implementing operator overloading.
 
Last edited:
  • #24
I'd say that C++ is, well, a cover-all language, which tries to allow for a lot of paradigms and sub-paradigms. I will agree, it can get unnecessarily mashed-up at times, and the lack of memory management can be a little annoying.

All languages equal in their own right, C++ is my personal favourite, so I'll try to point out the good parts and gloss over the flaws, since that's what I do with stuff I like. Multiple Inheritance is confusing and rarely useful, but when it is useful, it's useful. The more common alternative, which uses interfaces, is still possible in C++. The closest to templates I've seen in another language is Objective-C's dynamic typing.

But, yea. It can be bulky and unwieldy.
 
  • #25
rcgldr said:
Getting back to the OP, once you get past the terse syntax of C / C++ and perhaps the STL (standard template library) that your class may get into later on, you'll find the actual programming to be similar to what you would do with any programming language.
I disagree. Every language has its own idioms, its peculiar way of doing things. One of the key obstacles to learning a new language is to learn how to write in the vernacular of that language as opposed to force-fitting the idioms of some other language onto the language at hand. An even bigger issue is that some languages are fundamentally very different from one another. A pure functional language is very, very different from a procedural language.


Ben Niehoff said:
C++, on the other hand, is an unholy beast. It's a mashed-together conglomeration of both procedural and object-oriented ideas
That's not quite fair. OO is always going to be on top of some other programming paradigm, be it procedural (Java, C++, ObjectiveC), functional (Haskell, CLOS), or logical (Logtalk).
Seriously, what is the need for both references and pointers?
Do C++ the modern, RAII way and you won't use very many pointers. The C++ standard library provides a number of mechanisms that in many cases eliminate the need for pointers. Needing a pointer is a bit of a code smell. Needing a void* pointer is worse than code smell. The ubiquity of void* in C versus its absence in well-written C++ is one of the key features that distinguish the two languages.


Whovian said:
I'd say that C++ is, well, a cover-all language, which tries to allow for a lot of paradigms and sub-paradigms.
That mish-mash just got bigger with C++11. C++03 functors started to give a bit of a functional programming flavor to the language. Lambda expressions and closures sealed the deal. The new standards makes C++ have aspects of procedural programming, object oriented programming, generic programming, meta programming, functional programming, and multiprocessing. All that's missing is logic programming.
and the lack of memory management can be a little annoying.
Smart pointers take care of a lot of those memory management problems, and do so without needing a garbage collector.
The closest to templates I've seen in another language is Objective-C's dynamic typing.
The closest thing is Ada generics. The same two people, David Musser and Alexander Stepanov, who developed Ada generics also strongly influenced how C++ templates work and developed the bulk of the STL. Interestingly, Stepanov is fervently anti-OO: "I think that object orientedness is almost as much of a hoax as Artificial Intelligence."

But, yea. It can be bulky and unwieldy.
I can't argue with that.
 
  • #26
rcgldr said:
Getting back to the OP, once you get past the terse syntax of C / C++ and perhaps the STL (standard template library) that your class may get into later on, you'll find the actual programming to be similar to what you would do with any programming language.

D H said:
I disagree. Every language has its own idioms, its peculiar way of doing things. One of the key obstacles to learning a new language is to learn how to write in the vernacular of that language as opposed to force-fitting the idioms of some other language onto the language at hand.

I was thinking along the lines of the simple tasks in a classroom environment, such as reading and writing files, sorting arrays, ... , the type of things he may have already done in Java . I'm not sure if the OP's class is going to involve the more advanced features of C++, and it's not like he's moving from Java to a declarative language or some obscure language like APL.
 
Last edited:
  • #27
Ben Niehoff said:
..C++, on the other hand, is an unholy beast...

If speed is critical in your application, use plain C. If you like doing things object-oriented, use Java or some other high-level language.

I'm tempted to agree with you w.r.t. C++, but it does have its uses. But Java? Not even once!

C was my first language, and is probably my favorite. Python and Go are also near the top for me, and MATLAB is a necessary evil.

Begun, the language war has.
 

1. Why is C++ considered more difficult than Java?

There are a few reasons why C++ is generally considered more challenging than Java. Firstly, C++ is a lower-level language, meaning that it is closer to the computer's hardware and requires a deeper understanding of computer architecture. Additionally, C++ is a more complex language, with a larger feature set and more intricate syntax. Finally, C++ gives the programmer more control over memory management, which can be difficult to master.

2. Is C++ harder to learn than Java?

It depends on the individual's learning style and prior experience. For someone with a strong background in programming and a solid understanding of computer architecture, C++ may not be significantly more difficult to learn than Java. However, for a beginner, Java may be a more accessible language due to its simpler syntax and automatic memory management.

3. Are there any benefits to learning C++ over Java?

Yes, there are several benefits to learning C++ over Java. Firstly, C++ is often used for high-performance applications, such as game development and operating systems. Additionally, C++ gives the programmer more control over memory management, allowing for more efficient use of resources. Furthermore, C++ is a widely used language in the industry, so learning it can open up more job opportunities.

4. Can a Java programmer easily switch to C++?

It may not be a seamless transition, but a Java programmer can certainly learn C++. While the syntax and features of the two languages may differ, the fundamental concepts of programming remain the same. It may take some time and effort to learn the nuances of C++, but a Java programmer already has a strong foundation in programming that can be applied to learning C++.

5. Are there any tips for making learning C++ easier?

One helpful tip for learning C++ is to start with a solid understanding of basic programming concepts, such as data types, control structures, and functions. It may also be beneficial to practice writing code and solving problems in C++ on a regular basis. Additionally, seeking out resources such as tutorials, online courses, and coding communities can provide valuable support and guidance in learning the language.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
2
Views
373
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
20
Views
3K
  • Programming and Computer Science
Replies
21
Views
12K
  • Science and Math Textbooks
Replies
1
Views
923
  • Programming and Computer Science
Replies
21
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • STEM Career Guidance
Replies
10
Views
1K
Back
Top