OBJECT ORIENTED PROGRAMMING LAB
OBJECT ORIENTED
PROGRAMMING LAB
Lab journal -03
DESIGNED BY: ALI MUHAMMAD
Question:01
Write the programs given below:
1- What does the following program display? Comment each line what’s their output and
purpose?
*************************************
in t main ( ) {
in t value1 = 5 , value2 = 1 5 ;
in t * p1 , *p2 ;
p1=\&value1 ; //error p2 is not initialized
* p1 = 10 ;
*p2 = * p1 ;
p1 =p2 ;
* p1 =20;
cout << value1 ;
cout << value2 ;
return 0 ;
}
**************************************
CODE:
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int value1 = 5, value2 = 15; //declaring variables
int *p1; //declaring pointers
int *p2; //declaring pointers
p1 = &value1; //assigning address of value1 in p1
p2 = &value2; //assigning address of value 2 to p2
*p1 = 10; //changing value at "value1" variable through
p1 pointer
*p2 = *p1; //value of adress p1 to p2
p1 = p2; //changing addresses of p1 to p2
address
*p1 = 20; //giving value 20 to address stored in p1
cout << value1 << endl;
cout << value2;
_getch();
return 0;
}
Output:
1|Page
2- What does the following program display? Comment each line what’s their output and
purpose?
*************************************
int main ( )
{
int x [ 3 ] [ 4 ] , i , j ;
for ( i = 0 ; i < 3 ; + + i )
{
for ( j =0 ; j < 4 ; + + j )
{
x[i][j]=3*i+j;
cout <<x [ i ] [ j ] < <
"
"
;
}
cout <<endl ;
}
cout < < * ( * ( x + 2 ) + 1 ) ;
}
*************************************
2|Page
CODE:
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int x[3][4], i, j; //declaring array
for (i = 0; i < 3; ++i) //For loop that runs 3 times
{
for (j = 0; j < 4; ++j) //Nested For loop that runs 4 times
{
x[i][j] = 3 * i + j; //adding values to our array
cout << x[i][j] << " ";// display output
}
cout << endl;
}
cout << *(*(x + 2) + 1); //displaying values at address
_getch();
return 0;
}
Output:
3|Page
+++++++THE END+++++++++
4|Page