PDA

View Full Version : hello world



Unregistered
05-09-2002, 11:51 AM
I always see something like this::


// wrong, but it works!
#include <iostream>

int main()
{
std::cout<<"Hello, World!\n";
return 0;
}


Just for the record C++ is an Object Oriented Programming Language
So why teach in a non-object oriented manner?



//this is more acceptable
#include <iostream>


class cProgram{
public:
cProgram(){ std::cout << "Hello, World!\n"; }
~cProgram(){ std::cout << "Good bye, World!\n"; }
}

cProgram myApp;

int main()
{
return 0;
}




Its not much more complex than the first example.
And well worth learning.

Lets see some fance hello_world.cpp code.

jdinger
05-09-2002, 11:55 AM
Because for an absolute beginner who doesn't even know what "#include" does/means so why dump learning classes on them from right from the start?

Unregistered
05-09-2002, 12:06 PM
Why shouldn't you teach classes from the start?

What book doesn't explain "hello world" line by line.
cProgram could simpley be introduced, as a user defined type
After all isn't that what it is?

jdinger
05-09-2002, 12:10 PM
Read my post again. If you're dealing with a newbie who barely knows what a compiler on do you really think you should dump classes on them from the start? I love classes. I'd hate to write without them, but back when I didn't know the difference between an int and the function that was returning it, I'd have been completely lost if I tried to learn classes then. How can a person learn classes if they can't even define the variables and functions that would make up said classes? You have to crawl before you can walk.

Unregistered
05-09-2002, 12:13 PM
The point Iím trying to make is that the power of classes should be taken advantage of, from the start of ones venture into programming. As powerful as classes are, they should be included in every application in which their abilities can do task for the programmer.

Hammer
05-09-2002, 12:22 PM
.. but isn't the first example code using classes anyway? Isn't std::cout part of a class? Why embed it in more unneccasry code?

I dunno!:D

Barjor
05-09-2002, 12:27 PM
Why??Because it is flat out ugly. C++ is not a pure object lang and thank good for that. And Hammer is right. You get all power of objects using cout anyway. The code you posted don't compile on my computer.

Betazep
05-09-2002, 12:34 PM
//this is more acceptable
#include <iostream>


class cProgram{
public:
cProgram(){ std::cout << "Hello, World!\n"; }
~cProgram(){ std::cout << "Good bye, World!\n"; }
}

cProgram myApp;

int main()
{
return 0;
}


And besides that... since you are not instantiating your object in main as a local variable, you would be wrong to teach that example anyway... ;)

A lot can be done without using classes... and yes.... you use objects anyway when you #include libs... one idea of objects is that you can utilize code that has already been written instead or rewriting it all yourself; though that isn't really true object oriented programming as C has always had that capability....

Unregistered
05-09-2002, 12:34 PM
>>If you're dealing with a newbie who barely knows what a compiler on do you really think you should dump classes on them from the start? <<
the answer is YES.
Classes aare the VERY same thing as int, char, double, and even unsigned int.

After all its a Hello World app. Your knowledge of programming is just starting.

A perfect example is my nephew (he is 3) and counts to 50, knows all of his abc's and can almost tie his own shoes.

Carry on a conversation very well, and understand most of what he is told.

He learns this by being exposed to it.
Not by having to wait.
If you are getting started DONíT be lazy! Learn what you have to learn.
Classes are Easy.


They are simpley different types of data which one can use with others to arrange into more understandable formats.

They NEED to be included into any helloworld.cpp.

ygfperson
05-09-2002, 12:36 PM
why make hello world in a class just because c++ is more object oriented than c? use java for that. :D

Barjor
05-09-2002, 12:39 PM
Originally posted by Unregistered
>>
They NEED to be included into any helloworld.cpp.

No that would be to miss use classes. If you want an all object lang use Java or C#.

incognito
05-09-2002, 01:28 PM
Originally posted by Unregistered
I always see something like this::


// wrong, but it works!
#include <iostream>

int main()
{
std::cout<<"Hello, World!\n";
return 0;
}


Just for the record C++ is an Object Oriented Programming Language
So why teach in a non-object oriented manner?



//this is more acceptable
#include <iostream>


class cProgram{
public:
cProgram(){ std::cout << "Hello, World!\n"; }
~cProgram(){ std::cout << "Good bye, World!\n"; }
}

cProgram myApp;

int main()
{
return 0;
}




Its not much more complex than the first example.
And well worth learning.

Lets see some fance hello_world.cpp code.


Oh the irony this program doesn't even compile. :D just looking at the irony that's all.

Nick
05-09-2002, 01:54 PM
From one of the rational software books or something?

// wrong, but it works!
#include <iostream>

int main()
{
std::cout<<"Hello, World!\n";
return 0;
}

Even in this program your using objects. Your passing "Hello, World!\n" To global object std::cout opererator<<. Since you already have a class and object which abstracts the process of printing stuft on the screen, it's worthless creating one which just prints hello.

