A Basic Pointer Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//*****************************************************************
//   In this example we introcuce pointer concept. It does not 
//explain everything but it is rather an introduction
//*****************************************************************
#include <cstdlib>
#include <iostream>
using namespace std;
// A method by using concept of pointer
int square(int *haha);
int main(int argc, char *argv[])
{
    // Simply pointers
    int num1 = 5;
    int *num2;//num2 is a pointer decleration
    //It is not a number but it is an adress of number
    //As the name implies it points the number which we want
    num2 = &num1;//num2 holds an adress thus we give the num1's
    // adress to the num2
    cout< <"num1 is "<<*num2<<endl;
    cout<<"If use it in a method "<<endl;
    cout<<"The square of num1 is "<<square(&num1)<<endl;
    cout<<"The num1 is "<<num1<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}
int square(int *haha){
    *haha = *haha * *haha;
    return *haha;    
}

(more…)

Type Casting

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//*********************************************************************
//We introduce casting
//*********************************************************************
public class TypeCasting{
    public static void main(String[] args){
        int mum1 = 4;
        double num2 = 5.56789;
	char ch = 'l';
	//If we change a double into integer what happens
	//We use  in order to cast a variable to other types
	System.out.println("The integer value of num2 is "+num2);
	//If we change a character into integer what happens
	//It will give the ASCII value of the character
	System.out.println("The integer value of ch is "+ch);
    }
}

Pass By Referance

//*******************************************************
// We introduce the concept of pass by referance 
// The difference between a method which is 
//implemented by using pass by referance and which is not
//*******************************************************
#include <cstdlib>
#include <iostream>
using namespace std;
//Defining a method which calculates the square of given number
int getSquare1(int num1);
int getSquare2(int &num2);
int main(int argc, char *argv[])
{
    // We define two integer for our two different getSquare method
    int num1 = 2,num2 = 2;
    cout< <"num1 was initially "<<num1<<endl;
    cout<<"num2 was initially "<<num2<<endl;
    // We call the methods
    cout<<"Square of num1 is "<<getSquare1(num1)<<endl;
    cout<<"Square of num2 is "<<getSquare2(num2)<<endl;
    //After calling square methods
    cout<<"num1 is "<<num1<<endl;
    cout<<"num2 is "<<num2<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}
int getSquare1(int num1){
    num1 = num1*num1;
    return num1;    
}
int getSquare2(int &num2){
    num2 = num2*num2;
    return num2;    
}
//************************************************************
//The difference between the methods is that the first one do not
//use the variable it creates a variable instead of the num1 but 
//the second method it uses the num2 since we send  adress of the 
//variable to the method so it uses num2 not creates a copy of num2.
//Thus, num2 changes.
//**************************************************************

(more…)

Sayfa: 11 | Toplam: 11« First...7891011