How do you actually DO anything?

G

grhooked

I'm learning to program (C++ as my first language - a person
recommended it, saying I would understand OOP better).

I am not yet onto the actual OO things yet, but I understand
(basically) variables, arrays, pointers, and all the statements and
loops.

And yet it seems like I cannot do anything with my knowledge. The most
complex program I've managed to write is a program that squares a
number, returns an error if the answer to continuation questions is
not y/n, etc.

Would someone give me some code that shows me how to MAKE anything? I
can find examples, but I would not know what is GOOD code and would
inherit bad habits easily.
 
M

Michael Mol

I'm learning to program (C++ as my first language - a person
recommended it, saying I would understand OOP better).

There are any number of languages which are fine for OOP. But I do
prefer C++ for it. :)
I am not yet onto the actual OO things yet, but I understand
(basically) variables, arrays, pointers, and all the statements and
loops.

And yet it seems like I cannot do anything with my knowledge. The most
complex program I've managed to write is a program that squares a
number, returns an error if the answer to continuation questions is
not y/n, etc.

Would someone give me some code that shows me how to MAKE anything?

What do you want to make? A GUI program? A command-line tool? What
is it that you want to do that inspired you to want to learn to
program?
I can find examples, but I would not know what is GOOD code and would
inherit bad habits easily.

"Good" code is, to an extent, a subjective measure. Sure, you can
have a program that meets its design requirements, compiles without
warnings with all strict options enabled, and may even be provably
"correct", but that's only a start. Where you go from there in terms
of code quality varies from camp to camp. There are a lot of smart
people with very different ideas of what qualifies as "good" code.

Picking up bad habits is inevitable. Like any bad habit, the trick is
to break them when you recognize them as such. Your best bet is to
just sit down and write something you find useful. Which brings me
back to my earlier question: What do you want to make?
 
G

grhooked

There are any number of languages which are fine for OOP.  But I do
prefer C++ for it. :)






What do you want to make?  A GUI program?  A command-line tool?  What
is it that you want to do that inspired you to want to learn to
program?


"Good" code is, to an extent, a subjective measure.  Sure, you can
have a program that meets its design requirements, compiles without
warnings with all strict options enabled, and may even be provably
"correct", but that's only a start.  Where you go from there in terms
of code quality varies from camp to camp.  There are a lot of smart
people with very different ideas of what qualifies as "good" code.

Picking up bad habits is inevitable.  Like any bad habit, the trick is
to break them when you recognize them as such.  Your best bet is to
just sit down and write something you find useful.  Which brings me
back to my earlier question: What do you want to make?

Well, I started learning about computers because I wanted to
understand all these error messages I get. Then I got into programming
C++ because I heard it translates very well into PHP, and a site I
used to admin uses PHP as its server language.

Right now, I want to make a program that changes the desktop
background at certain intervals.
 
R

rep_movsd

C++ as a first language ( PHP doesn't really count as a language ) is
not an ideal choice. Whoever told you that C++ translates to PHP well
is very misled.

Start out with something else -
For some folks Delphi or C# is the ideal choice ( Windows GUI and
desktop stuff )
Some others swear by python.
 
M

MiB

I am not yet onto the actual OO things yet, but I understand
loops.

You also need to understand the environment in which your programs are
going to be executed. The C++ programming language comes with a
standard library of functions: the I/O streams library, the standard
template library STL, the standard C library, to name some. These
allow you to create programs i.e. for console output without deep
knowledge of your operating system. However, if you are going to write
applications that proffer a GUI with buttons, menus, and so on, the C+
+ keywords and standard functions are not enough. The same applies to
programs that, like your task of choice, interoperates with system
specifics.
Right now, I want to make a program that changes the desktop
background at certain intervals.

If you want to stick with C++ on Windows, you should download a copy
of Microsoft's Visual C++ Express edition. This is not intended to
start a "My IDE is better than yours" flame war, but I feel for a
beginning programmer on Windows this free of charge IDE gives some
head start. Get a good book on Windows programming, read about the
Windows SDK in general, maybe have a look at concepts like "callback
functions", "event loops" and the design patterns "Model-View-
Controller" and "Inversion of Control". This will help you to
understand how GUIs and GUI elements interact with your business
logic. Start with a small GUI application, not with drilling deep into
the inner workings of the Windows Shell. You selected task would
require you to programmatically manipulate the Windows internal
database "Registry". This is a very sensitive spot of the Windows OS -
not a beginner's topic. Screwing up here it's easy to shoot down a
system to the point of not booting anymore, having to install the
whole computer from scratch.

Be aware, most of the stuff is OS specific - Unix applications use
similar general concepts but are very different in implementation
details.

