0% found this document useful (0 votes)
17 views39 pages

Pointers L1

Uploaded by

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

Pointers L1

Uploaded by

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

CS II for Engineering

Lecture 1: Classes and Objects I

Nada Sharaf

Nada Sharaf Lecture 1: Classes and Objects I 1 / 39


General Information

Lecturer: Dr. Nada Sharaf, [email protected] A211


Lippman, S.; Lajoie, J.; Moo, B.: C++ Primer. 5th edition.
Addison-Wesley, 2012. ISBN 978-0321714114.
Stroustrup, B.: Programming - Principles and Practice Using C++.
2nd edition. Addison Wesley, 2014. ISBN 978-0-321-99278-9.

Nada Sharaf Lecture 1: Classes and Objects I 2 / 39


Credits and a Huge Thank You to Prof. Dr. Slim
Abdennadher and Prof. Dr. Klaues Baer

Nada Sharaf Lecture 1: Classes and Objects I 3 / 39


Tentative Grading

Quizzes: 25%
Project: 10%
Midterm Exam: 25%
Final Exam: 40%

Nada Sharaf Lecture 1: Classes and Objects I 4 / 39


What

You can write programs


You understand how computers work
It is time to go foe a different direction in programming: namely
Object Oriented Programming using C++
OOP: Object Oriented Programming
C++ was developed to combine the object oriented features of Java
with the power of C
Closer to hardware compared to Python or Javascript.

Nada Sharaf Lecture 1: Classes and Objects I 5 / 39


Where

Game Development
Embedded Systems
Operating Systems
Animations
Used in big companies such as
Google, Facebook, Valeo, ... etc

Nada Sharaf Lecture 1: Classes and Objects I 6 / 39


Nada Sharaf Lecture 1: Classes and Objects I 7 / 39
Why OOP

Makes problem solving more effective


Allow you to reuse code

Nada Sharaf Lecture 1: Classes and Objects I 8 / 39


Why Contd.
You can use primitive data types to represent a lot of values: counts,
names, addresses, ... etc.
Think about real life objects e.g. students, chess boards, ... etc
You can represent a student using
1 name: string
2 faculty: string
3 major: string
4 gpa: float
5 ID: int
But we have a lot of students ... i.e. lots of variables
Think about the courses of every student too.
What if we can encapsulate all of the variables into one “Student”
type?
In this case we can also perform actions on students such as updating
their gpa, transferring them to another major, ... etc.

Nada Sharaf Lecture 1: Classes and Objects I 9 / 39


Difference between Java and C++
Java C++

Java uses compiler and inter- C++ uses compiler only


preter
Supports operator overloading &
Supports only method over-
method overloading
loading
Compile anywhere
Run anywhere
Supports multiple inheritance
Does not support multiple in-
heritance
Support pointers and their manipula-
Does not support pointers
tion
Supports manual object management
Uses the automatic garbage
collector
Platform dependent
Platform independent.
Nada Sharaf Lecture 1: Classes and Objects I 10 / 39
Compilation vs. Interpretation

Compilation
▶ Compiler converts the
language to the required
target language
▶ Translates the whole program
at once

Nada Sharaf Lecture 1: Classes and Objects I 11 / 39


Compilation vs. Interpretation

Interpretation
▶ Program is read instruction by
instruction
▶ Every instruction is decoded
and executed directly

Nada Sharaf Lecture 1: Classes and Objects I 12 / 39


What does Java do?

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

Nada Sharaf Lecture 1: Classes and Objects I 13 / 39


Platform Independence

C++ is platform dependent.


programs written in C++ have
to be compiled on every
platform

Nada Sharaf Lecture 1: Classes and Objects I 14 / 39


Difference between C and C++

C++ supports object oriented features:


▶ Abstraction
▶ Polymorphism
▶ Inheritance
▶ Encapsulation
C does not support function and operator overloading

Nada Sharaf Lecture 1: Classes and Objects I 15 / 39


IDEs

Code Blocks: https://www.codeblocks.org/


Dev-C++ https://bloodshed-dev-c.en.softonic.com/
Online IDEs:
▶ http://cpp.sh/
▶ https://www.onlinegdb.com/online_c++_compiler
▶ https://www.jdoodle.com/online-compiler-c++/
▶ https://replit.com/

Nada Sharaf Lecture 1: Classes and Objects I 16 / 39


C++ Programs

The operating system executes


the main function of the
program
Every functions has:
▶ Return type
▶ Function name int main ()
▶ Parameter list (optional) {
▶ Function body return 0;
In C++, the main function }
returns an int
A return value 0 for the main is
usually used to indicate success
However, it can have other
parameter types

Nada Sharaf Lecture 1: Classes and Objects I 17 / 39


Classes

A class defines a type along with


a collection of operations for
that type
The type name is the same as
the name of the class
A class is a mould that you can
use to build multiple instances
of (objects)

Nada Sharaf Lecture 1: Classes and Objects I 18 / 39


Nada Sharaf Lecture 1: Classes and Objects I 19 / 39
Sales Item Class Example

int main ()
{
Sales_item book ;
std :: cin >> book ;
std :: cout << book << std :: endl ;
return 0;
}

Nada Sharaf Lecture 1: Classes and Objects I 20 / 39


Pointers I

// Example program
# include < iostream >
# include < string >
int main ()
{
int x ;
std :: cout <<" The ␣ address ␣ of ␣ " <<x << " is " << & x ;
}

