Professional Documents
Culture Documents
1 / 47
Review C++
Learning Objectives
2 / 47
Review C++
3 / 47
Review C++
4 / 47
Review C++
5 / 47
Review C++
Command-line Arguments
Your program can gain access to the command line arguments as
follows:
Command-line Arguments
argv:
1012 ----> "program.exe"
1023 ----> "Hello"
1045 ----> "World"
0
Classes
Classes
9 / 47
Review C++
10 / 47
Review C++
Property
The state of the traffic light (green, orange or red)
Operation
Set the state
Construction
Create a light in a default state (e.g. red)
Create a light in a given state
Destruction
Nothing special needs to be done
Internal representation
Store the state in a variable
11 / 47
Review C++
1 class Trafficlight {
2
3 };
12 / 47
Review C++
1 class Trafficlight{
2 public: // access declaration
3 enum light { green , orange , red }; // type member
4
5 Trafficlight (); // default constructor
6 };
13 / 47
Review C++
1 class Trafficlight {
2 public: // access declaration
3 enum light { green , orange , red }; // type member
4
5 Trafficlight (); // default constructor
6 Trafficlight(light); // constructor
7
8 ~ Trafficlight (); // destructor
9
10 void set_state(light);
11
12 private: // this is hidden
13 light state_; // data member
14 };
14 / 47
Review C++
1 class Trafficlight {
2 public: // access declaration
3 enum light { green , orange , red }; // type member
4
5 Trafficlight (); // default constructor
6 Trafficlight(light); // constructor
7
8 ~ Trafficlight (); // destructor
9
10 light state () const; // function member
11
12 private: // this is hidden
13 light state_; // data member
14 };
15 / 47
Review C++
1 class Trafficlight {
2 public: // access declaration
3 enum light { green , orange , red }; // type member
4
5 Trafficlight (); // default constructor
6 Trafficlight(light); // constructor
7
8 ~ Trafficlight (); // destructor
9
10 light state () const; // function member
11
12 void set_state(light);
13
14 private: // this is hidden
15 light state_; // data member
16 };
16 / 47
Review C++
1 class Trafficlight {
2 public: // access declaration
3 enum light { green , orange , red }; // type member
4
5 Trafficlight (); // default constructor
6 Trafficlight(light); // constructor
7
8 ~ Trafficlight (); // destructor
9
10 light state () const; // function member
11
12 void set_state(light);
13
14 private: // this is hidden
15 light state_; // data member
16 };
17 / 47
Review C++
1 class Trafficlight {
2 public: // access declaration
3 enum light { green , orange , red }; // type member
4
5 Trafficlight (); // default constructor
6 Trafficlight(light=red); // constructor
7
8 ~ Trafficlight (); // destructor
9
10 light state () const; // function member
11
12 void set_state(light);
13
14 private: // this is hidden
15 light state_; // data member
16 };
18 / 47
Review C++
1 class Trafficlight {
2
3 public: // access declaration
4 enum light { green , orange , red }; // type member
5
6 Trafficlight(light=red); // constructor
7
8 ~ Trafficlight (); // destructor
9
10 light state () const; // function member
11
12 void set_state(light);
13
14 private: // this is hidden
15 light state_; // data member
16 };
19 / 47
Review C++
20 / 47
Review C++
1 // TrafficLight.h
2 class Trafficlight {
3
4 public: // access declaration
5 enum light { green , orange , red }; // type member
6
7 Trafficlight(light=red); // constructor
8
9 ~ Trafficlight (); // destructor
10
11 light state () const; // function member
12
13 void set_state(light);
14
15 private: // this is hidden
16 light state_; // data member
17 };
21 / 47
Review C++
1 // TrafficLight.cpp
2 #include " TrafficLight .h"
3 Trafficlight :: Trafficlight (light=red){ // constructor
4 state_ = light;
5 }
6
7 Trafficlight :: light Trafficlight :: state (){ // constructor
8 return state_;
9 }
10
11 void Trafficlight :: set_state(light){ // constructor
12 state_ = light;
13 }
22 / 47
Review C++
Classes: declaration
1 Trafficlight oTrafficlight ;
OR
1 Trafficlight * classPtr;
2 classPtr = new Trafficlight;
23 / 47
Review C++
Pointer
24 / 47
Review C++
Pointer
pc = &c;
25 / 47
Review C++
Pointer
int * p;
26 / 47
Review C++
Pointer
27 / 47
Review C++
Pointer: vocalization
int * p;
p; "p is ..."
* "...a pointer to ..."
int "...an integer."
28 / 47
Review C++
Pointer
pb = pa;
pc = pa;
29 / 47
Review C++
String Literals
Consider,
H e l l o \0
Pointer to store the address of the first byte, or first character ’H’.
30 / 47
Review C++
31 / 47
Review C++
Another example:
1 int main ()
2 {
3 char * pmessage;
4 pmessage = "Hello";
5 print( pmessage );
6 return 0;
7 }
32 / 47
Review C++
Pointers: example
33 / 47
Review C++
Pointers: example
34 / 47
Review C++
References
Pointers: summary
An address &x tells where the variable is located in memory.
Pointer points to a variable by remembering its address:
ptr = &x;
double * d_ptr;
* ptr
37 / 47
Review C++
Pointer arithmetic
38 / 47
Review C++
Pointer arithmetic
1 unsigned int n;
2 std::cin >> n;
3 float x[n]; // will not work
41 / 47
Review C++
Examples:
dynamically allocates memory for a single int, and stores the
address in intPtr.
1 int * intPtr;
2 intPtr = new int;
1 int * intPtr;
2 intPtr = new int (6);
42 / 47
Review C++
43 / 47
Review C++
44 / 47
Review C++
Memory leaks
45 / 47
Review C++
Memory leaks
1 #include <stdlib.h>
2
3 int main ()
4 {
5 char * x = (char * ) malloc (100); // Memory Leak!
6
7 return 0;
8 }
46 / 47
Review C++
Reading Material
47 / 47