Recursion Explored

I read up on recursion a little this morning, and I realized that it is in essence, the fundamentals of all C++ loops :D

for example supposed we have:

void funcINeedToRepeatALot()
{
    for(int x; x > 3; x++) {
        cout << "Sam\t";
    }

}

and we need to repeat it a lot without a using a loop (‘cuz of overhead), you could always make a function that calls itself
so, supposing that:

bool aFuncThatIsSometimeTrueAndSometimesFalse()
{
    static unsigned short var = 0;
    ++var;

    if(var < 5 )
        return false;
    else return true;
}

we could creat a recursion like this:

void loop(bool (*conditionFunc)(void), void (*actionFunc)(void))
{
    if(conditionFunc)
        (*actionFunc)();
    else loop(conditionFunc, actionFunc);

}

and then you can call it on your own, with:

loop(&aFuncThatIsSometimeTrueAndSometimesFalse, &funcINeedToRepeatALot);

so now, you’ve built your own extenstion to your compiler. if you’re really into all this crap, you could actually modify your compiler’s source, add this manually and recompile your compiler and extend the C++ language

hope you like it :D

Making An Uncloseable Window in Java

Through Swing and the AWT, one can make easily make a window that won’t shut when the user tries to exit the application. Use setDefaultCloseOperation(); and pass it JFrame.DO_NOTHING_ON_CLOSE

import javax.swing.*;
import java.awt.*;

class app extends JFrame {
    app(String title, int height, int width)
    {
        super (title);
        setSize(width, height);
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        setVisible(true);
    }

}
class program {
    public static void main(String[] args) {
        app myApp = new app("Hello", 350, 750);
    }
}

C++ Done Right – Lesson 1: Hello World and the Basic C++ Program

If you successfully compiled the program in the last lesson, get ready to begin learning the fundamentals of the C++ language. If you didn’t get it to compile, don’t move on; go back and look for help.

Hello World in C++

As we saw in the last lesson, this is “Hello World” in C++:

#include <iostream>

using namespace std;

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

So we can understand all of this, we’ll break it up into small bits and explain piece by piece. First, take a look at this line:

#include <iostream>

This is commonly known as an include. What it does is it finds the file specified (in this case iostream) and when the program is compiled, it appends it to the “.CPP” file that called it. It’s like you took iostream and typed it right into your code!

You’ll also notice the “<” and the “>”. This is to tell include that it should look in a normal location for the file specified. If it is surrounded with ” and “, then it looks in the current directory.

Nest, we have:

using namespace std;

This line may not make a lot of sense right now, but it’s rather important. When you included iostream, you included a bunch of code. Well, a lot of that code is grouped together because it’s related. These groups are called namespaces. This line basically tells the compile your using one of these namespaces, in this case std.

Ok, quick terminology detour. A function is a piece of code that’s purpose is to execute a chuck of code you tell it. We’ll be looking at functions later, but it’s good to know what they are.

int main()

This line makes a function called main. This function is a special function. The compiler uses it as the “main” function in your program, and it’s job is to find and exectute all the code in your application.

cout << "Hello World";

This line obviously prints out “Hello World”. cout is the standard way to print output.

return 0;

This basically tells it that it should return nothing to the OS. Just about every single program will end with this

Conclusion

In this lesson we looked at the basic C++ program. Sorry if it’s not to well explained, it’s rather late here! In the next lesson we will be looking at variables

C++ Done Right – Introduction and Compiling You First Program

Welcome to C++ Done Right! Follow this tutorial with a little patience and you’ll be a happy confident C++ programmer in no time at all. It is a complete work in progress and is rather unstable if you will. Some sections are kinda short, others don’t exist yet. Lessons are prone to constant updates, and may have small typos and errors.

Who’s this tutorial for?

This tutorial is generally aimed at:

- Programmers from a language other than C++ that already know some programming concepts but want to learn C++
- People that have never programmed before, and want to get started with C++
- C++ programmers that want to refresh their memories, or need an explanatory reference

What’s covered in this tutorial?

The C++ language of course! To be more specific, we’ll be looking at:
- Variables
- Mathematics
- Functions
- Conditional Statements
- Loops
- Arrays
- Pointers
- References
- Classes
- Vectors
- Templates

