Question

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
nymus
DC Developer
DC Developer
Posts: 955
Joined: Tue Feb 11, 2003 4:12 pm
Location: In a Dream
Has liked: 0
Been liked: 1 time

Post by nymus » Fri Oct 29, 2004 5:35 pm

You can't program everything in oop. C++ isn't all OOP. The program you wrote can be written in c++:

Code: Select all

int main()
{
        std::cout << "hello world";
        return 0;
}
If you wanted to program in c-style function-data (which is unavoidable at low-level) c++ is a better tool because of type checking, function overloading, enums, constants, inline functions, templates and namespaces.

Once you start writing drivers, oop comes into play and once you stat writing games you need all of C++.

C++ can do everything C can do better and it's backward to tell people to learn C instead of C++. If you want them to learn C, tell them to learn C. Don't tell them to learn "C instead of C++" because C++ can do C better than C can do C especially from a design perspective.
behold the mind
inspired by Dreamcast
User avatar
Shapyi
DCEmu Freak
DCEmu Freak
Posts: 86
Joined: Wed Oct 17, 2001 7:44 pm
Location: New Jersey
Has liked: 0
Been liked: 0

Post by Shapyi » Fri Oct 29, 2004 6:40 pm

segasweet, this is your topic so I want to offer you some more help. Here is a site I have come to love during those frustrating lab classes last semester when I was learning STL in C++ and I forgot my book. Here: http://www.cppreference.com/
Its not really a tutorial site, it basically lists all the keywords in C and C++, what they do, etc. It has a nice quick reference list for a lot of the C functions and the C++ functions. It also provides a list and functionality of each of the templated container classes in the Standard Template Library (STL). I don't know if it has been ported to Dreamcast, but if you ever are taking a course and are required to know that stuff there you go.

I guess whether you prefer C or C++ depends on the scale of the project, what you are trying to accomplish and which will delieve the simplest code with the quickest results. Because most of programming in computer science really isn't about solving a problem, its about solving a problem using the best possible case in terms of execution time, memory usuage, cpu usuage, etc. Sometimes you need to use C++ to get more structured code. Sometimes using C may improve the speed of a program with a little extra coding.

It really doesn't matter if you learn C or C++, you can learn the other if you know one of them fairly easily.

also I would do

Code: Select all

#include <iostream>

int main()
{
     std::cout << "Hello World!" << std::endl;

     return 0;
}
But thats just me.
User avatar
Quzar
Dream Coder
Dream Coder
Posts: 7486
Joined: Wed Jul 31, 2002 12:14 am
Location: Miami, FL
Has liked: 0
Been liked: 3 times
Contact:

Post by Quzar » Fri Oct 29, 2004 6:44 pm

The thing is that if you learn C++ then are constrained to the C subset, you will be severely crippeled in what you can do if you learned everything as oop and C++ dependant things like what you listed.

Learning C first then C++ is a more appropriate way to do it because then you can recognize the subset, and if you do things like you would in C with C++ there is nothing wrong with it, but going the other way just doesnt work.

Code: Select all

for(int i = 0; i<j; i++)
something as simple as that, if you start out with C++ becomes habit, but if you then try to learn C, you end up with syntax errors and you dont know why =P.
"When you post fewer lines of text than your signature, consider not posting at all." - A Wise Man
User avatar
Shapyi
DCEmu Freak
DCEmu Freak
Posts: 86
Joined: Wed Oct 17, 2001 7:44 pm
Location: New Jersey
Has liked: 0
Been liked: 0

Post by Shapyi » Fri Oct 29, 2004 6:58 pm

Thats pretty trivial quzar, isnt it? Sure I made that mistake when I first had to use C in one of my classes but it took me 2 seconds to figure out you aren't allowed to do that in C.
User avatar
Quzar
Dream Coder
Dream Coder
Posts: 7486
Joined: Wed Jul 31, 2002 12:14 am
Location: Miami, FL
Has liked: 0
Been liked: 3 times
Contact:

Post by Quzar » Fri Oct 29, 2004 7:01 pm

