1 / 27

Public vs. Private and Protected Class Members

Public vs. Private and Protected Class Members. Professor Jason Chen School of Business Administration Gonzaga University Spokane, WA 99258 chen@gonzaga.edu. Enabling Technology. Displacement Technology. TECHNOLOGY SHIFT. Right idea, at the right time .

lore
Download Presentation

Public vs. Private and Protected Class Members

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Public vs. Private and Protected Class Members Professor Jason Chen School of Business Administration Gonzaga University Spokane, WA 99258 chen@gonzaga.edu

  2. Enabling Technology Displacement Technology TECHNOLOGY SHIFT • Right idea, at the right time From customized software products to • Commercially viable and widely available • Last for years or decades reusable software modules to Highly configurablesoftware products without additional intervention from a programmer. Change focus from information access to Emerging Technology value creation. N

  3. What is a Class? • A class is a user-defined type. • It is also a generic specification, or template, for producing an object at an abstract, conceptual level. • When classes that do not create instances of themselves, the classes are called abstract.

  4. Why Classes? • The classes exist so that a programmer can extract and collect common behavior among classes in one location. • An abstract class specifies its default behavior so that subclass method will refine, augment, or overload its implementation. • A concrete subclass adds new abilities to the behaviors inherited from its abstract superclass when the subclass’s function and purpose require.

  5. Public vs. Private Class Members • When you declare a class, you can define members as public or private (or protected). • Public class members are available throughout your program using the object name and the dot (class.member) or indirection (class_ptr->member) operators.

  6. Public vs. Private Class Members (cont.) • Private class members, on the other hand, can only be accessed using class member functions (i.e., functions declared in the base class). Using private class members, your programs can better control the values assigned to class members and how those values are used. By default, all class members are private. Once you include a public label within your program, however, you can specify public members. If you later want to declare private members, you must use the private label.

  7. Public vs. Private Class Members (cont.) • Protected class members are accessible within the base or derived class member functions, but not outside of such functions.

  8. Information Hiding • Information hiding is the process of designing functions or classes as “black box.” In other words, to use a function or class, a programmer does not need to know the box’s inner workings, since it brings with it all of its methods, but rather, simply the operation the box performs and how to interface with the box.

  9. Within your C++ programs, private class members support information hiding.

  10. First Class Example: Employees TYPES OF EMPLOYEES Salaried Hourly Permanent Hourly Temporary name name name home phone home phone home phone office phone office phone office phone salary level wage wage bonus level reports to reports to reports to assistant YOUR TASK: Draw a UML diagram that best describes the relationship of these three types of employees .

  11. employee name : char[64] home_phone : char[64] office_phone : char[64] reports_to : char[64] show_employee() salaried hourly temporary salary : money wage : money wage : money assistant : char[64] bonus_level : money show_hourly() show_temporary() show_salaried() employee class

  12. movie name : : char[64] first_star : : char[64] second_star : :char[64] show_move() initialize() sleepless fugitive name : : char[64] name : : char[64] first_star : : char[64] first_star : : char[64] second_star : :char[64] second_star : :char[64] show_move() show_move() initialize() initialize() How to construct objects? movie fugitive, sleepless;

  13. Global resolution operator class name operation name movie name : : char[64] first_star : : char[64] second_star : :char[64] show_move() initialize() objects Class movie class void movie::show_movie(void) { cout << "Movie name: " << name << endl; cout << "Starring: " << first_star << " and " << second_star << endl << endl; } void movie::initialize(char *movie_name, char *first, char *second) { strcpy(name, movie_name); strcpy(first_star, first); strcpy(second_star, second); } See movies.cpp #include <iostream> #include <string> #include <conio.h> using namespace std; classmovie { public: char name[64]; char first_star[64]; char second_star[64]; void show_movie(void); void initialize(char *name, char *first, char *second); }; int main(void) { movie fugitive, sleepless; fugitive.initialize("The Fugitive", "Harrison Ford", "Tommy Lee Jones"); sleepless.initialize("Sleepless in Seattle", "Tom Hanks", "Meg Ryan"); fugitive.show_movie(); sleepless.show_movie(); return 0; }

  14. Figure: Conventional vs. Object - Oriented Conventional Object-Oriented Decomposed by Functions/process Objects/concepts Data type e.g., int num_of_employees; Class type class employee  … } employee full-time, part-time; Types One variable contains one value. e.g. number_of _employees =20; Content One class contains many objects; One object has many attributes, operations

  15. Compare two files: movies.cpp and public.cpp //file name: movies.cpp int main(void) { movie fugitive, sleepless; fugitive.initialize("The Fugitive", "Harrison Ford", "Tommy Lee Jones"); sleepless.initialize("Sleepless in Seattle", "Tom Hanks", "Meg Ryan"); fugitive.show_movie(); sleepless.show_movie(); return 0; } Question: What will happen if we declare all three attributes to be private? //file name: public.cpp int main(void) { movie fugitive, sleepless; fugitive.initialize("The Fugitive", "Harrison Ford", "Tommy Lee Jones"); sleepless.initialize("Sleepless in Seattle", "Tom Hanks", "Meg Ryan"); cout << "The last two movies I've watched are: " << fugitive.name << " and " << sleepless.name << endl; cout << "I thought " << fugitive.first_star << " was great!" << endl; return 0; }

  16. //File name: private_w_public.cpp #include <iostream> #include <string> #include <conio.h> using namespace std; class movie { public: void show_movie(void); void initialize(char *name, char *first, char *second); private: char name[64]; char first_star[64]; char second_star[64]; }; void movie::show_movie(void) { cout << "Movie name: " << name << endl; cout << "Starring: " << first_star << " and " << second_star << endl << endl; } void movie::initialize(char *movie_name, char *first, char *second) { strcpy(name, movie_name); strcpy(first_star, first); strcpy(second_star, second); } int main(void) { movie fugitive, sleepless; fugitive.initialize("The Fugitive", "Harrison Ford", "Tommy Lee Jones"); sleepless.initialize("Sleepless in Seattle", "Tom Hanks", "Meg Ryan"); cout << "The last two movies I've watched are: " << fugitive.name << " and " << sleepless.name << endl; cout << "I thought " << fugitive.first_star << " was great!" << endl; //fugitive.show_movie(); //sleepless.show_movie(); return 0; } Question: What will happen if we declare all three attributes to be private? Answer: The program will NOT be working. Why? see private_w_public.cpp)

  17. void main(void) { movie fugitive, sleepless; fugitive.initialize("The Fugitive", "Harrison Ford", "Tommy Lee Jones"); sleepless.initialize("Sleepless in Seattle", "Tom Hanks", "Meg Ryan"); fugitive.show_movie(); sleepless.show_movie(); getch(); } //File name: private.cpp #include <iostream.h> #include <string.h> #include <conio.h> class movie { public: void show_movie(void); void initialize(char *name, char *first, char *second); private: char name[64]; char first_star[64]; char second_star[64]; }; void movie::show_movie(void) { cout << "Movie name: " << name << endl; cout << "Starring: " << first_star << " and " << second_star << endl << endl; } void movie::initialize(char *movie_name, char *first, char *second) { strcpy(name, movie_name); strcpy(first_star, first); strcpy(second_star, second); } Homework: What should be changed if a ‘string’ type is used in the program?

  18. int main(void) { movie fugitive, sleepless; fugitive.initialize("The Fugitive", "Harrison Ford", "Tommy Lee Jones"); sleepless.initialize("Sleepless in Seattle", "Tom Hanks", "Meg Ryan"); fugitive.show_movie(); sleepless.show_movie(); return 0; } //file name: movies_string.cpp //use of string type #include <iostream> #include <string> using namespace std; class movie { public: string name; string first_star; string second_star; void show_movie(void); void initialize(string name,string first,string second); }; void movie::show_movie(void) { cout << "Movie name: " << name << endl; cout << "Starring: " << first_star << " and " << second_star << endl << endl; } void movie::initialize(string movie_name,string first,string second) { name = movie_name; first_star = first; second_star = second; }

  19. movie name : : char[64] first_star : : char[64] second_star : :char[64] show_move() initialize() Constructor:Construct objects automatically show_move()

  20. //File name: constructor.cpp #include <iostream> #include <string> using namespace std; class movie { public: void show_movie(void); movie(char *name, char *first, char *second); private: char name[64]; char first_star[64]; char second_star[64]; }; void movie::show_movie(void) { cout << "Movie name: " << name << endl; cout << "Starring: " << first_star << " and " << second_star << endl << endl; } movie::movie(char *name, char *first_star, char *second_star) { strcpy(movie::name, name); strcpy(movie::first_star, first_star); strcpy(movie::second_star, second_star); } int main(void) { movie fugitive("The Fugitive", "Harrison Ford", "Tommy Lee Jones"); movie sleepless("Sleepless in Seattle", "Tom Hanks", "Meg Ryan"); fugitive.show_movie(); sleepless.show_movie(); return 0; } See construct_string.cpp

  21. movie name : : char[64] first_star : : char[64] second_star : :char[64] movie() ~movie() show_movie() Destructor:Destruct objects automatically

  22. movie::~movie(void) { cout << "In the movie destructor for " << name << endl; getch(); } int main(void) { movie fugitive("The Fugitive", "Harrison Ford", "Tommy Lee Jones"); movie sleepless("Sleepless in Seattle", "Tom Hanks", "Meg Ryan"); fugitive.show_movie(); sleepless.show_movie(); return 0; } //File name: destructor.cpp #include <iostream> #include <string> #include <conio.h> using namespace std; class movie { public: void show_movie(void); movie(char *name, char *first, char *second); ~movie(void); private: char name[64]; char first_star[64]; char second_star[64]; }; void movie::show_movie(void) { cout << "Movie name: " << name << endl; cout << "Starring: " << first_star << " and " << second_star << endl << endl; } movie::movie(char *name, char *first_star, char *second_star) { strcpy(movie::name, name); strcpy(movie::first_star, first_star); strcpy(movie::second_star, second_star); }

  23. INLINE FUNCTION //File name: inline.cpp #include <iostream> #include <string> using namespace std; class movie { public: char name[64]; char first_star[64]; char second_star[64]; void show_movie(void) { cout << "Movie name: " << name << endl; cout << "Starring: " << first_star << " and " << second_star << endl << endl; } void initialize(char *movie_name, char *first, char *second) { strcpy(name, movie_name); strcpy(first_star, first); strcpy(second_star, second); } }; void main(void) { movie fugitive, sleepless; fugitive.initialize("The Fugitive", "Harrison Ford", "Tommy Lee Jones"); sleepless.initialize("Sleepless in Seattle", "Tom Hanks", "Meg Ryan"); cout << "The last two movies I've watched are: " << fugitive.name << " and " << sleepless.name << endl; cout << "I thought " << fugitive.first_star << " was great!" << endl; }

  24. Inline vs. Out-of-line Functions Sharing Member Function Code: When you declare class member functions, you have two choices. You can declare the function statements as inline statements (inside of the class declaration itself), or you can declare the statements out-of-line. The advantage of using out-of-line function declarations is that each object you create shares a copy of the program code (rather than each object you create gets its own unique copy of the function code). For example, if you create 100 objects, only one function is created, which objects share. In this way, your program’s memory requirements is reduced. Criteria of choosing inline or out-of-line functions: 1. If the function is larger or many objects will be created for that class, then out-of-line function should be used. (Reduce the size of the program). 2. If the function might be called very often, then inline function should be employed. (It saves CPU time for calling function and returns values; however, the size of the program my be bigger).

  25. #4a use data type of char (i.e., char title[80]) use cin.getline(title,80) #4b use of Rational Rose Case Tool try to generate the code and then modify #4a to this assignment Email me the following four (4) files: movie_L_F.mdl (UML file) mymoviedr_L_F.cpp movie_L_F.cpp movie_L_F.h Due on March. 22, Monday midnight. HW#4

  26. #include <iostream.h> #include <string.h> #include <conio.h> class movie { public: char name[64]; char first_star[64]; char second_star[64]; void show_movie(void) { cout << "Movie name: " << name << endl; cout << "Starring: " << first_star << " and " << second_star << endl << endl; } void initialize(char *movie_name, char *first, char *second) { strcpy(name, movie_name); strcpy(first_star, first); strcpy(second_star, second); } }; void main(void) { movie fugitive, sleepless; fugitive.initialize("The Fugitive", "Harrison Ford", "Tommy Lee Jones"); sleepless.initialize("Sleepless in Seattle", "Tom Hanks", "Meg Ryan"); cout << "The last two movies I've watched are: " << fugitive.name << " and " << sleepless.name << endl; cout << "I thought " << fugitive.first_star << " was great!" << endl; getch(); }

  27. movie name : : char[64] first_star : : char[64] second_star : :char[64] show_move() initialize() movie movie name : : char[64] name : : string first_star : : char[64] first_star : : string second_star : :char[64] second_star : :string show_move() show_move() movie class See movies.cpp

More Related