You can find plenty of example code, ten thousands of related articles
at http://www.codeguru.com, http://www.sourceforge.net, http://www.codeproject.com,
and many more similar sites. Its probably not helpful to give source
code for a Windows C++ GUI project here - usually a lot of code
generated by the IDE is involved; you do not need to read or
understand it to create GUI programs as long as you understand the
concepts behind it. The "OO things" are a vital part of concurrent
Windows programming, so you should try to understand the concept of
classes, objects and inheritance in order to make sense of it.
Well, I started learning about computers because I wanted to
understand all these error messages I get. Then I got into programming
C++ because I heard it translates very well into PHP, and a site I
used to admin uses PHP as its server language.

PHP and C++ share some syntactic similarities - but that's about it.

best,

MiB.
 
S

Stefan Ram

And yet it seems like I cannot do anything with my knowledge.

For example, you can write a program calculating the return
of an investment after 10 years assuming a certain interest
rate.

The »problem« is that today we have spreadsheet software that
is easier to use for this purpose. But some decades ago, there
where programming languages, but no spreadsheet software yet.
Back then it was perceived as a great achievement to be able
to calculate something like this. You can still calculate it
using C++ today, but today there already is special-purpose
software for this.

To create software with C++ that does something new, something
that can not be done with a database, spreadsheet or another
existing application, you usually will have to learn more C++
and also more about additional libraries, like GUI libraries.

C++ is not completely useless as most desktop software should
be written in C or C++. For example, I assume that Windows,
Word, Excel, Access, and so on are written in C or C++. But to
write such software, you will have to continue learning C++.

C++ is a language where one can not do much with a little
knowledge, it is better either not to learn it at all or to
invest a lot of time to C++, common libraries and coding. I
agree to other posters that usually it is not a good choice as
a first language »to learn programming«. Java is easier for
this purpose, and one can do many things with its standard
edition one can not do with standard C++, like writing GUI
software, accessing the internet, or accessing the file
system. And it has a garbage collector that makes memory
management easier.

See also:

http://norvig.com/21-days.html
 
S

Stefan Ram

Would someone give me some code that shows me how to MAKE anything?

A more technical answer would be:

A C++ implementation does something by evaluating
expressions of a C++ program. The evaluation of an
expression can have effects. These effects are
partially determined by the expression written in
the source doe and partially by the state of the
world at the instant of evaluation.

For example, the evaluation of the expression
»::std::cout << "\n"« might have the effect of the
emission of a line terminator to the standard output.

So, if this is what you wanted to do, you can do it by
writing or copying this expression and having it being
evaluated at the time you want this effect to happen.

Your wish is not granted unless it's a fish,
Your wish is not granted unless it's a dish,
A fish on a dish, is that what you wish?

The Incredible String Band
 
V

Vinodh

  A more technical answer would be:

  A C++ implementation does something by evaluating
  expressions of a C++ program. The evaluation of an
  expression can have effects. These effects are
  partially determined by the expression written in
  the source doe and partially by the state of the
  world at the instant of evaluation.

  For example, the evaluation of the expression
  »::std::cout << "\n"« might have the effect of the
  emission of a line terminator to the standard output.

  So, if this is what you wanted to do, you can do it by
  writing or copying this expression and having it being
  evaluated at the time you want this effect to happen.

      Your wish is not granted unless it's a fish,
      Your wish is not granted unless it's a dish,
      A fish on a dish, is that what you wish?

    The Incredible String Band

Start with this program
#include <iostream>
using namespace std;

int main()
{
cout << " Hello world" << endl;
return 0;
}
 
M

Michael Mol

Writing a program that changes the desktop background image requires
knowledge of the API for your operating system, and that's a large leap
if you're still learning the language.  I'd recommend staying in the
"Writing simple programs for the terminal" stage until you're
comfortable enough with C++ to advance.