Shapyi wrote:Thats pretty trivial quzar, isnt it? Sure I made that mistake when I first had to use C in one of my classes but it took me 2 seconds to figure out you aren't allowed to do that in C.
it was a trivial example but if you take almost any program out there in C++ and want to convert it to C the task would be very difficult, showing that there is a large difference both in the syntax and style.
"When you post fewer lines of text than your signature, consider not posting at all." - A Wise Man
User avatar
Shapyi
DCEmu Freak
DCEmu Freak
Posts: 86
Joined: Wed Oct 17, 2001 7:44 pm
Location: New Jersey
Has liked: 0
Been liked: 0

Post by Shapyi » Fri Oct 29, 2004 7:24 pm

quzar wrote:it was a trivial example but if you take almost any program out there in C++ and want to convert it to C the task would be very difficult, showing that there is a large difference both in the syntax and style.
Well of course, they are two different languages. A lot of huge features of C++ can't be implemented in C, stepping down from C++ to C really is a big headache. There is a lot in C++ you can't do very easily in C. For example, template functions.

Code: Select all

template <class T>
T add( const T & x,  const T & y)
{
    return x+y;
}
You can't do that with C. And that will work for any class that has an overloaded + operator. Double, int, string, whatever. I made them constant reference because if you pass two huge strings you don't want to make two copies of them in memory.
BlackAura
DC Developer
DC Developer
Posts: 9951
Joined: Sun Dec 30, 2001 9:02 am
Has liked: 0
Been liked: 0

Post by BlackAura » Fri Oct 29, 2004 10:11 pm

Would you mind explaining in a little more detail why you prefer C to C++?
Simple. C++ is too much like C.

C was designed as a low-level systems programming language. Essentially, a higher-level assembly language, intended for writing operating system kernels and low-level systems software. Most of it's features are designed with that in mind.

C++ is simply an extension of C, which attempts to add all kinds of higher level abstractions on top of C.

That's basically the problem. C++ inherits all the problems of C, and then compounds them with all the additional features added, almost all of which exhibit problems associated with C. Things like having to manually manage memory, an inability to cleanly separate and interface from an implementation, dealing with the differences between an object, a pointer and a reference... These are exactly the same kinds of things you have to deal with if you're using C. The difference is that C++ is a far more complex language, and thus working around all those little problems is far more complicated. Such problems simply do not exist in higher level languages (although they do have their own set of problems), and having to deal with them whilst working with high level abstractions seems... stupid.
quarn
DC Developer
DC Developer
Posts: 80
Joined: Wed Oct 17, 2001 7:44 pm
Location: Sweden
Has liked: 0
Been liked: 0

Post by quarn » Sat Oct 30, 2004 3:42 am

quzar, ofcourse one shouldn't create lots of objects just because you want to use OOP. If you program is as small as that you can fit it all in the main-function in either C or C++. If you are reading at a university or equal, I would suggest that you take a course about code design and design patterns.

Btw, I don't see your point about the "Hello world!" program either. Someone showed you an example of how it looks in c++ and you see that they aren't that different. So one can't say that one of them is better than the other. Also, std::cout is an object... an ostream derivate I belive, with the <<-operator overloaded.
In commercial applications, OOP is pretty much the only way to go (unless of course you are the only coder working with it) because it pretty much forces a fixed standard of coding on you and allows for multiple people to work on a project without having seen all the code (easily).
Yes, and that's the reason for why I use OOP. Even though I work alone at a lot of projects I still want to keep the door open for other people to join in easily and help me out without them having to know all the code. And also the subsystem class dividing makes it easier to write good code that you can reuse in other projects. Maybe you can easily lift out a resource manager that takes care of all the resource loading or the input subsystem and use in different projects. Since all subsystems are always written with high cohesion and low coupling in mind, they shouldn't need any components from another subsystem to get working. I believe that this kind of structuring is easier to manage/extend/reuse with OOP than without it.

