Archive

Archive for the ‘Programming’ Category

C++ Tutorial 4 – More on Namespaces

July 10th, 2010 Rishabh Dev No comments

To see an introduction to what namespaces are, go back to tutorial 3. Now, cout is a function in the namespace “std”. Now, there’s another way of using cout without specifying the namespace before hand.

#include<iostream>
int main(
{

a
std::cout<<"Hello Jolly!";
system("pause");
return 0;
}

Here, std::cout means the cout that comes from the std namespace. However, since we would eventually use a large number of output and input statements(ie cout’s and cin’s), we would usually declare the namespace beforehand.

Creating our own namespaces

We can also create our own namespaces. These may contain a number of variables and functions to be used for the particular namespace. We here create a namespace called “my” with a variable of integer type called x.

We will be going into variables and data types in a few tutorials, but here’s ow you create an integer variable called x-

int x;   //creates a variable called x
           //which takes integral values

So, here’s our own namespace-

namespace my{   //creating a namespace
int x;
}
int main()
{
my::x;         //using the namespace variable
}

If we intend to use our variable x a number of times, we could specify our namespace in the program.

using namespace my;  //declare namespace beforehand

3 people like this post.

Popularity: 5% [?]

C++ Tutorial 3 – Basic Program Structure

July 10th, 2010 Rishabh Dev No comments

So here’s our very first program in C++. We’re gonna take a look at it and then see what each line of code means.

#include<iostream>
using namespace std;

int main(
{
cout<<"Hello Jolly!";
system("pause");
return 0;
}

#include<iostream>

Here, we’re using the code already there in the file iostream in own program. So the compiler is told to get the code from iostream and compile it before moving on to compile the rest of the code. The “<” and “>” are used when the file is located in the default compiler bin and not in an external location. You can try making your own iostream file. Copy all code from iostream to any file, say phoenix.txt and use #include<phoenix.txt> and it’ll work.

using namespace std;

Before anything else, you need to know that all statements end with a semi-colon. So, this is your first C++ statement. This just means that you’re using a namespace called the standard library. Now, this is done to avoid name collisions. For instance, consider a university which has a student with roll number 70. Another university may also have a student with the same roll number. Now, these are two namespaces, and by specifying  which namespace we’re using, we can differentiate between the two students.

int main(){…}

All he shit that we do before this statement is just getting things ready and set for the main function. Did i say function? Yes, this is your first C++ function. Like any function, it must return some data. We use “int” here to show that the returned data would be an integer. Every function has pretty much the same format.

It goes like this-> return-type func-name (parameters){statements}

cout<<”Hello Jolly!”;

Now this is another C++ statement. If you have seen the output of the program using your compiler-which is what you should have done. You’ll only see Hello Jolly! printed on a stupid blank black screen. Well, there you go-the cout is your standard output function. Anything in the double inverts with the output operator “<<” goes as output to the screen. This function, along with the standard input function(cin) is stored in the “std” file which we talked about in the namespace statement.

system(”pause”);

A program like this one would usually run in a matter of nanoseconds and the window would close in a flash, unless mi friend-you use the above to simply, pause the system-causing it to wait until you exit.

return 0;

Did I say the main function in C++ would return an integer? So, here we return 0-nada-nothing-goose egg.

Again, the most important thing is to go and try it out for yourself.

[Go to Tutorial 4]

1 people like this post.

Popularity: 6% [?]

C++ Tutorial 1 – The Basics

July 10th, 2010 Rishabh Dev No comments

Hello world! I just thought maybe a bunch of C++ tutorial could help. So here they are. To start with, a program is a simply, a set of instructions for the computer to do. The programmer is he who makes the instructions. And the language in which they are written is the programming language.

Low-Level VS High-Level Language

Now, just like you, your computer has its own languages-called the low-level languages(these are the ones your computer knows but they’re hard for you to understand). Then there are the ones which resemble your language including words like if, else, while, for called the high-level languages(these are the ones you know but your computer does not).

THE COMPILER

Now, a compiler is just another program which makes your high-level languages understandable to your computer. It is a translator that translates the code written in a high-level language into assembly language(low-level). So, the first thing you need is the compiler. I use Dev C++. You can use whichever you like. (The most popular being Visual C++).

Programming PARADIGM

This is more theoretical and would not much really affect your coding skills, but anyhow-a paradigm is a style of programming. For instance, a procedural paradigm would concentrate on the sequence of the program, a generic paradigm would concentrate on generalization concepts like templates, etc. C++ is a multi-paradigm language.

[Go to Tutorial 2]

2 people like this post.

Popularity: 5% [?]

An Introduction to Pointers in C++ Programming

July 1st, 2010 Rishabh Dev No comments

A pointer, formally called a “derived data type” defines where to get the value of a data variable. It itself, does not store the data but the memory location of the variable.

So, in short, it is a (derived)data type which points to the data of the variable but stores only the memory location of the variable.

The pointer uses the deference(*) operator & the reference/address(&) operator. Some of the stuff we can do using pointers is shown down there.. ;)

int *p1;          /* all of these can be used
int* p2;                  to declare pointers */
int*p3;

int *ptr, a;      //declared pointer and variable
ptr=&a;           //pointer now contains the address of a

cout<<ptr;        //gives the memory location of a
cout<<*ptr;       //gives the value stored at a

It’s very important not to get confused between the &variable, pointer, and *pointer.

&variable is used to assign the pointer an address of the variable, pointer gives the memory location of the variable and *pointer gives the value of the variable.

3 people like this post.

Popularity: 5% [?]

Understanding Inheritance in OOP

October 4th, 2009 Rishabh Dev No comments

The example below explains inheritance, an important property of OOP languages.

We have three classes: living, animal & dog. The dog inherits all the characteristics of living & animal base classes however, the plant does not(being an instance of just the living class).

#include<iostream>
using namespace std;
class living
{
      int energy;
      public:
                void getenergy()
                {
                     cout<<"Gets energy"<<endl;
                     }
};
class animal:public living
{
      int feet;
      public:
                void move()
                {
                     cout<<"It moves"<<endl;
                     }
};
class dog:public animal
{ 

      int tail;
      public:
                void bark()
                {
                     cout<<"It barks"<<endl;
                     }
};
int main()
{
    living plant;
    plant.getenergy(); 

    dog phoenix;
    phoenix.getenergy();
    phoenix.move();
    phoenix.bark(); 

    system("pause");
    return 0;
}

The dog inherits the properties of animal which inherits the properties of living. Hence, this inheritance is an example of “Multi-level” inheritance.

7 people like this post.

Popularity: 2% [?]

Content Protected Using Blog Protector By: PcDrome.