Knowledge of the OS API isn't that big of a leap, though discussion of
specifics offtopic for this group. (I already sent a direct-email
reply offering to walk him through it, but I haven't heard back yet.)
OS (and, more specifically, desktop environment) APIs are generally
well documented, and they're even scriptable from the command line for
some operating systems.
 
D

Default User

Preston wrote:

C++ does not translate to PHP well. PHP has a vaguely C-like syntax
in its function calls and use of semicolons to end statements, but
that's it. If you're wanting to learn PHP, you should simply learn
PHP and save yourself some trouble.

"Vaguely C-like"? The relationship is much closer than that. Anyone
familiar with C is going to be able to pick up a lot of PHP rapidly.
Now, the string and array capabilities will be less familiar, and take
some time.




Brian
 
D

Default User

No, the relationship isn't closer than that. I've seen so many PHP
programmers decide to jump into C because, hey, they already know how
to use semicolons and function calls, right? They basically already
know C! They give it up after discovering fun things like pointers
and manual memory management.

You're making a mountain out of the proverbial molehill. Coming from
almost ANY other language to C the same learning curve will exist. Very
few languages have the sort of dependency on pointers and dynamic
memory as C.

The overlap between the libraries of the two languages is substantial
(with a few gotchas). The control structures are virtually the same.
It's easier, probably, to go from C to PHP, but that's not a big deal.
If you can't make the transition, you weren't cut out to be C
programmer anyway. The basis that PHP gives you is probably the
soundest of any of the freer-form languages.




Brian
 
S

Stefan Ram

Preston said:
No, the relationship isn't closer than that. I've seen so many PHP
programmers decide to jump into C because, hey, they already know how
to use semicolons and function calls, right? They basically already
know C! They give it up after discovering fun things like pointers and
manual memory management.

C and C++ exist partially because there must be some language
memory management systems are being implemented in (so this
language can not itself have memory management). This is
perfectly valid.

It only is a mistake to use either C or C++ nowadays in
schools or similar institutions to teach programming to
students who do not want to become programmers and can not
dedicate a lot of time to programming.

C and C++ are optimized for run-time efficiency. There are
languages that are more beginner friendly.

Pascal is beginner friendly, because it is small. One can not
do much with it, but a program that is used each day by
thousands of scientists was written in Pascal (that is, TeX).

Java is not that small, but has memory management and standard
libraries to accomplish common tools (GUI, web, and file
system access).

The JVM is partially written in C. The recently released
Parrot 1.0 is written in C. So, if you want to implement a
memory manager or a programming language or a virtual machine,
use C(++). But usually people writing such software have
dedicated their lives to programming. They are not students
who only have a couple of hours to learn some basics of
a programming language.
 
G

grhooked

Knowledge of the OS API isn't that big of a leap, though discussion of
specifics offtopic for this group. (I already sent a direct-email
reply offering to walk him through it, but I haven't heard back yet.)
OS (and, more specifically, desktop environment) APIs are generally
well documented, and they're even scriptable from the command line for
some operating systems.

Thanks for sending me an email, I responded to you.
 
G

grhooked

  C and C++ exist partially because there must be some language
  memory management systems are being implemented in (so this
  language can not itself have memory management). This is
  perfectly valid.

  It only is a mistake to use either C or C++ nowadays in
  schools or similar institutions to teach programming to
  students who do not want to become programmers and can not
  dedicate a lot of time to programming.

  C and C++ are optimized for run-time efficiency. There are
  languages that are more beginner friendly.

  Pascal is beginner friendly, because it is small. One can not
  do much with it, but a program that is used each day by
  thousands of scientists was written in Pascal (that is, TeX).

  Java is not that small, but has memory management and standard
  libraries to accomplish common tools (GUI, web, and file
  system access).

  The JVM is partially written in C. The recently released
  Parrot 1.0 is written in C. So, if you want to implement a
  memory manager or a programming language or a virtual machine,
  use C(++). But usually people writing such software have
  dedicated their lives to programming. They are not students
  who only have a couple of hours to learn some basics of
  a programming language.

I don't care about it being easy (anything easy isn't worth doing). I
do want it to be 'doable;' in that, after putting in time and effort,
I understand what I am doing. I am a student, but not in any field of
programming (this, tennis, and baseball are the activities I dedicate
my free time to).

I don't understand the USE of pointers, and arrays just baffle me. The
loops and most statements make sense, however. Perhaps some hidden
functionality lies in those two that is keeping me from achieving my
purpose.

@ Michael:

Thanks for the e-mail. I responded to you.
 
J

Jorgen Grahn

On 23 Mar 2009 13:28:27 GMT said:
C++ is a language where one can not do much with a little
knowledge, it is better either not to learn it at all or to
invest a lot of time to C++, common libraries and coding.

This varies a bit with the environment you are writing for, I suspect.
On Unix, those common libraries are pretty much the same for all the
popular languages (C, C++, Perl, Python). The APIs you use from C++
are C-based and tedious, but at least you can learn them once and
apply that knowledge in different languages (with some twists).

Java is (based on hearsay) different: you buy into a whole platform
which doesn't look much like anything else.
[Java] has a garbage collector that makes memory
management easier.

And resource management harder. Some people want GC, but many (most)
do not miss it in everyday C++ programming.

/Jorgen
 
S

Stefan Ram

Jorgen Grahn said:
[Java] has a garbage collector that makes memory
management easier.
And resource management harder. Some people want GC, but many (most)
do not miss it in everyday C++ programming.

I do not see how the memory manager makes the management
of other resources harder.

Java does not have language constructs for scope guards
(RAII), but this does not seem to be caused by the existence
of a memory manager. One can imagine a language with both a
memory manager and objects on the stack that are being
destructed when the scope is being exited.
 
G

Guest

  C and C++ exist partially because there must be some language
  memory management systems are being implemented in (so this
  language can not itself have memory management). This is
  perfectly valid.

This seems an odd reason for the existence of C or C++.
Aren't some memory managed languages implemented in themselves
or in subsets of themselves?

In fact to implement malloc() or new requires something other
than full C or C++ (eg. a subset of one or other of them).
malloc()/new often relies on the underlying OS. Which in turn
may be written in some dialect of C...

It's turtles all the way down.

In summary, I think you are wrong.
  It only is a mistake to use either C or C++ nowadays in
  schools or similar institutions to teach programming to
  students who do not want to become programmers and can not
  dedicate a lot of time to programming.

are there many people who learn to program without becoming
programmers?
  C and C++ are optimized for run-time efficiency. There are
  languages that are more beginner friendly.

probably true
  Pascal is beginner friendly, because it is small. One can not
  do much with it, but a program that is used each day by
  thousands of scientists was written in Pascal (that is, TeX).

I'm not sure Pascal is a good example these days (not enough
implementaions and other users readily available). Python
seems to give quick results.
  Java is not that small, but has memory management and standard
  libraries to accomplish common tools (GUI, web, and file
  system access).

  The JVM is partially written in C.

