ch01
ch01
1
Outline
2
Storage Containers (Vectors)
// output elements of v
for(i = 0; i < v.size(); i++)
cout << v[i] << " "
Output: 7 4 9 3 1
3
Storage Containers (Vectors)
4
Storage Containers (Vectors)
V ecto r 15 20 30 35 40 V ecto r 1 5 2 0 3 0 3 5 4 0
In sert 2 5 at 25 E rase 2 0 at
15 30 35 40
P o sitio n 2 1 5 2 0 30 35 40
P o sitio n 1
S h ift righ t 2 0 S h ift left
5
Storage Containers (Lists )
• list container
each element has a reference that identifies the next item in
the list.
Adding a new item involves breaking a link in the chain
//
fro n t re a r
n e w Ite m
6
Storage Containers (Maps)
D en ver
752 604
648 A lb u q u erq u e
S an F ran cisco 763
432
504
P h o en ix
S an D iego 355
7
Storage Containers (Trees )
A tree is a container that stores elements as nodes emanating
from a root.
The tree holds 8 airbill numbers. Any search requires at most 3
movements along a path from the root.
R oot
G 9B 7
B 40A
N T2 P
F A 27
H 14K W 29Z
D 29Z
TV 9 3
8
Data Abstraction with struct Data Type
• Data Abstraction
to group together related variables (or a record) using one entity
The Data Type of the variables can be (and usually are) different
• Syntax
struct Structure_Name
{
Data_Type data_member_1;
Data_Type data_member_2;
Data_Type data_member_n;
};
Don’t forget this semicolon !!!
9
Example 1
• Declare:
Student student1, student2;
Employee emp[20];
• Use:
cin >> student1.name;
cin >> student2.name;
for(int i=0; i<20; i++)
{
cin >> emp[i].name;
cin >> emp[i].ssn;
emp[i].payRate = 12.5;
}
1. Without struct, we have to use two arrays
2. struct can do something more in C++ (we will get to this later)
13
Classes and Objects
14
Classes and Objects
• An object has the data members of its class and can call the
member functions of its class
15
Syntax of classes
• class ClassName
{
public: Don’t forget this colon !!!
member functions
member variables (rarely)
private:
member variables
member functions declarations (internal use only)
}; Don’t forget this semicolon !!!
• Note that class, private, and public are C++ reserved words.
16
Example 1 – Student class
private:
string name;
};
17
Classes (Private/Public Sections)
pr i vate :
data m e m be rs
a c c e ssible to the
im ple m e nta tion of
m e m be r func tio ns
the m e m be r func tions publ i c :
ac c e s s ible by
data m e m be rs any pro gram s tate m e nt
m e m be r func tio ns
18
Classes (Private/Public Sections)
a c c e ssible to the
im ple m e nta tion of
the m e m be r func tions publ i c :
ac c e s s ible by
data m e m be rs any pro gram s tate m e nt
m e m be r func tio ns
19
Classes (Private/Public Sections)
pr i vate :
data m e m be rs
a c c e s s ible to the
im ple m e nta tion of
m e m be r func tio ns
the m e m be r func tions
20
Example 2 – MyData class
private:
int data;
};
21
Example 3 – Employee class
24
constructors and destructors
• A destructor cleans up after your object and free any memory you might
have allocated.
a destructor has no return value and no parameters and MUST be named
~ClassName
destructor is optional
25
constructors and destructors
class Example
{ int main()
public: {
// create an object and set data to 0
Example(){data=0;} Example obj1;
obj1.showData();
//constructor with parameter
Example(int x) {data = x;} obj1.enterData(100);
obj1.showData();
void enterData(int d)
{data = d;}
// create an object and set data to 200
void showData() Example obj2(200);
{cout << data << endl;} obj2.showData();
//destructor return 0;
~Example(){} }
Output:
private:
int data; 0
}; 100
200
26
accessors and modifiers – class definition
28
accessors and modifiers – instantiation and usage
int main()
{
Rectangle box; //instantiation of the class rectangle
double wide, high;
cout<<”Enter the length of the rectangle\n”;
cin >> wide;
cout << ”Enter the width of the rectangle\n”;
cin >> high;
box.setData(wide, high);
cout << ”Width: “ << box.getWidth() << endl;
cout << ”Length: “ << box.getLength() << endl;
cout << ”Area: “ << box.getArea() << endl;
return 0;
}
29
const accessor Functions
• Text page 12 - 13
Example:
double getWidth() const;
30
Using Header Files – Approach I
int main()
{
Rectangle box; //instantiation of the class rectangle
double wide, high;
cout<<”Enter the length of the rectangle\n”;
cin >> wide;
cout << ”Enter the width of the rectangle\n”;
cin >> high;
box.setData(wide, high);
cout << ”Width: “ << box.getWidth() << endl;
cout << ”Length: “<< box.getLength() << endl;
cout << ”Area: “<< box.getArea() << endl;
return 0;
}
33
Using Header Files – Approach II
34
Header file
• File extension is .h
could use any symbol that has not been defined
#ifndef file_name
#define file_name
class declarations
...
#endif
#ifndef rectangle_h
#define rectangle_h
class Rectangle
{
public:
void setData(double, double);
double getWidth() {return width;}
double getLength() {return
length;} double getArea() {return
area;}
private:
double width;
double length;
double area;
void calcArea() {area = width *
length;}
};
36
#endif
Implementation (Source) File
#include “file_name”
• The quotes tells the compiler to look for the header file in the file folder of
the program instead of the file folder containing the standard header files
like iostream
37
Example: Rectangle class
// Contents of rectangle.cpp
#include “rectangle.h”
38
Without inline implementations
// rectangle.h header file without inline implementation
#ifndef rectangle_h
#define rectangle_h
class Rectangle
{
public:
void setData(double, double);
double getWidth();
double getLength();
double getArea();
private:
double width;
double length;
double area;
void calcArea();
};
#endif
39
Without inline implementations
// rectangle.cpp implementation file
#include “rectangle.h”
#include “rectangle.h”
int main ()
{
Rectangle rect;
rect.setData(5, 10);
cout << rect.getArea();
return 0;
}
41
API (randomNumber Class)
double frandom();
Return a real number x, 0.0 <= x < 1.0
int random();
Return a 32-bit random integer m, 0 <= m < 231-1
42
Generating Random Numbers
int item, i;
double x;
43
String Functions and Operations
44
String Functions and Operations
45
String Functions and Operations