About getting extra speed from using C. You have probably heard of the term "90% of the time is spent in 10% of the code". So write that 10% in assembly and you are set ;). But if you can do a high level optimization (using better algorithms), it will probably give you more speed than these low level optimizations.

BlackAura, So you use C because C++ is too much like it that it wouldn't really matter which you use?
The difference is that C++ is a far more complex language, and thus working around all those little problems is far more complicated.
In what way? Didn't you just say that C++ handles it the same way as C?

I agree that C#, Java etc. does a better job than C++ in terms of making it easy to write complex programs. However, the problem is that these high level languages aren't compiled into machine code. They aren't made with the acctual machine in mind.

C was made with assembly in mind. C++ was made with C in mind. You wanted to be able to have total control over the machine while still being able to use the new object oriented way of thinking. C++ is like a compromiss to join in the best features of both low level and high level :)
User avatar
Quzar
Dream Coder
Dream Coder
Posts: 7486
Joined: Wed Jul 31, 2002 12:14 am
Location: Miami, FL
Has liked: 0
Been liked: 3 times
Contact:

Post by Quzar » Sat Oct 30, 2004 10:31 am

It seems that you are not open to any sort of argument however reasonable for using C over C++. So I'm not even gonna try anymore.
"When you post fewer lines of text than your signature, consider not posting at all." - A Wise Man
quarn
DC Developer
DC Developer
Posts: 80
Joined: Wed Oct 17, 2001 7:44 pm
Location: Sweden
Has liked: 0
Been liked: 0

Post by quarn » Sat Oct 30, 2004 11:10 am

quzar wrote:It seems that you are not open to any sort of argument however reasonable for using C over C++. So I'm not even gonna try anymore.
Tell me again, what was your argument? That one should use printf instead of cout? That's not an argument, that's a matter of personal preference. I thought we agreed on that..

Maybe I've missed your argument though..

Anyway, here is an interesting read.
User avatar
Shapyi
DCEmu Freak
DCEmu Freak
Posts: 86
Joined: Wed Oct 17, 2001 7:44 pm
Location: New Jersey
Has liked: 0
Been liked: 0

Post by Shapyi » Sat Oct 30, 2004 11:40 am

quzar wrote:It seems that you are not open to any sort of argument however reasonable for using C over C++. So I'm not even gonna try anymore.
If that could be said about quarn, the same could be said about you. After reading all this you don't seem like you want to give C++ a chance. You seem to be constantly arguing that C is better then C++.

I have one question. I always thought C# was a Microsoft thing. Can someone explain alittle about it o me? I have Visual C# that came with Visual Studio .NET. I never used it.

EDIT: Just one more thing to add. All of you are experienced programmers in my opinion. I think this argument is rather crazy since all experienced programmers find their language, their developement enviroment and stick with it because thats what has worked for them. Just a thought though.
User avatar
Quzar
Dream Coder
Dream Coder
Posts: 7486
Joined: Wed Jul 31, 2002 12:14 am
Location: Miami, FL
Has liked: 0
Been liked: 3 times
Contact:

Post by Quzar » Sat Oct 30, 2004 11:50 am

Shapyi wrote:
quzar wrote:It seems that you are not open to any sort of argument however reasonable for using C over C++. So I'm not even gonna try anymore.
If that could be said about quarn, the same could be said about you. After reading all this you don't seem like you want to give C++ a chance. You seem to be constantly arguing that C is better then C++.
I said that OOP (C++ or java) is better for larger projects and C for smaller. quarn says that C++ is better for everything and that no matter the size its better to do it in C++. My argument was that to a certain point of size and complexity there is no benefit or it may make it more difficult to attempt to use OOP design.
"When you post fewer lines of text than your signature, consider not posting at all." - A Wise Man
Kamjin
DC Developer
DC Developer
Posts: 216
Joined: Wed Dec 17, 2003 5:27 am
Has liked: 0
Been liked: 0

Post by Kamjin » Sat Oct 30, 2004 12:31 pm

quarn wrote: I believe that this kind of structuring is easier to manage/extend/reuse with OOP than without it.