some of them are
The recently released
  Parrot 1.0 is written in C. So, if you want to implement a
  memory manager or a programming language or a virtual machine,
  use C(++).

C++ isn't always the tool of choice for implementing languages.
But usually people writing such software have
  dedicated their lives to programming.


poor sods :)
They are not students
  who only have a couple of hours to learn some basics of
  a programming language.

people expect to accomplish something in a complex field
in only a couple of hours?

Does it work?

I don't think I could learn Java or pascal in a couple of hours
 
G

Guest

I don't care about it being easy (anything easy isn't worth doing).

good for you!
I
do want it to be 'doable;' in that, after putting in time and effort,
I understand what I am doing. I am a student, but not in any field of
programming (this, tennis, and baseball are the activities I dedicate
my free time to).

I don't understand the USE of pointers, and arrays just baffle me.

well, you can go a long way in C++ without using raw arrays (prefer
std::vector).
"Accelerated C++" is a good book for learning C++ as a high level
language.

I'll probably get shot for this, but "The C Programming Language" by
Kernighan and Richie is pretty good on pointers and arrays.

Arrays are easy.
int i;

That's an int (or integer)
int a [10];

is a collection of 10 contiguous ints. To sum them

sum = 0;
for (i = 0; i < 10; i++)
sum = sum + a;

That's about it! Pointers are essentially machine addresses
(on most implementaions). The syntax is a bit horrid but it's
based on the idea that declaration mirrors usage (no one is sure if
this was
good idea but its what we've got!).

int* pa;
pa = &i;

pa is a pointer to int. It stores the address of an int.
&i means take the address of i. So pa is storing the address
of i or pa is pointing to i.

i = 10;

cout << i;

prints 10 as does

cout << *pa;

where the * DEREFERENCES pa. It returns the value stored in the
address
pointed to by pa.

In

int* pa;

pa is a pointer to an int (an int*) and also *pa is an int.
This is why C programmers (but less often C++ programmers) write
int *pa;

To the compiler (C or C++) they mean the same thing.

Pointers are useful for holding dynamically allocated memory
or building complex data structures (lists or trees).

The
loops and most statements make sense, however. Perhaps some hidden
functionality lies in those two that is keeping me from achieving my
purpose.

you can get a long way in C++ without needing arrays.
 
I

I V

Jorgen Grahn said:
[Java] has a garbage collector that makes memory management easier.
And resource management harder. Some people want GC, but many (most) do
not miss it in everyday C++ programming.

I do not see how the memory manager makes the management of other
resources harder.

Java does not have language constructs for scope guards (RAII), but
this does not seem to be caused by the existence of a memory manager.
One can imagine a language with both a memory manager and objects on
the stack that are being destructed when the scope is being exited.

I believe C++/CLI is just such a language:

http://en.wikipedia.org/wiki/C++/CLI#Finalizers_and_automatic_variables

And other languages have explicit scoping constructs that allow for a
limited form of RAII, e.g. (Python):

with open(filename) as f:
content = f.read()
# f.close() gets called automatically on leaving this scope.
 
P

peter koch

I don't care about it being easy (anything easy isn't worth doing). I
do want it to be 'doable;' in that, after putting in time and effort,
I understand what I am doing. I am a student, but not in any field of
programming (this, tennis, and baseball are the activities I dedicate
my free time to).

I don't understand the USE of pointers, and arrays just baffle me. The
loops and most statements make sense, however. Perhaps some hidden
functionality lies in those two that is keeping me from achieving my
purpose.

You might not need those in the beginning. I recommend that you learn C
++ by reading Koenigs book on C++ programming. I don't remember the
title and have only skimmed it, but it looked very good and has been
recommended from many sides.

/Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top