Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Language Styles

c++ languages styles programming syntax

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1 McHugh75

McHugh75

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 58 posts

Posted 02 July 2013 - 11:05 AM

Well, the naz*s over at StackOverflow blocked this question so I'm gonna try again.

So far in my programming career (1.5 years) I have simply been solving problems and the only thing that really changes for me is the syntax of the language. This has got me thinking, however, because I recently read Bjarne Stroustrup saying that C++ is unique and should not be used to write "Java-Style" code or "C-Style" code. What are these style differences that each language supposedly possesses and how should I take advantage of them? I am particularly interested in answers regarding C++ but general answers would suffice as well.

Thank you CodeCall, for not analyzing every single question submitted.


Edited by McHugh75, 02 July 2013 - 11:05 AM.


#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts

Posted 02 July 2013 - 12:05 PM

C-style code uses functions for everything, uses macros heavily, and makes OOP style coding difficult, at best.

 

Java-style code, by contrast, uses objects for everything, abstracts away all pointer concepts, and tends to have clunky support for generics.

 

C++ is a hybrid language, with support for both of those styles and more. But the result of that is that doing a "pure" Java-style means you're so focused on classes and objects that you can lose sight of when a global or simple function is appropriate. Similarly, there are many C++ constructs that make C-style code very dangerous, as it steers you away from encapsulating data where appropriate. In addition, Templates allow you to do a lot of things that are very difficult in either language.

 

Also, there are other families of languages that take very different approaches to programming in general. Lisp and Haskell both support functional programming with various levels of purity, which is not easily achieved in C++. Erlang takes a rather odd approach to exception handling, where it lets things crash and burn, and has separate objects that monitor those crashes and restarts them while logging what happened. Heck, JavaScript and Ruby have mutable object/class definitions, so that you can add new properties/methods to a class/object on the fly.

 

The features a language has will lend themselves to different programming styles, with some features being preferable to others in most circumstances, but not obvious if you are used to using a different language.


Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 0xDEADBEEF

0xDEADBEEF

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 790 posts

Posted 02 July 2013 - 01:36 PM

I suppose there is style to these things.

 

Basically C++ can be written like most 'C-like' languages, but shouldn't. The comparisons with C are easy, since C doesn't have classes you've often got a class like object decomposed into a struct & suite of functions, which alot of people will point to. I think though when comparing C++ & C you've got stronger type safety in C++; you don't need to pass around void* pointers as you can other techniques to stop that.

 

The comparisons with Java maybe are a little more subtle. You can probably point to single inheritance as a very Java thing, or deriving all your classes from a single base class. Over use of exceptions, using classes instead of function pointers, not using operator overloading when its appropriate, no using stack based allocation correctly.


Creating SEGFAULTs since 1995.


#4 McHugh75

McHugh75

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 58 posts

Posted 02 July 2013 - 03:04 PM

Well that's great to know. I thought I was really missing out on something but it seems that it is less the language itself but really what the language facilitates the use of that drives what it's 'style' is. Thanks Evan and WingedPanther. You guys make CodeCall great!

#5 0xFACEB004

0xFACEB004

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 625 posts

Posted 02 July 2013 - 06:39 PM

Well that's great to know. I thought I was really missing out on something but it seems that it is less the language itself but really what the language facilitates the use of that drives what it's 'style' is. Thanks Evan and WingedPanther. You guys make CodeCall great!

 

Thank you...I think that was great question, and those are great answers WingedPanther and Evan! These are the things that they haven't gone very deep into in the classes that I have had over the last 2.5+ years of school, but it makes perfect sense when you think about it from those perspectives.

 

And I completely agree that the quality of the experienced programmers, and especially the non-"1337 h4xor" attitude here on CodeCall makes this forum a great place to learn! Hopefully, someday I can contribute as much as I have learned here. Thanks again guys.


                                                                                                                                                                            FACEB00K Likes this.


#6 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts

Posted 03 July 2013 - 04:39 AM

You start to see some of these differences when you read books on more advanced concepts, or read books on how to learn a language that refer to "similar" languages a lot for pointers. As an example, I've got a book on OOP Design Patterns that looks at C++ and Smalltalk as the two primary languages for examples. There are several points were language features drive details of how to implement a concept in one language vs the other. I have a book on Java that regularly highlights key conceptual differences with C++ and C#. I have a couple of books on C++ that emphasize why a good practice in C is actually a bad practice in C++. Every time, it comes down to language features.

 

As an example: C++ doesn't have interfaces, but does have pure virtual classes and multiple inheritance. Java has single inheritance and interfaces. Most of the time, you will get similar effects, but C++ can do something Java can't: it can have a virtual class that is NOT pure: it defines 2/3 of the functions in terms of only a couple functions the user will have to define in a subclass. The Boost library uses this feature to provide semi-automated parent classes, where you only have to define a couple of operators, and the parent class defines the rest in terms of those. This simply isn't possible in Java, and allows for some interesting techniques in C++.


Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download