Barjor
05-09-2002, 01:58 PM
hey elchulo2002
Do you like the "Direct X 7 in 24h" Book? I been looking at it but never got around to buy it. DO you have anything to recomend for Open GL?

incognito
05-09-2002, 02:03 PM
Originally posted by Barjor
hey elchulo2002
Do you like the "Direct X 7 in 24h" Book? I been looking at it but never got around to buy it. DO you have anything to recomend for Open GL?

check for rather lenghty response on your pm.

Prelude
05-10-2002, 07:40 AM
>Just for the record C++ is an Object Oriented Programming Language
Is it? Last I heard C++ was an Object Oriented extension slapped on a procedural language.

>So why teach in a non-object oriented manner?
Because C++ is capable of both OO and procedural solutions, so to truly learn the language one would have to learn both. Since procedural is usually easier to accept in the beginning it is often taught first and the concepts of OO are saved for when the learner can better handle it.

>A perfect example is my nephew (he is 3) and counts to 50,
>knows all of his abc's and can almost tie his own shoes.
Good for him, he's obviously ready for Polymorphism, Inheritance, and Encapsulation, so what's keeping you from teaching him C++?

>He learns this by being exposed to it.
>Not by having to wait.
>If you are getting started DONíT be lazy! Learn what you have to learn.
Good idea, hit someone new to programming with the whole complexity of C++ and watch how long they stick around. Start easy so that you don't scare them off, then slowly build up. You may like to be overwhelmed constantly, but you would be the exception.

-Prelude

stevey
05-10-2002, 11:53 AM
i can only program in C (and its debatable-i'm only at weenie stage). i'm learning C++ and visual C++, altho its only a hobby now, ive given up on ever getting a C programming job......

i think its better to learn C first, then C++ after rather than overload my tiny mind. i don't think the concept of OOP is easy at all...course i'm probably as thick as a whale sandwich...

RobR
05-10-2002, 12:01 PM
I actually started with [WHISPER]VB[\WHISPER] and then went to C++. Personally, I find OOP fairly easy to understand in principle (read Bruce Eckal, Thinking in C++). In practice, I find the hardest bit of OOP is what are my objects. That bit just doesn't seem to flow naturally for me.

To put it another way, the theory seems very pretty, but the practice looks hacky & ugly!!

GSLR
05-10-2002, 12:16 PM
Hi

Have been learning c for a year now, do ok i suppose.

Whats a class???

Q

jdinger
05-10-2002, 12:25 PM
GLSR, if you been studying just C and not C++ then don't feel bad about not know what a class is. Classes were introduced in C++. Basically think of a class as a struct that can have functions as members that manipulate/return the variable members.

Like this:

In C


struct Test
{
int i;
int j;
};

Test test;

int GetI()
{
return(test.i);
}


In C++


class Test
{
private:
int i;
int j;
public:
Test();
~Test();
int GetI();
};

Test test;

int k=test.GetI();

endo
05-10-2002, 12:39 PM
Does anyone remember how hard it was to learn C++ without any prior programming knowledge? I certainly do, and I had enough to cope with trying to figure out pointers for the first time. Classes in the first program would have made it so much harder to understand!

ps hi all, I have been unregistered for ages but finally got round to it :D

Barjor
05-10-2002, 12:44 PM
C++ is very intimidating(sp) before you get the big picture. I was losing hair when I first started. I stil don't know much but I got to the point where it is fun and it flows alot better because I got the concept down.

ygfperson
05-10-2002, 12:49 PM
it actually didn't seem that hard to me. at first i was confused on why people want/need objects, but it makes sense now. when engineered correctly (a big if), objects encapsulate the many details. it becomes much easier. however, linux doesn't seem to use it that often (to my knowledge). it prefers C. libraries aren't put into objects, they're just functions and defines. i'm confused on why linux doesn't use more c++.

stevey
05-10-2002, 04:51 PM
can you give a simple example of why we need/want objects ??? cos i'm not really sure on this at all...

RobR
05-10-2002, 04:55 PM
There's a thread here... (http://www.cprogramming.com/cboard/showthread.php?s=&threadid=17444)

PsychoBrat
05-10-2002, 09:05 PM
oh ffs! its kinda obvious that you should keep the first lesson simple, then move on to the slightly more complicated concepts.

that guy that was talking about the 3 yearold kid:

did you try to teach him/her the whole alphabet in his/her first lesson? NO? i didn't think so :P

-out-
PsychoBrat

stevey
05-11-2002, 11:23 AM
Originally posted by RobR
There's a thread here... (http://www.cprogramming.com/cboard/showthread.php?s=&threadid=17444)

thanx, i will peruse that link shortly.....

OxYgEn-22
05-11-2002, 01:11 PM
Going back to the book subject for a minute :D, I actually own the book "OpenGL Game Programming" by the "Game Development Series" and I find it to be fantastic!!, Bought over amazon.com because this book was expensive as hell!!! but i love it to death. It has all sorts of good code to use. I was wondering if anyone else had read a book by this series?? I was thinking about buying the Multyplayer Game Programming book next, anyone have that and want to comment on it?? :D