C was made with assembly in mind. C++ was made with C in mind. You wanted to be able to have total control over the machine while still being able to use the new object oriented way of thinking. C++ is like a compromiss to join in the best features of both low level and high level :)
If that could be said about quarn, the same could be said about you. After reading all this you don't seem like you want to give C++ a chance. You seem to be constantly arguing that C is better then C++.
This sounds like the old "Goto" arguement all over again..(try coding assembly with no goto's using only if's and while's :) )

It's pointless to argue all this.. you're just argueing flow VS. OO, and not the language..

C was aimed a lowlevel work, where you don't have the option to guess at what happenes where, and you can't afford to have "Black boxes" throughout your code, and for portabilty just modularize your code with it's relevant structures..
The arguement on bothes sides doesn't work, OO is intented as a method to make programming an easier concept to those who don't understand, and don't need to learn how the machine actually works.
C was created for those who used assembly as an alternative to cut dev time down.

Is C better than C++ it's entirely a matter of opinion, anyone who is can program properly in many lanuages (include machine) knows what's needed at what time. It's just another tool.
In some cases Pascal, COBOL, Fortan, even assembly will be better than C / C++ it all depends on what you're trying to accomplish.


have one question. I always thought C# was a Microsoft thing. Can someone explain alittle about it o me? I have Visual C# that came with Visual Studio .NET. I never used it.
Not to go too far into this, but it's a MS thing.. it's a subset of C/C++ OO style with new functions to make working with the web/networks/dll/com easier.. it's kind of a C++/Delphi/Java hybrid .. multiple inheritance.. destructors don't work the same. a lot of the types don't line up with C/C++.. if you're doing a lot of windows programming it's probably a good idea to get a book on it, it'll make you life easier in the long run.. negative part.. it's useless outside the windows world.
Last edited by Kamjin on Sat Oct 30, 2004 12:34 pm, edited 1 time in total.
Kamjin
DC Developer
DC Developer
Posts: 216
Joined: Wed Dec 17, 2003 5:27 am
Has liked: 0
Been liked: 0

Post by Kamjin » Sat Oct 30, 2004 12:33 pm

click the wrong $@#^ button..
quarn
DC Developer
DC Developer
Posts: 80
Joined: Wed Oct 17, 2001 7:44 pm
Location: Sweden
Has liked: 0
Been liked: 0

Post by quarn » Sat Oct 30, 2004 1:58 pm

I agree with Kamjin that different languages have different purposes. I don't use a hammer for my screws, and I assume you don't either.

Though I have yet to see a concrete example from quzar where OOP makes the code harder. I'm not saying that there aren't such situations, I just can't visualize it in my head. And as I said earlier one shouldn't create objects/classes just to use OOP, but rather always follow good code design.

C# is a microsoft thing as it is developed by microsoft. Though it is an open standard (here and here) and has atleast one open source implementation in the works.
c99koder
DC Developer
DC Developer
Posts: 200
Joined: Wed Oct 17, 2001 7:44 pm
Location: East Windsor, NJ
Has liked: 0
Been liked: 0
Contact:

Post by c99koder » Sat Oct 30, 2004 4:05 pm

Anyone feel like porting mono to the Dreamcast? :-P Of course, it probably runs on DC Linux.. I played around with C# + GTK# on my linux box, it's quite a nice language.

As for C vs C++, all of my code is technically C (you can do OOP in C, it's really not that hard), however I run it through the C++ compiler so I can use new / delete instead of constantly typing malloc(sizeof(...));

As for doing OOP, DCBlap's engine is written in C. It's got a linked list, with a few pointers to functions. so for example, a paddle's ->update(); would point to paddle_update(); and the ball's ->update(); points to ball_update();

The main loop just runs through the linked list and calls current->update(current); (to pass a pointer to itself, like "this" in C++). It's more work, but it's certainly not that hard to do. There are parts of KOS that use this same method (the VFS modules, the debug handler in 1.3.x, the maple device drivers, etc.). They make it easy to override the KOS modules, for example I usually replace the KOS VMU detection driver with my own that sends a picture to the LCD when the card is inserted.

-Sam
quarn
DC Developer
DC Developer
Posts: 80
Joined: Wed Oct 17, 2001 7:44 pm
Location: Sweden
Has liked: 0
Been liked: 0

Post by quarn » Sat Oct 30, 2004 4:48 pm

c99koder wrote:As for doing OOP, DCBlap's engine is written in C. It's got a linked list, with a few pointers to functions. so for example, a paddle's ->update(); would point to paddle_update(); and the ball's ->update(); points to ball_update();
Yeah, that's what I meant with simulating OOP in one of my posts at page 1. One thing you don't get by using such a metod is incapsulation and this sort of solution for using OOP is really only feasible if your "class" implements only one "interface". So class "PNG" implementing interfaces "ImageLoader" and "ImageSaver", wouldn't work.

This won't work either if you need several layers of inheritence. Like class "Object", class "Light" extends "Object", class "Sun" extends "Light" or whatever.

Each interface/class requires it's own structure to send around to the different functions and then you will end up with bad code design anyway. But if your program only needs one level of interface/class implementation, then this is one nice way to handle polymorphism.
c99koder
DC Developer
DC Developer
Posts: 200
Joined: Wed Oct 17, 2001 7:44 pm
Location: East Windsor, NJ
Has liked: 0
Been liked: 0
Contact:

Post by c99koder » Sat Oct 30, 2004 4:53 pm

There's more than one pointer in the struct, I just chose update() as an example. there's also message(), create(), etc. you can have as many as you want. Inheritance would have been nice, as there are many properties inside the struct that not every object type uses, but overall it suited what I needed pretty well.

Anyway, I'm reminded of an interesting quote by Rob Pike:
Rob Pike wrote:Here's an analogy. If you want to make some physical artifact, you might decide to build it purely in wood because you like the way the grain of the wood adds to the beauty of the object. In fact many of the most beautiful things in the world are made of wood. But wood is not ideal for everything. No amount of beauty of the grain can make wood conduct electricity, or support a skyscraper, or absorb huge amounts of energy without breaking. Sometimes you need metal or plastic or synthetic materials; more often you need a wide range of materials to build something of lasting value. Don't let the fact that you love wood blind you to the problems wood has as a material, or to the possibilities offered by other materials.

The promoters of object-oriented design sometimes sound like master woodworkers waiting for the beauty of the physical block of wood to reveal itself before they begin to work. "Oh, look; if I turn the wood this way, the grain flows along the angle of the seat at just the right angle, see?" Great, nice chair. But will you notice the grain when you're sitting on it? And what about next time? Sometimes the thing that needs to be made is not hiding in any block of wood.
While he targetted it towards OOP, it applies to both the C and the C++ camp :P
User avatar
Quzar
Dream Coder
Dream Coder
Posts: 7486
Joined: Wed Jul 31, 2002 12:14 am
Location: Miami, FL
Has liked: 0
Been liked: 3 times
Contact:

Post by Quzar » Sat Oct 30, 2004 4:56 pm

All ive been saying is that C++ isnt always the answer, not that C always is... but thats an awsome analogy.
"When you post fewer lines of text than your signature, consider not posting at all." - A Wise Man
quarn
DC Developer
DC Developer
Posts: 80
Joined: Wed Oct 17, 2001 7:44 pm
Location: Sweden
Has liked: 0
Been liked: 0

Post by quarn » Sat Oct 30, 2004 5:21 pm

c99koder, I wasn't suggesting that update() must be the only pointer in the struct either. Did you get my point about not being able to implement two interfaces or have multiple levels of inheritance though?
quzar wrote:All ive been saying is that C++ isnt always the answer, not that C always is...
And I keep asking you, when isn't C++ the answer? :) But I haven't recieved an example yet..

As I see it, C++ is C but with the option to use OOP among other features.

All I wanted to know was why people prefer C instead of C++ and I havn't really seen any arguments pro C and con C++. Or maybe I just didn't understand.

I promote C++ because of the object orientation and all the development time good designed OO saves.
Post Reply