Pointers L1
Pointers L1
Nada Sharaf
Quizzes: 25%
Project: 10%
Midterm Exam: 25%
Final Exam: 40%
Game Development
Embedded Systems
Operating Systems
Animations
Used in big companies such as
Google, Facebook, Valeo, ... etc
Compilation
▶ Compiler converts the
language to the required
target language
▶ Translates the whole program
at once
Interpretation
▶ Program is read instruction by
instruction
▶ Every instruction is decoded
and executed directly
Both
▶ Program is read instruction by
instruction
▶ Every instruction is converted
to byte code
▶ The Java virtual machine then
reads the byte code and
produces the machine code to
execute it on the device
int main ()
{
Sales_item book ;
std :: cin >> book ;
std :: cout << book << std :: endl ;
return 0;
}
// Example program
# include < iostream >
# include < string >
int main ()
{
int x ;
std :: cout <<" The ␣ address ␣ of ␣ " <<x << " is " << & x ;
}
// Example program
# include < iostream >
# include < string >
int main ()
{
int x = 10;
int * ptr ;
ptr = & x ;
std :: cout <<" The ␣ address ␣ of ␣ " <<x << " is " << ptr << " ␣ or ␣ " < <& x ;
}
// Example program
# include < iostream >
# include < string >
int main ()
{
int v1 = 20;
int * p2 = & v1 ;
* p2 = 35;
std :: cout << v1 ;
}
std :: cout <<" a : ␣ " <<a ; std :: cout <<" a : ␣ " <<a ;
std :: cout < < " ␣ and ␣ b " << b ; std :: cout < < " ␣ and ␣ b " << b ;
} }
A pointer is a variable
A pointer is a compound type
that points to another type
However inside a pointer we
store an address
Pointers have types depending
on the value inside the address
they hold
2 The dereferencing operator, * can access the value of the cell the
pointer is pointing to
y = * ptr ; // y now holds the same value as x
std :: cout <<" a : ␣ " <<a ; std :: cout <<" a : ␣ " <<a ;
std :: cout < < " ␣ and ␣ b " << b ; std :: cout < < " ␣ and ␣ b " << b ;
} }
// Example program
# include < iostream >
using namespace std ;
int main () {
int x = 350;
int y = 700;
int * p1 = & x ;
int ** p2 = & p1 ; 350
cout < <* p1 < < endl ; 700
* p2 =& y ;
cout < <* p1 < < endl ;
}
class Class_Name {
Access Modifier :
Attribute_0 ;
Attribute_1 ;
.....
Access Modifier :
Function_0 ;
Function_1 ;
.....
};
class BankAccount {
private :
string owner ;
double balance ;
int account_number ;
public :
void i ni ti al iz e_ ac co un t ( string o , double b ,
int num );
int ge t_ ac co un t_n um be r ();
void deposit ( double amount );
void withdraw ( double amount );
bool hasHigherBalance ( BankAccount bAcc2 );
};
class BankAccount {
private :
string owner ;
double balance ;
public :
int account_number ;
public :
void i ni ti al iz e_ ac co un t ( string o , double b ,
int num );
int ge t_ ac co un t_n um be r ();
void deposit ( double amount );
void withdraw ( double amount );
bool hasHigherBalance ( BankAccount bAcc2 );
};