5752GE3 Computer Science Lecture 13
5752GE3 Computer Science Lecture 13
LECTURE 13
Introduction to C++ Programming
• What is programming?
Programming is taking
A problem
Find the area of a rectangle
A set of data
length
width
A set of functions
area = length * width
Then,
Applying functions to data to solve the problem
2
Introduction to C++ Programming
3
Procedural Concept
4
Procedural Concept (II)
• Procedural Languages
• C, Pascal, Basic, Fortran
• Facilities to
• Pass arguments to functions
• Return values from functions
• For the rectangle problem, we develop a function
int compute_area (int l, int w){
return ( l * w );
}
5
Object-Oriented Concept
7
Basic C++ Extension from C
• comments
/* You can still use the old comment style, */
/* but you must be // very careful about mixing them */
// It's best to use this style for 1 line or partial lines
/* And use this style when your comment
consists of multiple lines */
• cin and cout (and #include <iostream.h>)
cout << "hey";
char name[10];
cin >> name;
cout << "Hey " << name << ", nice name." << endl;
cout << endl; // print a blank line
• declaring variables almost anywhere
// declare a variable when you need it
for (int k = 1; k < 5; k++){
cout << k;
}
8
Properties C++
• Is a better C
• Expressive
• Supports Data Abstraction
• Supports OOP
• Supports Generic Programming
• Containers
• Stack of char, int, double etc
• Generic Algorithms
• sort(), copy(), search() any container Stack/Vector/List
9
C++ Data Types
simple structured
address
float double long double
pointer reference
10
Recall that . . .
char str [ 8 ];
11
Addresses in Memory
int x;
float number;
char ch;
x number ch
12
Obtaining Memory Addresses
• The address of a non-array variable can be obtained by using
the address-of operator &
13
What is a pointer variable?
14
Using a Pointer Variable
2000
int x;
12
x = 12;
x
int x; 2000
x = 12; 12
x
3000
int* ptr;
2000
ptr = &x;
ptr
cout << *ptr;
16
Using the Dereference Operator
int x; 2000
x = 12; 12 5
x
3000
int* ptr;
2000
ptr = &x;
ptr
*ptr = 5;
// changes the value at the
address ptr points to 5
17
Self –Test on Pointers
4000
char ch;
A Z
ch = ‘A’;
ch
6000
char* q; 5000
*q = ‘Z’;
char* p;
p = q; // the rhs has value 4000
*ptr = ‘a’;
3000
3001
ptr
19
Reference Variables
Reference variable = alias for another variable
- Contains the address of a variable (like a pointer)
- No need to perform any dereferencing (unlike a pointer)
- Must be initialized when it is declared
int x = 5;
int &z = x; // z is another name for x
int &y ; //Error: reference must be initialized
cout << x << endl; -> prints 5
cout << z << endl; -> prints 5
z = 9; // same as x = 9;
21
Reference Variables Example
#include <iostream.h>
void p_swap(int *a, int *b)
{
// Function prototypes
(required in C++) int temp;
temp = *a; (2)
void p_swap(int *, int *); *a = *b; (3)
void r_swap(int&, int&); *b = temp;
}
int main (void){
int v = 5, x = 10; void r_swap(int &a, int &b)
cout << v << x << endl; {
p_swap(&v,&x); int temp;
cout << v << x << endl; temp = a; (2)
r_swap(v,x); a = b; (3)
cout << v << x << endl; b = temp;
return 0; }
} 22
Dynamic Memory Allocation Diagram
High-end
Run-time allocated
Stack
memory
Heap
Compile-time
static data
allocated
memory
Program
code
Low-end
23
Dynamic Memory Allocation
• In C, functions such as malloc() are used to dynamically
allocate memory from the Heap.
• In C++, this is accomplished using the new and delete
operators
• new is used to allocate memory during execution time
• returns a pointer to the address where the object is to be stored
• always returns a pointer to the type that follows the new
24
Operator new Syntax
new DataType
ptr
ptr = new char;
5000
*ptr = ‘B’;
‘B’
26
The NULL Pointer
• There is a pointer constant called the “null pointer”
denoted by NULL
• But NULL is not memory address 0.
delete Pointer
delete [ ] Pointer
*ptr = ‘B’;
5000
cout << *ptr;
‘B’
delete ptr; NOTE:
delete deallocates the
memory pointed to by ptr
29
Example
char *ptr ;
3000
ptr
ptr = new char[ 5 ]; ???
NULL
6000
???
30
Pointers and Constants
char* p;
p = new char[20];
32