Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Object Oriented Programming Assignment # 02: Submitted To

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

MTE-203

OBJECT ORIENTED PROGRAMMING


Assignment # 02
Submitted to:
Fadia Sohail
Submitted by:
Ibtsaam Elahi
Registration no:
UW-20-MTS-BSC-008
Submission date:
29th Oct,2021
Department of Mechatronics Engineering
Wah Engineering College
Wah Cantt

Ibtsaam Elahi UW-20-MTS-BSC-008


(Question # 01)
What are Pointers? How they are implemented in Programming.

Pointers:
Pointer is a variable in C++ that holds the address of another variable. They
have data type just like variables, for example an integer type pointer can hold the address of
an integer variable and an character type pointer can hold the address of char variable. Dealing
with these memory addresses in programming is done by pointers. Pointers are extremely
powerful programming tool that can make things easier and help to increase the efficiency of
a program and allow programmers to handle an unlimited amount of data. It is possible for
pointers to dynamically allocate memory, where programmers don't have to worry about how
much memory they will need to assign for each task, which cannot be done/performed without
the concept of a pointer.

Syntax of pointer
data_type *pointer_name;

❖ Implementation of pointers:
There are some important points to implement pointers:
➢ Define a pointer variable
➢ Assigning the address of a variable to a pointer using unary operator (&) which returns the
address of that variable.
➢ Accessing the value stored in the address using unary operator (*) which returns the value
of the variable located at the address specified by its operand.

Benefits of using pointers in programming

➢ Pointers save the memory.


➢ Pointers reduce the length and complexity of a program.
➢ Pointers allow passing of arrays and strings to functions more efficiently.
➢ Pointers make possible to return more than one value from the function.
➢ Pointers increase the processing speed.

Ibtsaam Elahi UW-20-MTS-BSC-008


(Question # 02)
Write a program that defines the use of pointers with Structures and
Functions. Also mention the output.
In this program I will define a pointer in structure and then make object of structure in main
function. And do operation in any user define function. So

❖ Source code:
#include<iostream>
using namespace std;
int lareg(int,int);
struct larg{
int *large;
int *xptr;
int *yptr;
};
int main()
{
larg r;
int x,y;
cout<<"Enter the value of
x :";
cin >> x;
cout<<"Enter the value of
y :";
cin>> y;
r.xptr=&x;
r.yptr=&y;
lareg(x,y);
}
int lareg(int a,int b)
{
int large;
if(a>b)
large=a;
else
large=b;
cout<<"The largest
nmuber is :" <<large;
}

Ibtsaam Elahi UW-20-MTS-BSC-008


❖ Output Window:

Thanks

Ibtsaam Elahi UW-20-MTS-BSC-008

You might also like