STACK

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

LAB3

Class & Pointers

Eng.Wafaa alghaliby
What is Class
The mechanism that allows you to combine data and the
function in a single unit is called a class.
-A class would be the data type
-An object would be the variable.
Defining Class:
class class_name
{
;private: Data Members or private function
;public: Data Members or public function
};
class Rectangle
{
:private
;float width, length
:public
void set_value(float w, float l)
{
;Width=w; length=l
}
int calcArea(void)
{
Return width* length
}
;}
;Rectangle c1, c2
;c1.set_value(2.5,2.4 )
Exercise 1
Create a class called student class that includes three attribute as
data members
student_name ,mark1,mark2,mark3

And Provide public functions:


1. Void Set_value(string n, float m1, float m2, float m3) :
for set data member
2. float Student_calculate() :
if one of each mark between (49-47) , set mark=50 then
calculate the average
3. void print () function
for display the studet ninformation
Pointers
What is a POINTER?

• A pointer variable contains an address. A pointer variable can be


declared as follows:
int     *ptr;
This declares ptr to be a (pointer) variable that can contain the
address of an integer variable.

Initialize pointers to 0, NULL, or an address


 0 or NULL – points to nothing (NULL preferred)

:Memory address
1020 1024 1032

… … 100 … 1024 …
Int a
 Pointer Variables
A pointer is also a variable, so it has its own memory address
int a = 100;
Result is:
int *p = &a; // 1024
100 1024
1024 1032
cout << a << " " << &a <<endl; 100

cout << p << " " << &p <<endl;


Cout<< *p
We can access to the value stored in the variable pointed to by using the
dereferencing operator (*), a 1024
100

P 1024

P& 1032
Exercise 1
Write a program that asks the user to enter integers as
inputs to be stored in the variables A and B
respectively. There are also two integer pointers names
ptrA and ptrB . Assign the values of A and B to ptrA
and ptrB respectively, and display them.

**Then swap between them by using pointers.


 
DYNAMIC MEMORY
ALLOCATION

· Memory is allocated for variables in two ways:

· Static (or at compile-time)


)integer variable x, Arrays(
· Dynamic (or at run-time)

Dynamic memory allocation is in contrast with this. Memory


gets allocated at the time of running the program and hence we
can use memory to exactly meet our needs.
DMA
New and Delete Operators
⁻ Instead of define an int variable (int number), and
assign the address of the variable to the int pointer (int
*pNumber = &number)

⁻ the storage can be dynamically allocated at runtime, via a


new operator.

⁻ The delete operator can only be used to delete data


allocated by a delet operator.
Object (variable) creation: New
Syntax
ptr = new SomeType;

where ptr is a pointer of type SomeType

Example
;int* p = new int

Uninitialized int variable

p
Object (variable) destruction: Delete
Syntax
delete p;
storage pointed to by p is returned to free store and p is now undefined

Example
;int* p = new int
;p = 10*
;delete p

p 10
Array of New: dynamic arrays

Syntax
P = new SomeType[Expression];
Where
 P is a pointer of type SomeType
 Expression is the number of objects to be constructed -- we
are making an array

Because of the flexible pointer syntax, P can be


considered to be an array
Example
Dynamic Memory Allocation
 Request for “unnamed” memory from the Operating System

 int *p, n=10; new


p = new int;
p
p = new int[100];

new

p
new
;p = new int[n] p
new[] and delete[] Operators
 Dynamic array is allocated at runtime rather than
compile-time, via the new[] operator. To remove the
storage, you need to use the delete[] operator (instead of
simply delete).
Exercise 2

By Using dynamic arrays, write a program that define


an array contains 10 integer elements , add 5 for each
elements in array ,then print the array elements.
Pointers as Function Parameters
A pointer can be used as a function parameter. It gives
the function access to the original argument, much like
a reference parameter does.
Function Header:
void fun_N(int * ptr)
Function call:
fun_N(& ptra)
#include<iostream>
using namespace std;

void sum(int *a,int *b)


{
cout<<*a+*b;
}
main()
{
int a,b;
cin>>a>>b;
sum(&a,&b);
}
Exercise 3

Using only pointers(no array index),write

 Function to add all numbers in an integer array ,

 Function to print all odd numbers array in the array

You might also like