Introduction to C++ Data Abstraction w/ Classes
Topic #3
1 CS162 Topic #3
Topic #3 CS162 Topic #3 1 Topic #3 Abstract Data Types - - PowerPoint PPT Presentation
Introduction to C++ Data Abstraction w/ Classes Topic #3 CS162 Topic #3 1 Topic #3 Abstract Data Types Introduction to...Object Models Introduction to...Data Abstraction Using Data Abstraction in C++ ...an introduction to the
1 CS162 Topic #3
2 CS162 Topic #3
3 CS162 Topic #3
4 CS162 Topic #3
5 CS162 Topic #3
6 CS162 Topic #3
7 CS162 Topic #3
8 CS162 Topic #3
9 CS162 Topic #3
10 CS162 Topic #3
11 CS162 Topic #3
12 CS162 Topic #3
13 CS162 Topic #3
14 CS162 Topic #3
15 CS162 Topic #3
16 CS162 Topic #3
17 CS162 Topic #3
18 CS162 Topic #3
19 CS162 Topic #3
20 CS162 Topic #3
21 CS162 Topic #3
class data_type_name { public: //operations go here private: //memory is reserved here };
22 CS162 Topic #3
23 CS162 Topic #3
24 CS162 Topic #3
25 CS162 Topic #3
string my_str; vs. int i;
26 CS162 Topic #3
within the public section of the class interface
27 CS162 Topic #3
cin.get(ch);
28 CS162 Topic #3
29 CS162 Topic #3
30 CS162 Topic #3
31 CS162 Topic #3
struct video { char title[100]; char category[5]; int quantity; };
32 CS162 Topic #3
class list { public: list(); int add (const video &); int remove (char title[]); int display_all(); private: video my_list[CONST_SIZE]; //for now... int num_of_videos; };
33 CS162 Topic #3
int main() { list home_videos; //has an array of 100 videos list kids_shows; //another 100 videos here...
cin.get(out_of_site.title,100,‟\n‟); cin.ignore(100,‟\n‟);
//use
34 CS162 Topic #3
35 CS162 Topic #3
36 CS162 Topic #3
37 CS162 Topic #3
38 CS162 Topic #3
class list { <--- the data type!!! public: list(); <--- the constructor int add (const video &); 3 member functions int remove (char title[]); int display_all(); private: video my_list[CONST_SIZE]; data members int num_of_videos; }; <--- notice like structures we need a semicolon
39 CS162 Topic #3
– by default, all members in a structure are available for use by clients (e.g., main programs); they are public
40 CS162 Topic #3
video object;
41 CS162 Topic #3
42 CS162 Topic #3
versus classes you use to solve a problem.
struct video { char title[100]; char category[5]; int quantity; };
43 CS162 Topic #3
44 CS162 Topic #3
45 CS162 Topic #3
46 CS162 Topic #3
47 CS162 Topic #3
48 CS162 Topic #3
class list { public: notice it is public int display_all() { for (int i=0; i<num_of_videos; ++i) cout <<my_list[i].title <<„\t‟ <<my_list[i].category <<„\t‟ <<my_list[i].quantity <<endl; }
video my_list[CONST_SIZE]; int num_of_videos; }; 49 CS162 Topic #3
50 CS162 Topic #3
51 CS162 Topic #3
class list { public: int display_all()
video my_list[CONST_SIZE]; int num_of_videos; };
– prototype statements – structure declarations and definitions – class interfaces and class declarations – include other files
52 CS162 Topic #3
#include “list.h” notice the double quotes int list::display_all() { for (int i=0; i<num_of_videos; ++i) cout <<my_list[i].title <<„\t‟ <<my_list[i].category <<„\t‟ <<my_list[i].quantity <<endl; } – Notice, the code is the same – But, the function is prefaced with the class name and the scope resolution
– This places the function in class scope even though it is implemented in another file – Including the list.h file is a “must”
53 CS162 Topic #3
54 CS162 Topic #3
55 CS162 Topic #3
class list { public: list(); <--- the constructor
list::list(){ num_of_videos = 0; }
56 CS162 Topic #3
int main() { list fun_videos; implicitly calls the constructor list all_videos[10]; implicitly calls the constructor 10 times for each of the 10 objects!!
57 CS162 Topic #3
struct video {
char * title; char category[5]; int quantity; };
58 CS162 Topic #3
59 CS162 Topic #3
class list { public: list(); int add (const video &); int remove (char title[]); int display_all(); private: video *my_list; int video_list_size; int num_of_videos; };
60 CS162 Topic #3
61 CS162 Topic #3
62 CS162 Topic #3
63 CS162 Topic #3
list fun_videos(20); //size is 20
64 CS162 Topic #3
list::list(int size=100) { my_list = new video [size]; video_list_size = size; num_of_videos = 0; } (Remember, to change the prototype for the constructor in the class interface)
65 CS162 Topic #3
66 CS162 Topic #3
(Notice the ~ in front of the function name) (It can take NO arguments and has NO return type) (This too must be in the class interface....)
67 CS162 Topic #3
68 CS162 Topic #3
69 CS162 Topic #3
70 CS162 Topic #3
71 CS162 Topic #3
72 CS162 Topic #3
73 CS162 Topic #3