In summary, dduardo provides a summary of the content in the following conversation. He also provides a suggestion for clarification.
  • #1
dduardo
Staff Emeritus
1,906
3
Contents
1. Getting a C++ Compiler and Compiling Your First Program
2. Simple Datatypes and Declarations
3. Operators and Expressions
4. Input and Output (I/O)
5. Selection Statements
6. Iterative Statements
7. Arrays
8. Functions
1. Getting a C++ Compiler and Compiling Your First Program
Getting a C++ Compiler
For Windows users, especially beginners, I suggest getting an integrated development environment (IDE) for doing your work. You will get syntax highlighting, debugging, and compiling all in one package. For these tutorials, I suggest you use Bloodshed’s Dev-C++. It is a free IDE that has a compiler based on GCC. You can get it here:
http://www.bloodshed.net/devcpp.html
I’ve just tried the latest beta on windows and it works. If you want to run a different compiler or IDE that is fine, but it would be easier if you all stick to one type of environment for windows. I’ve also...

Continue reading...
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
Please post your C++ questions or comments here for Tutorial 1.
 
  • #3
dduardo said:
Please post your C++ questions or comments here for Tutorial 1.
Man!
What do you mean by tutoriel ? hmm ?
 
  • #4
Look at the sticky below this thread. I'm in the process of writing up the first part. I will post it very soon.
 
  • #5
Sorry I didn't read your notice post right above before giving a comment,
OK, seriously, the topics you listed gave me an impression of my first days in college, biting my nails and my pencil from time to time listening to my Delphi teachers trying to explain what was necessary to become a programmer, because I was starting to learn programming languages with Delphi and Basic, although their syntax and structures have been utterly erased from my memmory. What a Cool tutor you are, I have to say that!
Before introducing about array, or perhaps while you are writing about it, i also think if pointer is important enough to be included, to be digged as deeply as possible.
I think conditional statements and iterative or control statements should also dig dig diged deeply too, especially should give students some ideas of how to use if/else effectively as possible since I believe these things will be mentioned more in their future (thisz juts my personal idea). Those who hav some basic skills in progremming will suun grasp the control statements, but handling problems with those are again letthem go back to how to correctly use iterators and operators.
Ok, frankly I'm sleepy now, I can only give some *rough* draft/ideas or acutlay nothing, hoep some piple will join into make htis much interesting and more *beautiful*.
Finaly, Stick with the Standard and update infor all the time, yor tutoriels'll look as handsome as Mangan does! :D
+Nec

*I guess :shy: there're some thinkings taht I am Mangan :shy:, byt it only happens after i see my doctor and ask her to change my all :shy:*

(tomorrow or acouple of days later, I will try to write more about what i think)
Sorry for short and quite menaingless comments-if they turn out to be so.
 
Last edited:
  • #6
Nec, yes, I will give plenty of real world examples. What I'll do is revise and add more content to my previous postings for those who are still have difficulty while still creating new tutorials so people who already have basic programming skills don't feel like the course is going slow.
 
  • #7
hey dduardo this is a really cool idea. i enjoyed it from since the java classs was implemented and seeing c++ i was amazed because 1. i recently started the language and 2. i use dev c++ and that's a featured program in your tutorial so I just like to let you know that what you are doing is really appreciated. its like going to school on the web.
 
  • #8
Very good dduardo! :smile: :smile:
A good resource I can learn something from :approve:
 
  • #9
Hey, first I was want to say good job, It's nice to see somebody who's willing to input the effort and time to help others.

Anyways...
If I had to suggest something... I would ask you to clarify your code examples a little.
like this excerpt...

int x = 1, y = 3, z = 4;
int *num;
const float pi = 3.14;
z*=y;
y = x + (int)pi;
num = &y;

if you could go into a tad more detail I'm sure that I and others would appreciate it. I know that playing around with it is a good way to learn, but I'm having a hard time understanding what's going on there and I just think it would help if you gave a little push.

