0% found this document useful (0 votes)
77 views5 pages

Solution 2 28122022 013134am

This document contains two programming questions related to object-oriented programming lab. 1. The first program displays the values 10, 10, 20 when the variables value1 and value2 are printed. It uses pointers to change and assign values between the variables. 2. The second program displays a 3x4 integer array with values calculated as 3i+j. It uses pointers to access and display the value at index [1][2] of the array.

Uploaded by

Nazia butt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views5 pages

Solution 2 28122022 013134am

This document contains two programming questions related to object-oriented programming lab. 1. The first program displays the values 10, 10, 20 when the variables value1 and value2 are printed. It uses pointers to change and assign values between the variables. 2. The second program displays a 3x4 integer array with values calculated as 3i+j. It uses pointers to access and display the value at index [1][2] of the array.

Uploaded by

Nazia butt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

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

You might also like