Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Newbie question regarding programming

106 views
Skip to first unread message

ag...@drrob1.com

unread,
Oct 20, 2014, 8:16:40 PM10/20/14
to
I am trying to learn c++, after many years of hobby programming w/
modula-2, and recent ada programming.

I am mostly finished w/ a free edx.org course on c++. I have found
this worthwhile, but my question is this: how can I write a program
with dividing my code into separate files.

Modula-2 and ada both easily do separate compilation and linking. But
I don't know how to do this after taking an online course in c++.

Does this have something to do with make?

Thanks,
Rob
Message has been deleted

ag...@drrob1.com

unread,
Oct 20, 2014, 9:40:44 PM10/20/14
to
On 21 Oct 2014 00:56:20 GMT, r...@zedat.fu-berlin.de (Stefan Ram)
wrote:

>ag...@drrob1.com writes: how can I write a program
>>with dividing my code into separate files.
>
> The answer has two parts.
>
> The first part is implementation-independent.
> Create these two files:
>
> hello.cpp
>
>#include <iostream> // ::std::cout
>#include <ostream> // <<
>void hello(){ ::std::cout << "Hello, "; }
>
> main.cpp
>
>#include <iostream> // ::std::cout
>#include <ostream> // <<
>void hello();
>int main(){ ::hello(); ::std::cout << "Example!\n"; }
>
> The second part is implementation-dependent: Look up in the
> documentation of your implementation how two compile two
> files and link the resulting object files to an executable.
>
> After this step, you can do two exercises:
>
> Exercise:
>
> Extract the declaration of »hello« from »main.cpp« into a
> separate header file and include this header file in both
> source files.
>
> Exercise:
>
> Add include guards to the header file.

I should have added that I'm talking about Ubuntu 14.04 gcc

Where would I find the documentation of which you speak?

ag...@drrob1.com

unread,
Oct 20, 2014, 9:42:56 PM10/20/14
to
On 21 Oct 2014 00:56:20 GMT, r...@zedat.fu-berlin.de (Stefan Ram)
wrote:

>ag...@drrob1.com writes: how can I write a program
>>with dividing my code into separate files.
>
> The answer has two parts.
>
> The first part is implementation-independent.
> Create these two files:
>
> hello.cpp
>
>#include <iostream> // ::std::cout
>#include <ostream> // <<
>void hello(){ ::std::cout << "Hello, "; }
>
> main.cpp
>
>#include <iostream> // ::std::cout
>#include <ostream> // <<
>void hello();
>int main(){ ::hello(); ::std::cout << "Example!\n"; }
>
> The second part is implementation-dependent: Look up in the
> documentation of your implementation how two compile two
> files and link the resulting object files to an executable.
>
> After this step, you can do two exercises:
>
> Exercise:
>
> Extract the declaration of »hello« from »main.cpp« into a
> separate header file and include this header file in both
> source files.
>
> Exercise:
>
> Add include guards to the header file.

And, what's a guard?
Message has been deleted

ag...@drrob1.com

unread,
Oct 21, 2014, 7:03:44 AM10/21/14
to
On 21 Oct 2014 01:46:35 GMT, r...@zedat.fu-berlin.de (Stefan Ram)
wrote:

>>I should have added that I'm talking about Ubuntu 14.04 gcc
>>Where would I find the documentation of which you speak?
>
>$ g++ hello.cpp main.cpp
>$ ./a.out
>Hello, Example!
>
> (Finding out about include guards is intended to be a /part
> of/ the exercise.)

Thanks

Osmium

unread,
Oct 21, 2014, 8:01:45 AM10/21/14
to
<ag...@drrob1.com> wrote:

> And, what's a guard?

An include guard is part of the pre-processor; it's a mechansims to assure
that a an include only happens once. Wiki should be helpful to you here.

include guard wiki


Robert Hutchings

unread,
Oct 21, 2014, 8:12:43 AM10/21/14
to
Does "#pragma once" accomplish the same thing? I am also a n00b.....

Osmium

unread,
Oct 21, 2014, 8:44:43 AM10/21/14
to

"Robert Hutchings" <rm.hut...@gmail.com> wrote in message
news:m25ijc$jgc$1...@dont-email.me...
Again, Wikipedia is your friend.



.


Jorgen Grahn

unread,
Oct 21, 2014, 4:48:49 PM10/21/14
to
On Tue, 2014-10-21, ag...@drrob1.com wrote:
> I am trying to learn c++, after many years of hobby programming w/
> modula-2, and recent ada programming.
>
> I am mostly finished w/ a free edx.org course on c++. I have found
> this worthwhile, but my question is this: how can I write a program
> with dividing my code into separate files.
>
> Modula-2 and ada both easily do separate compilation and linking. But
> I don't know how to do this after taking an online course in c++.

No, those things are often left out from courses and tutorials for
some reason.

I can show a fairly simple example of mine (nevermind what the
programs /do/):

git://github.com/kjgrahn/groblad.git

> Does this have something to do with make?

Make is a good and popular way of managing C++ programs split into
many source files, yes. I use it.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
Message has been deleted

ag...@drrob1.com

unread,
Oct 21, 2014, 6:59:02 PM10/21/14
to
On 21 Oct 2014 21:02:22 GMT, r...@zedat.fu-berlin.de (Stefan Ram)
wrote:

>Jorgen Grahn <grahn...@snipabacken.se> writes:
>>No, those things are often left out from courses and tutorials for
>>some reason.
>
> For a long time, I have had it in my beginner's course,
> but some years ago, I have moved it to the advanced learners'
> course. The beginner's course only contains such small
> programs that it was difficult to explain what it is good for,
> and some other topics needed to be added to the beginner's course.
> Moreover, one might discuss whether this is still part
> of the language proper or rather part of a specific environment.