Other than that though, great tutorial and I'm looking forward to the next parts.
 
  • #10
Hey this is great. I'm just about to take a c++ class this summer. Great timing dduardo
 
  • #11
hey i got a question..

suppose class Foo has function returnMe. it has no variables (meaning returnMe() without any arguments)

Foo x;

x.returnMe();
this should return an OBJECT x. So basically it just returns itself.

How do i define Foo :: returnMe() { } ??
 
  • #12
00XeRo, will do. I have been a little busy with my own software project, so updating has been a little slow.

*****Advanced C++ Below. It has nothing to do with the tutorial*****

cronxeh if you want a class to return itself use the this pointer. The this pointer is a c++ keyword. Here is an example of how it works:

Code:
#include <iostream>

using namespace std;

class foo {

public:

foo* returnme(void);

};

foo* foo::returnme(void) {
return this;
}

int main( int argc, char *argv[] ) {

foo x;
foo *y;

y = x.returnme();

cout << y << endl;
cout << &x << endl;

return 0;
}

If you run this program you will see that y points to the base address of x. Therefore y is x.
 
Last edited:
  • #13
I always wonderd what the arguments were that are inside the main meathod

int main( int argc , char **argv )

what are int argc, char **argv?
 
  • #14
The arguments inside of main are used to store command line arguments. For instances:

ping -A localhost

"ping" is the program name and "-A localhost" are the arguments. The environment your in will pass the argument part to your program. Argc or Argument Count will tell you that there are 2 arguments - "-A" and "localhost". Argv or Argument Value is an array of pointers (or a pointer to a pointer) that stores the actual agrument strings.

int main( int argc, char **argv) is equivalent to int main( int argc, char *argv[] )

[edit] gnome, your right, it is an array of pointers. I should make a point about that subtle distinction in my tutorial.

An array of pointers:
int *array[];

A pointer to an array
int (*array)[];
 
Last edited:
  • #15
/*They are there to allow you to pass parameters into the program from the command line.

argv[] is an array of pointers to your parameters; the parameters are stored as character strings.

argc is the number of parameters.

argv[0] always points to the name of the program, so argc is always >= 1

For example, here is a program that prints "hello" followed by whatever string the user types after the program name at the command line, or just "hello." if no parameters are supplied:
*/

#include<iostream>
using namespace std;

int main(int argc, char *argv[]){

cout << "hello";

if (argc>1)
cout << ",";

for (int i=1; i < argc; i++)
cout << " " << argv;

cout << "." << endl;

return 0;

}
 
  • #16
ok, apparently I was supposed to know C before taking this class. My teacher wants me to use a header file. What exactly do I put in a header file and could you show me an example?
 
  • #17
When you want to include a header in a file you should write something like this:
#include "myheader.h"

The header file should contain something like this:

#ifndef MYHEADER_H
#define MYHEADER_H

//CODE

#endif
 
  • #18
what type of code would I put in there?
sorry I'm so dense
 
  • #19
In C++ typically you would put a class definition in a header file (.h) -- just the declaration of the class, containing a bunch of one-line declarations of all of the methods and data members of the class.

Then, the code to implement all of the methods goes in an implementation file (.cpp).
 
  • #20
DarkAnt, the code you put there are your functions.
 
  • #21
hey dduardo when you are finished with the main topics are you going to give a tutorial on using some gui toolkit or so?
 
  • #22
Which gui toolkit do you want? FLTK, GTK, QT?
 
  • #23
i have wxwindows and fltk added to dev cpp but i just can't seem to find a download for GTK with dev cpp. Its not like i want to learn them all I was just experimenting to see which one i should go with so that later on i don't regret choosing one then having to make a switch. It would be nice to know which one you think is best? Do u do any gui by the way?
 
  • #24
dduardo said:
DarkAnt, the code you put there are your functions.

Would that be something to enable your mathematical functions such as square root, etc.?

Not that making your own isn't fun. Check this out - a mathematical transmission. Newton-Raphson doesn't work so well at extremely low or extremely high "rpms". Pushing the significant digits into Newton-Raphson's most effective range and tracking the powers of ten separately makes this run a lot better, especially for those problem numbers like "What's the square root of 1.01?" which converge somewhere between here and eternity.

Using recipricals for numbers less than one makes things a lot easier to deal with, as well (like that annoying divide by zero problem when you try to find the square root of .5)

Being able to find the square root on three sticks of bamboo slapped together 50 years ago easier than my program could got so frustrating I decided to just give in and borrow a couple slide rule tricks. :approve:


//Name: Bob
//Filename: squareroot.cpp

#include<iostream.h>

void main()

{
//variables
double my_number=-1, temp_number, difference_number;
int count,recip=0,napier=0;
const int max_count=25;
const double tolerance=.000001;


//input (no negatives allowed)
while(my_number<0)
{
cout <<"Input number: ";
cin >>my_number;
if(my_number<0)
{
cout <<"You must enter a positive number!" <<endl;
}
else
{
}
}
cout <<endl;

//pre-processing (if number less than one, use reciprical)
if(my_number<1)
{
recip=1;
my_number=1/my_number;
}
else
{
}

//pre-processing (Napier's method to speed processing)
if(my_number<10)
{
my_number=my_number *100;
napier=-1;
}
else
{
while(my_number>1000)
{
my_number=my_number/100;
napier++;
}
}

//processing (Newton-Raphson method)
count=0;
temp_number=my_number;
while(count<max_count)
{
difference_number=my_number-temp_number*temp_number;
if(difference_number<tolerance && difference_number * (-1) < tolerance)
{
break;
}
else
{
temp_number=temp_number-difference_number/(1-2*temp_number);
}
count++;
}

//post-processing (undo Napier's)
if(napier==-1)
{
temp_number=temp_number/10;
my_number=my_number/100;
}
else
{
while(napier>0)
{
my_number=my_number*100;
temp_number=temp_number*10;
napier--;
}
}


//post-processing (if reciprical was used, flip back to original)
if(recip==1)
{
temp_number=1/temp_number;
my_number=1/my_number;
}
else
{
}

//output
if(difference_number<tolerance && difference_number * (-1)<tolerance)
{
cout << endl << "Square root of " <<my_number <<" is: " <<temp_number <<endl;
}
else
{
cout << endl << "Routine failed to converge! " <<temp_number << endl;
}

return;
}
 
  • #25
Sorry Bob, I didn't mean to be that impolite anyway, :wink: :biggrin:, Hello !
 
Last edited:
  • #26
Alek said:
I dis- :approve:
Your coding style I have to say is really really :biggrin:

Can you please change the dot h into no-dot-h ?
The Standard tells me that I have to return an int in main-function instead of void.
I think so,...-lol-
Over!
 
  • #27
cipher said:
hey dduardo when you are finished with the main topics are you going to give a tutorial on using some gui toolkit or so?
If you could give all members tutorials in biology ? :biggrin:

I don't know why no one thinks up some tutorials in LISP ? I am learning, it is used a lot in PR, NN.
It is so kind of you to give people this tutorial anyway.
Thank DDuardo :biggrin:
 
  • #28
Alek said:
Sorry Bob, I didn't mean to be that impolite anyway, :wink: :biggrin:, Hello !

That's okay. I know a little Basic, but C++ is completely new to me.

I already know the iostream can go either way (evidently, different compilers handle it differently and our teacher warned us that we might have to drop the .h if we used this at home on a different compiler).

