Professional Documents
Culture Documents
STACK
STACK
STACK
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
: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
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.
Example
;int* p = new int
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
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