In 1973 I was in high school, and learned fortran. When I got to
college a few years later, I used what I had, which was fortran and
basic, w/ a little of 360 assembler.

Then I learned modula-2, and still use it for my own needs on windows.
As the language is essentially not supported, I am now trying Ada.

Then I read about edx.org and decided to take their c++ course. That
course is almost finished. I am going to try to translate some of my
code into c++ and see how that goes.

I think I will try #pragma once for include guards. The other
languages I have used do not have the concepts that need these guards.

I find it very hard to read C-ish code. So I've added these other
helpers

#define EQ ==
#define NE !=
#define MOD %
#define AND &&
#define OR ||
#define IS {
#define END }
#define ADR &
#define POINTERTO *
#define PROCEDURE void

I have others, but this makes the point. I also do not make my living
programming so I am free to do as I like.

I'll add to my list
#pragma once

And I'll see how that goes.

One stumbling block for me is the GUI in Ubuntu. Stony Brook modula-2
has what I need to use the windows GUI, but I do not know how to do
this in linux. I came across ncurses, but I cannot find documentation
that is good enough for me to use it. Yet.

I'll keep trying.

Ian Collins

unread,
Oct 21, 2014, 8:06:30 PM10/21/14
to
ag...@drrob1.com wrote:
>
> I find it very hard to read C-ish code. So I've added these other
> helpers
>
> #define EQ ==
> #define NE !=
> #define MOD %
> #define AND &&
> #define OR ||
> #define IS {
> #define END }
> #define ADR &
> #define POINTERTO *
> #define PROCEDURE void

If you fill your code with such abominations, you will find it hard to
get help when you need it. If you are going to use C++, don't try and
make it look like another language. Such actions inevitably end in tears.

--
Ian Collins

peter koch

unread,
Oct 22, 2014, 8:47:56 AM10/22/14
to
Den onsdag den 22. oktober 2014 00.59.02 UTC+2 skrev ag...@drrob1.com:
> Then I learned modula-2, and still use it for my own needs on windows.
> As the language is essentially not supported, I am now trying Ada.
>
> Then I read about edx.org and decided to take their c++ course. That
> course is almost finished. I am going to try to translate some of my
> code into c++ and see how that goes.
>
> I think I will try #pragma once for include guards. The other
> languages I have used do not have the concepts that need these guards.
>
> I find it very hard to read C-ish code. So I've added these other
> helpers
>
> #define EQ ==
> #define NE !=
> #define MOD %
> #define AND &&
> #define OR ||
> #define IS {
> #define END }
> #define ADR &
> #define POINTERTO *
> #define PROCEDURE void

Please don't! I have a somewhat similar background as yourself - moving from Algol(W) and Pascal to C, and I remember my confusion the firast time I saw C-code. &&, {, ~ and all this other stuff (if (i) instead of if (i != 0)) and found it awful. I also remember considering #define'ing myself out of that
mess.
But sticking with those "awful characters" is strongly recommended. First of
all, it will help you read the code from others. Secondly you might - after a while - appreciate the notion and think back to those horrible days where you had to write BEGIN and other screaming novellas in your code. This has happened to me. Perhaps this is related to the Stockholm syndrome, but I do not care anymore. I even write if (x) instead of e.g. if (x != 0) whenever x is a pointer or a resource such as a std::stream. If x is an integer, I still keep the " != 0".
>
> I'll add to my list
> #pragma once
>
I stick with include-guards which is the "official" and portable way of doing it.

/Peter

ag...@drrob1.com

unread,
Oct 22, 2014, 7:57:04 PM10/22/14
to
After adding #pragma once, I learned that this is the default setting
for gnu under Ubuntu 14.04. So I removed it again.

As for my verbose macros, I have the luxury of not making my living
programming. That's probably why I've been able to not learn c-ish
for so long. I first looked at C in 1982 or so, and quickly looked
away.

I understand the point about other people's code; I think I can handle
that if I go slowly. But I really do think that I need to use a macro
to not mistake = vs ==, and also & vs &&

Louis Krupp

unread,
Oct 22, 2014, 8:35:32 PM10/22/14
to
On Tue, 21 Oct 2014 18:58:47 -0400, ag...@drrob1.com wrote:


>#define EQ ==
>#define NE !=
>#define MOD %
>#define AND &&
>#define OR ||
>#define IS {
>#define END }
>#define ADR &
>#define POINTERTO *
>#define PROCEDURE void

People will disapprove, but once you've had to multipunch ALGOL
special characters on an IBM 026, this is the only way to go. I still
remember the Hollerith codes for left parentheses (12-8-5), right
parentheses (12-8-6) and semicolons (11-8-6). If I'd just defined
names for those as above and punched copies to insert into every ALGOL
program I wrote, I'd have some brain cells left over for something
useful today.

(Note that the ampersand is also a binary operator for which you'll
want another name, and while that's enough for C, C++ uses the
ampersand in reference declarations as well. And while the asterisk
means "pointer to" in declarations, in expressions, the unary operator
means "dereference.")

My advice is to #define PROCEDURE as nothing at all (just "#define
PROCEDURE" on its own line) so you can help make your code look a bit
more like ALGOL, which can only be a good thing:

int PROCEDURE n(...)

void PROCEDURE s(...)

(Yes, I am joking. Sort of.)

Louis

Paavo Helde

unread,
Oct 23, 2014, 12:03:52 AM10/23/14
to
ag...@drrob1.com wrote in
news:qrgg4ad169es6tq8o...@4ax.com:

> But I really do think that I need to use a macro
> to not mistake = vs ==, and also & vs &&

Nowadays compilers like gcc issue warnings in many cases when these
operators are messed up. Just do not ignore warnings.

Cheers
Paavo



0 new messages