Totally clueless on the main function (we've only had two classes, so far).

At least starting to get a feel for having to live without any line numbers.
 
  • #29
[c++] somthing about job scheduling

hello everyone,

i would like to have a function to Minimize the average waiting time of N print jobs using c++.
i am thinking of using heap class to build it
i have read a book and it has the follow idea.

the first line is the number of job needed to be do
subsequent lines - arrival time, length of job

sample input 1
6
0 7
0 2
0 1
5 10
6 3
8 5
Average waiting time : 4.33
However i don't know how to calculate the waiting time out and how to start
can anyone teach me how to do that , thanks all :smile:
 
  • #30
jwyw0813, a greedy algorithm should work. If you are not familiar with "greedy": search "\"job schedule\" greedy" on google.

A quick idea is: add jobs to the heap in non-descending order of their starting time and use as ordering criteria for the heap their end times. I have omitted some details and, more important, I haven't proved this works -- and neither tried it, so be sure to take the info in this paragraph with a grain of salt.

As for the heap.. If you are writing the program in C++ then you might be better of using std::set instead of a heap (because it is readily available from the standard library). In Java you have TreeSet. In C# you have SortedList (this might be slower...),. In OCaml you have Set, etc. In short you can use a set-like DS, which is usually implemented as a balanced search tree, instead of the heap, simply because heap implementations are not readily available in most standard libraries.
 
  • #31
I've been programming in C++ for around four to five years right now, and have to say I wish I had a tutorial like that when I started.

You might really want to teach Hungarian notation and standard naming conventions though.
Err, you know:

methods: methodNameHere()
constant: CONSTANT_HERE,
integer: iNum1, iNum2,
float: fNum1, fNum2

I say this because it really helps make the code neat and simple to read and understand.

Also, for anyone else interested, I would highly recommend the Deitel and Deitel book, C++ How to Program. Hands down one of the best books for beginners.

EDIT: Might also want to include a blurb on http://bloodshed.net, in my opinion the best free IDE for C++ there is. IDEs generally make the process of learning how to code from scratch much easier than working with commandline executed compilers. Plus, syntax hilighting is nice. :)
 
Last edited by a moderator:
  • #32
Hungarian notation is an historical one. Everybody agrees these days that it is obsolate (see Java or .NET coding standards for example.

As for the book, just look at:
http://www.accu.org/cgi-bin/accu/rvout.cgi?from=0au_d&file=cp003204a

The conclusion: "Do not buy this book, and if someone tries to give you a copy decline, politely, then firmly."

If you want good books look for:
Bjarne Stroustrup, The C++ Langauge (the "c++ bible" by the creator of the language)
Effective C++ & More Effective C++, Scott Meyers (nice idioms)
Design Patterns, Gamma et. al (designing big applications)
Modern C++ Design, Andrei Alexandrescu (advanced _language_ usage, very heavy on templates)

In general, when you consider buying a book make a visit to www.accu.org to see at least one authorized opinion.
 
Last edited by a moderator:
  • #33
I was never a fan of Hungarian notation. However if you have to edit source code for a Win32 program you might come across it, so you should at least be aware of what it is.

I've never read the Deitel & Deitel book on C++ (I have their book on Java, it's OK), but to imply that a book for beginners is in the same class as an exhaustive C++ reference book (the one by Stroustrup) and an advanced text on OO design for software professionals and other advanced users is unreasonable. Those are good books but not suitable for those without substantial experience (either with C++ or OOP in general). And there's a lot to learn in C++.
 
Last edited:
  • #34
hatefilledmind said:
Those are good books but not suitable for those without substantial experience (either with C++ or OOP in general). And there's a lot to learn in C++.
I've read C++ How to Program from Deitel & Deitel, and it is excellent for beginners. It's not just learning C++ but learning programming concepts from the beginning in most structured way. The treatment of structured programming especially is great. I think it is useful for professionals as well who need to consolidate the messy knowledge they have in mind, and to write clearer programs.
I just would like to read a book for MFC with C++ but I'm not finding an appropriate one.
 
  • #35
rgrig said:
Design Patterns, Gamma et. al (designing big applications)
This is a classically important book of course, but if you ever read that book you'd understand that it is not for learning C++. How could a beginner understand what are patterns whithout knowing about design, or worse about programming basics.
 
Back
Top