Learning C++ / PDF (free)
Learning C++ / PDF (free)
Sample of the PDF document
Learning C/C++
|
Rules of thumb.
Here is a set of "rules" you might consider
while learning C++. As you get more proficient you can evolve them into
something suitable for your kind of applications and your style of
programming. They are deliberately very simple, so they lack detail. Don't
take them too literally. To write a good program takes intelligence, taste,
and patiance. You are not going to get it right the first time; experiment!
- When
you program, you create a concrete representation of the ideas in your
solution to some problem. Let the structure of the program reflect those
ideas as directly as possible:
- If
you can think of "it" as a separate idea, make it a class.
- if
you can think of "it" as a separate entity, make it an object
of some class.
- If
two classes have something significant in common, make the commonality
a base class
- If
a class is a container of objects, make it a template
- When
you define a class that does not implement a mathematical entity like a
matrix or a complex number, or a low-level such as a linked list:
- Don't
use global data
- Don't
use global (nonmember) functions.
- Don't
use public data memebers
- Don't
use friends, execpt to avoid 1,2, or 3.
- Don't
access data members pf another object directly.
- Don't
put a "type field" in a class; use virtual functions.
- Don't
use inline functions, except as a significant optimization.
|
Learning C/C++
Part One
|
Getting Started with C++.
I will show you a very small program and then explain how
it works.
/*A small program */
#include
void main(void)
{
cout << "Hello World\n";
}
This program will print Hello World to the screen.
You should from the very beginning learn to write comments of what is what,
because when you have a very long program and and you haven't looked at it
for month it is very easy to forget what the parts really do. There for you
have the /* and */ that marks the beginning and end of an comment.
The next statement:
#include
represents one of C's unique features, knows as a preprocessor statement.
A preprocessor statement is like a precompile instruction. In this case, the
statement instructs the compiler to retrieve the code stored in the
predefined iostream.h file into the source code on the line requested. (The iostream.h
file is called a header file).
Under the #include statement comes the main function:
void main(void)
{
.
.
.
}
If you wonder why you have to write void main(void), well
there is a very simple reason: You have too. The program must have a main function.
In this function there is a statement:
cout << "Hello World\n";
The first cout pronounced see-out is a C++ function like
in Basic print.
The notation << indicates that the proposition will
send the string to cout - the symbol points in the direction that the
information floats.
Everything you want to be send to the screen has to be
within " ".
The \n is a line feed and will generate a new line.
the ; indicates end of the string.
Exercises:
1. Make a program that says: Hi John Doe!.
2. Make a program that says:
I am John Doe
I really like lasagna.............
Learning C++ / PDF (free)
|
0 commentaires: