![]() |
| | Home | Ink Cartridges | Collectibles/Toys | Computer Books | Geeks Electronics | EMagazines | AboutUs | |
Up to 87% savings on Geeks.com!
SECRET CAMCORDER
Prices too low to show! Expires 8/31
This sale includes 22 camcorders from Sony, JVC, Samsung, and Canon!
Take 10% Off All MCM Electronics Purcases!
HP Store search box: Computers, Electronics, Hardware |
![]()
![]()
![]()
By Sergey Skudaev
In this tutorial you will learn how to write a simple source code or computer program in C++. Besides, you will get knowledge that is necessary to any programming language. There are many computer languages you can use to write computer program. If human languages are very different, computer languages are very similar. You, probably, will spend one-two years to learn the first computer language. The second one you will learn for couple of month or even less.
Computer programming is fun. Create a world from a word in Java or C++. A world that is logical, fair and predictable that is everything what the real world is not.
To learn computer programming you need to understand few things:
1. What is variable, and what is data type?
2. How to understand function?
3. What is the difference between passing parameters by reference and by value?
4. How to understand pointer?
5. What is variable scope?
What is object oriented programming?
6. What is class?
7. What is inheritance?
8. What is polymorphism?
How to understand what computer program is? Have you ever read a screenplay? If you did, you know that first the author introduces a list of characters:
John Wilson - Web Developer
Joanne Wilson - his wife
Robert Gray - hacker
Then author shows characters in actions and we expect what each character will do. As screenplay develops we watch a sequence of scenes in which characters act.
A computer program has the same structure.. First, programmer declares variables:
account_number integer
Balance double
Minimum_payment float
last name string
Expiration_date date
A variable name in any computer language must be one word. Often programmer use prefix to show variable data type. For example: strName for string data type or intAccount for integer data type.
Integer, double, string and date are data types. Each data type required different "space" in computer memory. For example integer occupy 4 bytes. Byte made of 8 bits. Bit is a smallest unit of information. It may contain zero or one. In binary system 00000001 equals 1. 00000010 equals 2, 11111111 equals 256 i.e. 2 in power of 8. 4 bytes can hold number from -2,147,483,648 to + 2,147,483,647. Double occupy 8 bytes and can hold huge number with decimals. Float occupies 4 bytes and can hold a number with decimals. When you prepare a gift box, you will not choose yard-by-yard box for diamond ring. It is not efficient way of packaging. The same thing with variables. You will declare data types according to you needs, so that computer memory will be not wasted.
There is also type of variables that hold not a value it-self, but an address where this value is stored. This type of variables is called references or pointers. What is address? Run this simple code for illustration.
#include<iostream.h>
int main(void)
{
int a = 5; //an integer a
int *b = &a; //pointer to address of a
*b=10;
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
return 0;
}
Command prompt window displays:
a=10
b=0x0065FDF4
Press any key to continue
Why output displays that a=10? In the first line of code we assign 5 to "a" variable. Because in line *b=10 we assigned 10 to "b" pointer. Since "b" holds the address of variable "a", system assign 10 to the variable, which address is "b" (0x0065FDF4)
In header file in switchnumbers.h type:
void switch_by_reference (int &x, int &y);
void switch_by_value(int x, int y);
In switchnumbers.cpp file type:
#include<iostream.h>
#include "switchnumber.h"
void switch_by_reference (int &k, int &n)
{
int temp=0;
temp=k;
k=n;
n=temp;
cout<<"Values inside switch_by_reference function:"<<endl;
cout<<"k="<<k<<endl;
cout<<"n="<<n<<endl;
}
void switch_by_value(int x, int y)
{
int temp=0;
temp=x;
x=y;
y=temp;
cout<<"Values inside switch_by_value function:"<<endl;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
}
In main.cpp file type:
#include<iostream.h>
#include "switchnumber.h"
int main(void)
{
int a = 5;
int b = 9;
cout<<"Initial values:"<<endl;
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
cout<<"Call switch by reference function..."<<endl;
switch_by_reference(a,b);
cout<<"after switch by reference:"<<endl;
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
cout<<"Call switch by value function..."<<endl;
switch_by_value(a,b);
cout<<"after switch by value:"<<endl;
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
return 0;
}
Run this code and you will get output:
Initial values:
a=5
b=9
Call switch by reference function...
Values inside switch_by_reference function:
k=9
n=5
after switch by reference:
a=9
b=5
Call switch by value function...
Values inside switch_by_value function:
x=5
y=9
after switch by value:
a=9
b=5
Press any key to continue
When you pass parameters by reference, you pass parameters (a and b) addresses and system assign new values to addresses. As a result the values of the initial variables (a and b) are switched.
When you pass parameters by value, system copies parameters (a and b) values to function local variables (x and y). As a result local variable values ( x and y ) inside the function are changed, but initial variables (a and b) that were declared outside the function are not changed. Before calling switch by value function "a" was equal 9 and "b" was equal 5. After this call, "a" still equals 9 and "b" still equals 5.
It is very important to understand the difference between passing parameters by reference and passing passings by value.
The following C++ code example includes declaration of message variable inside Greeting class, inside PrintMessage method of the class and inside the main function. In each place message variable is assigned different values. Inside class message="Hello World\n", Inside PrintMessage function message="How are you?" and inside main function message="Hi There". When you execute the program, command prompt window display: "How are you?" because class level variable is not visible inside the PrintMessage function in scope of the local variable with the same name. A variable inside the main function and outside the class is most global and therefore it is not visible as well.
If you comment the line message="How are you?", compile and execute the code again command prompt display "Hello World" Now class level variable becomes visible. The variable message inside the main function and outside the class is more global than class variable and is not visible inside the class in scope of class variable with the same name.It is visible outside the class. If you copy the line
cout<<message<<endl;
inside the main function like that:
int main(int argc, char* argv[])
{
char* message="Hi There!";
Greeting gr;
gr.PrintMessage();
cout<<message<<endl;
return 0;
}
Compile and execute the code. Command prompt displays:
Hello World
Hi There!
"Hello World" is output of gr.PrintMessage() line and "Hi there" is ouput of cout<<message<<endl; line
.If you comment line message="Hello World\n"; inside the class constructor compile the code and try to execute it, program crashed because no value assigned to pointer char* message inside the class
There is the complete code:
#include<iostream.h>
class Greeting
{
public:
void PrintMessage();
Greeting();
private:
char* message;
};
Greeting::Greeting()
{
message = "Hello World\n";
}
void Greeting::PrintMessage()
{
char* message="How are you?";
cout<<message<<endl;
}
int main(int argc, char* argv[])
{
char* message="Hi There!";
Greeting gr;
gr.PrintMessage();
return 0;
}
Please rate the tutorial