Guide | How To C++ tutorial (Malwareup)

The associated guide may contain user-generated or external content.

new user

New Member
Thread author
Jun 18, 2011
77
Here is a tutorial I have created on Malwareup. It is a stick in the Programming/Coding Discussion over there. I might as well post it here.

Original Thread: http://malwareup.org/viewtopic.php?f=40&t=2068
MalwareUp Programming Board (Check this out if you are interested in Programming): http://malwareup.org/viewforum.php?f=40


Below this line, it is copy and pasted from MalwareUp
------------------------------------------------------------
I am going to start a Basic tutorial Series for C++.
It will begin with Just Hello World, but unlike other tutorials I will explain what every Line of Code does.
Writing/using code you don't know is frankly, stupid and lazy.


I will be assuming you are using the QT SDK. On Linux any C++ IDE(or a plain text editor if you know what you are doing) will do. On Windows/OSX, use QT SDK.
QT is a Language Framework. It has libraries and code you can re-use (under terms of LGPL). There are lots of stuff added upon normal C++ (if you include relevant headers). QT Projects Only Compile in QT SDK (well I think, it uses gcc and qmake, you probably can without the sdk but for non-linux users its best to avoid compiling outside the SDK). If you are using QT libraries and functions.

Do not use Visual C++; it is alright for certain Windows specific cases, but for the most part stay away from visual anything.
http://qt.nokia.com/
There are many links at the end that you may want to check out.


Capitals make a difference in your code, cout and COUT are completely different!

Linux only:
Terminal Commands also are affected by capitals, su and SU are not the same!
If you use a Debian/Ubuntu system, run this command first:
Code:
sudo apt-get install build-essential
(For other distros, if this package does not exist, look for gcc and gdb)
Then get the SDK (do not get from package manager, may be outdated), http://qt.nokia.com/
To run the .bin file, right click, go to properties, and on the permissions tab check the box to make it executable. Then open a terminal, cd into the directory of the file and type:
Code:
./NameOfFileGoesHereCapitalLettersMatter.bin
Go through the install.
End Linux Only

On Windows/OSX just double click the installer.

Make sure your system is fully up-to-date.

Lets begin.

If you are using QT, make sure you have a folder named projects somewhere. This is where all your programming stuff will go.

Open QT Creator. At the start, you get a welcome screen, under the QT logo You have 3 tabs: Getting Started, Develop, and News&Support.
Leave News&Support. On the Develop Tab you have a list of Your recent projects, you can click on them to load.
What we want to do is click New Project. In the Pop-Up, choose QT C++ Application. Then QT Console Application. Hit the Choose Button. For the name we will call it "projectOne" (remove quotes). For the Create In part, navigate to the Projects folder you made earlier. Check the Box to use as default. Hit next. On this part, leave add to version control and the other option as <none> and hit Finish. You will get a Popup about the QT version. You should only have one Option under desktop that is checked. Hit Finish.

You will be taken to a Window Showing some code.
Code:
#include <QtCore/QCoreApplication>

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

    return a.exec();
}

Delete all of that code.

Put this in instead:
Code:
#include <iostream>
//This is a Comment In your Code
/*This
is also a different type of comment
*/
using namespace std;
int main ()
{
    cout << "Hello World!"<< endl;
    cout << "Press Enter to Continue" << endl;
    cin.ignore();
    cin.get();
    return 0;
}//end main

Compile (Build) the program (hammer symbol).

When it is done, do not run it (run is green arrow).

The Compiler has given you a binary. Now go to that projects folder we made. There will be two more folders inside it.
One will the whatever your project name is.
The other will be called nameofproject-build-desktop
Go inside the nameofproject-build-desktop
There will be 3 files. They are: main.o, Makefile, projectname
The file that is your project name is an executable.
On windows Double click. On linux, because it is a console app, open a terminal and navigate to directory. Run
Code:
./filename
.
You will get Hello World! on your screen, and be asked to press enter to continue. Do so.
Now we could have run this app from the green arrow in qt creator, but Qt has issues and bugs with console applications, since it does not open a console, but run it within the IDE.
Now you can exit out of the Project Folder.

You have successfully built your first C++ program!
Now here is were the real purpose of my tutorial kicks in.
What did all that code mean?

Lets go over it line-by-line.

Code:
#include <iostream>
That Includes the iostream header file. Header files are files that contain functions and other tools for the language.
For example: cin.get(); is a function
exit(0); is a function, it is not used here,but you will get a compiler error without including the cstdlib header!
You can right your own functions, but some functions that look simple are many many lines of code. A header file is basically a file containing a bunch of functions written for you. You even write your own headers!

Now the next couple of lines are special.
They are comments.
Comments are notes, or information you write, that do not affect the program.
In QT, comments are coloured green.
Everything after // for that one line is a comment
Everything after /* is a comment, for the whole program, until you end it with */


Code:
using namespace std;
Here is the best explanation I could find:
Explained as simply as possible, namespaces allows us to group a set of global classes, objects and/or functions under a name. If you specify using namespace std then you don't have to put std:: throughout your code. The program will know to look in the std library to find the object. Namespace std contains all the classes, objects and functions of the standard C++ library.
Found that here: http://forums.devshed.com/showpost.php?p=193617&postcount=2

Code:
int main()
and
Code:
return 0;
(I know return 0; is at the end)
Starts the main function. Main is a function found in every program, it is the main part of the program.
int is the function return type. At the end of a function, a value is returned. int means integer, the return (return 0; at the end) is an integer. The return value can indicate different things. return 0; indicates to the OS that the execution ended successfully.

Code:
{
Beginning of a Code Block. End is
Code:
}
Code Blocks are chunks of code. We will get into this more later. Eventually you will be using multiple code blocks.

Code:
cout << "Hello World!"<< endl;
cout is a stream used to display output to the default location. (Console).
After cout we put << and then what we want to output in quotes "". Then we put << endl; The endl; clears the stream, well get into that later, but it also functions like pressing enter in a text editor.

Code:
cin.ignore();
I do not want to go into detail. But later we will be taking input and storing them in variables. When the user press the enter key, some times that enter key is "picked up" by the cin.get(); we use to "pause" or stop the program. This gets rid of that. It is just good habit to put it before cin.get();


Code:
cin.get();
This grabs input. Much like the cin statement we will later use. Since we are putting no arguments, it will not do anything with the input that is given. Just a handy way of pausing, since the program will stop to wait for input.


Code:
return 0;
Already covered.
Go to where int main() is covered.


Thanks For Reading Part One!

Tips:
Never use:
Code:
void main()
Instead of that use
Code:
int main()
void main() is non-standard, only microsoft's compiler even allows it!

Never use:
Code:
system("pause");
It is non-portable. Only works on windows. The cout << "Press Enter" << endl; and cin.get(); lines accomplish what that does.

Other Resources:

http://developer.qt.nokia.com/
http://doc.qt.nokia.com/latest/tutorials.html
http://www.cplusplus.com/doc/tutorial/
 

Dejan

New Member
Mar 3, 2011
559
RE: MalwareUp Cross post: C++ tutorial

I kind of gave up on the idea of learning C++, mostly because it seemed to difficult for someone who hasn't done any coding yet, but I might become interested again in programming, this tutorial might help me :)
 

About us

  • MalwareTips is a community-driven platform providing the latest information and resources on malware and cyber threats. Our team of experienced professionals and passionate volunteers work to keep the internet safe and secure. We provide accurate, up-to-date information and strive to build a strong and supportive community dedicated to cybersecurity.

User Menu

Follow us

Follow us on Facebook or Twitter to know first about the latest cybersecurity incidents and malware threats.

Top