Nada Sharaf Lecture 1: Classes and Objects I 21 / 39


Pointers II

// 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 ;
}

Nada Sharaf Lecture 1: Classes and Objects I 22 / 39


Dereferencing a Pointer

// Example program
# include < iostream >
# include < string >
int main ()
{
int v1 = 20;
int * p2 = & v1 ;
* p2 = 35;
std :: cout << v1 ;
}

Nada Sharaf Lecture 1: Classes and Objects I 23 / 39


Pointers

Sending a copy vs. sending the address.


# include < iostream > # include < iostream >
# include < string > # include < string >
void swap ( int x , int y ) void swap ( int * x , int * y )
{ {
int tmp ; int tmp ;
tmp = x ; tmp = * x ;
x = y; *x = *y;
y = tmp ; * y = tmp ;
} }
int main () int main ()
{ {
int a = 2 , b = 3; int a = 2 , b = 3;
swap (a , b ); swap (& a , & b );

std :: cout <<" a : ␣ " <<a ; std :: cout <<" a : ␣ " <<a ;
std :: cout < < " ␣ and ␣ b " << b ; std :: cout < < " ␣ and ␣ b " << b ;
} }

Nada Sharaf Lecture 1: Classes and Objects I 24 / 39


Pointers ... Again

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

Nada Sharaf Lecture 1: Classes and Objects I 25 / 39


Pointer Operators

1 Address Operator, &, retrieves the address of any variable


ptr = & x ;

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

Nada Sharaf Lecture 1: Classes and Objects I 26 / 39


Example

1 What is the cell that ptr points


to ? ptr points to x
2 How to assign ptr to hold the
address of x? Using the &
operator

Nada Sharaf Lecture 1: Classes and Objects I 27 / 39


Pointer State

The value of a pointer can be


1 Pointing to an object.
2 Pointing to the location just immediately past the end of an object.
3 NULL pointer i.e. not bound to any object.
4 Invalid

Nada Sharaf Lecture 1: Classes and Objects I 28 / 39


Example

Use sli.do Code: 377634


# include < iostream >
using namespace std ;
int main () {
int ival = 42;
int * p = & ival ;
cout << * p << endl ;
* p = 100;
cout << ival << endl ;
}

Nada Sharaf Lecture 1: Classes and Objects I 29 / 39


Pointers

Sending a copy vs. sending the address.


# include < iostream > # include < iostream >
# include < string > # include < string >
void swap ( int x , int y ) void swap ( int * x , int * y )
{ {
int tmp ; int tmp ;
tmp = x ; tmp = * x ;
x = y; *x = *y;
y = tmp ; * y = tmp ;
} }
int main () int main ()
{ {
int a = 2 , b = 3; int a = 2 , b = 3;
swap (a , b ); swap (& a , & b );

std :: cout <<" a : ␣ " <<a ; std :: cout <<" a : ␣ " <<a ;
std :: cout < < " ␣ and ␣ b " << b ; std :: cout < < " ␣ and ␣ b " << b ;
} }

Nada Sharaf Lecture 1: Classes and Objects I 30 / 39


Multiple Levels

// 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 ;
}

Nada Sharaf Lecture 1: Classes and Objects I 31 / 39


C++ Access Modifiers

public: anyone anywhere can


access it
private: anyone within the
class access it. This is the
default
protected: anyone within the
class and its subclasses access
it

Nada Sharaf Lecture 1: Classes and Objects I 32 / 39


Template

class Class_Name {
Access Modifier :
Attribute_0 ;
Attribute_1 ;
.....
Access Modifier :
Function_0 ;
Function_1 ;
.....
};

Nada Sharaf Lecture 1: Classes and Objects I 33 / 39


Defining a Class without Functions

class Sales_Item { struct Sales_data {


public : string bookNo ;
string isbn ; unsigned units_sold ;
double revenue ; double revenue ;
unsigned units_sold ;
}; };

Nada Sharaf Lecture 1: Classes and Objects I 34 / 39


Bank Account Example with Functions

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 );
};

Nada Sharaf Lecture 1: Classes and Objects I 35 / 39


Private Attributes

Thus, we need setter and getter functions in this case

Nada Sharaf Lecture 1: Classes and Objects I 36 / 39


Different Modifiers

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 );
};

Nada Sharaf Lecture 1: Classes and Objects I 37 / 39


Implementation of Functions
# include < iostream >
using namespace std ;
class BankAccount {
private :
string owner ;
double balance ;
int account _number ;
public :
void i n i t i a l i z e _ a c c o u n t ( string o , double b , int num )
{ owner = o ;
balance = b ;
acco unt_numb er = num ;}
int g e t _ a c c o u n t _ n u m b e r ()
{ return acco unt_numb er ;}
void deposit ( double amount );
void withdraw ( double amount );
bool h a s H i g h e r B a l a n c e ( BankAccount bAcc2 );
};
void BankAccount :: deposit ( double amount )
{ balance = balance + amount ;}
int main ()
{
BankAccount bA1 ;
bA1 . i n i t i a l i z e _ a c c o u n t ( " Owner " , 0 , 1);
return 0;
}
Nada Sharaf Lecture 1: Classes and Objects I 38 / 39
Thank you :)

Nada Sharaf Lecture 1: Classes and Objects I 39 / 39

You might also like