These are concepts we look at, but some concepts have been broken into multiple lessons as to not overwhelm anybody.

Understanding Compilation and Interpretation

Before we start building our first program, we need to understand two terms. The first is compile, and the second isinterpret.

Compiling is when a program is translated from a high level easy to understand source code, to assembly and later binary code. It is then saved in an executable format and can be used when ever you need. Make sense? The first part of the tutorial explains it quite well:
http://www.freebsd.org/doc/en_US.ISO8859-1/books/developers-handbook/tools-compiling.html

Interpreting code is in a sense the same as compiling, but the code isn’t saved in a executable file. Instead, every time you use the code,
it is translated.

If you ever hear about JIT compilation, understand this is a mixture of both compiling and interpreting.

Generally compiled programs are faster, but interpreted programs can be executed sooner.

Compiling in C++

To compile, you’ll need a compiler, and in this tutorial we’ll be using the Visual C++ (VC++) compiler from Microsoft. A free version is available from:
http://www.microsoft.com/express/vc/

Now, since most of the people taking this tutorial are new to programming, we’ll be using an Integrated Development Environment(IDE). An IDE is an application that tries to put development tool and editing tools all in one place. It usually works with the compiler and debugger and comes with tons of stuff for your advantage.

Visual C++ is both a compiler and IDE, so install it.

Compiling Your First Program

Here, we are gonna compile a program. I won’t explain it, we’ll be doing that in the next lesson. Generally when programming you’re gonna wanna make a Project. A project is a way for the IDE to group all your different files in a single directory, and know where to out put the executable.

Open VC++, anf goto ‘File’ > ‘New’ > ‘Project’. Choose ‘Win32 Application’, and make it an ‘Empty’ project. Now from the ‘Projects’ menu, select ‘Add New Item’. Make a new .CPP file and add the following code:

#include <iostream>

using namespace std;

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

Press F7 to compile, or F5 to run in the debugger. If all went well you compiled your first program in C++!

Conclusion

In this lesson we installed a compiler and IDE as well as compiles a program. Next time we’ll take our real first steps into the language by deciphering the little snippet you just put together.

Friday’s Book: Hacking: The Art of Exploitation, 2nd Edition (Paperback)

Ok everybody, I’ve decided to start a new thing on here; every Friday, I’ll feature a book that I like, and review it a little.

This week, we have Hacking: The Art of Exploitation, 2nd Edition (Paperback)

From Amazon.com:
Hacking is the art of creative problem solving, whether that means finding an unconventional solution to a difficult problem or exploiting holes in sloppy programming. Many people call themselves hackers, but few have the strong technical foundation needed to really push the envelope.

Rather than merely showing how to run existing exploits, author Jon Erickson explains how arcane hacking techniques actually work. To share the art and science of hacking in a way that is accessible to everyone, Hacking: The Art of Exploitation, 2nd Edition introduces the fundamentals of C programming from a hacker’s perspective.

Now, I haven’t actually read all of this book, but I browsed it for quite a while at my local Barnes & Nobles, and I must I’m impressed.

This book is targeted at two things in general:
- Teaching how hacking is done, the art not the technique
- Hacking software in general (Hacking, not cracking).

So basically, if you wanna break that parental controls system, this is the book for you!!!

Are Macs Completley Secure Against Viruses???

The simple answer, is no. I looked into it a little, read me a good ammount of articles, and I formed my hypothesis. I’ll keep this short, and to simple points. Basically, Macs break down like this:

  • Macs are impervious to viruses targeted at Windows and Linux since programs for that Operating System are written with a system specific API. (Java may be an exception to all of this)
  • Macs are still vulnerable to viruses that are written specifically for that OS. It’s not like they can’t have viruses. They can and do get infected
  • Macs generally have more security than a Windows system because they are Unix based. This means there is only one root, and most users have limitations in what they can do. In Windows, unless the Administrator sets up the the system properley, most users run at the equivalent of an Admin. The security features of Mac are only a bump in the road for virus writers and hackers

Now, I bet you are wondering how I formed this idea (I know you are!!!).  Anyways, here are links to some great articles on the subject:

http://elliottback.com/wp/macs-dont-have-viruses/
http://www.smallblue-greenworld.co.uk/pages/macintosh.html

Follow

Get every new post delivered to your Inbox.