1 / 15

Abstraction and Data Hiding

Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch 1.1 - 1.6 Read Style Guide (see course webpage). Abstraction and Data Hiding. Data Abstraction: Captures essential qualities of an object and names the object. Ignores implementation details

Download Presentation

Abstraction and Data Hiding

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. Data StructuresCSCI 132, Spring 2014Lecture 2Classes and Abstract Data TypesRead Ch 1.1 - 1.6Read Style Guide (see course webpage)

  2. Abstraction and Data Hiding Data Abstraction: • Captures essential qualities of an object and names the object. • Ignores implementation details • Is necessary for managing large, complex software projects. Data Hiding: • Hides implementation details from user • Prevents user from accessing implementation directly • User must interact with object through an interface.

  3. Classes in C++ • A class is a means of abstraction in C++ • A class is a specification of a group of objects that all have the same essential properties FOR EXAMPLE . . .

  4. A class example class Student Properties (data members) name, graduation year, list of courses, number of courses Operations (methods) List Courses List Student Information Add a Course Set Graduation Date

  5. An object is an instance of a class For example: object studentA Name: Andrea Student Graduation Year: 2007 List of Courses: CSCI132, MATH331 Number of courses: 2

  6. class Student Specification // SPECIFICATION FILE ( student.h ) typedef char string9[10]; class Student // declares a class data type {// does not allocate memory public : // 5 public function members void AddCourse(string9 CourseName); void ListCourses(void) const; void ListInfo(void) const; void SetGradDate(int year); Student(); // Constructor Function Student(char name[ ], int year); private : // 4 private data members char studentName[30]; string9 courses[6]; int numCourses; int gradDate; } ;

  7. Use of C++ data typeclass • Facilitates re-use of C++ code for an ADT. • Software that uses the class is called a client. • Variables of the class type are called objects or instances of the class. • Client code uses public member functions to handle its class objects. • Private members cannot be directly accessed by client code. It can only be accessed by member functions.

  8. Client Code UsingStudent #include "student.h" // includes specification of the class #include <iostream> using namespace std; int main ( void ) { int newYear; string9 newCourse; Student newStudent; // create new Student object cout << "Enter graduation year: "; cin >> newYear; newStudent.SetGradDate(newYear); // set Graduation date cout << "Enter course to be added: "; cin >> newCourse; newStudent.AddCourse(newCourse); // Add a course newStudent.ListInfo(); // List Student information newStudent.ListCourses(); // List courses return 0; }// end of main

  9. class type declaration The class declaration creates a data type and names the members of the class. It does not allocate memory for any variables of that type! Client code still needs to declare class variables.

  10. C++ Data Type class represents an ADT • 2 kinds of class members: data members and function members • class members are private by default • data members are generally private • function members are generally declared public • private class members can be accessed only by the class member functions, not by client code.

  11. 2 separate files generally used forclass type // SPECIFICATION FILE ( student .h ) // Specifies the data and function members. class Student { public: . . . private: . . . } ; // IMPLEMENTATION FILE ( student.cc ) // Implements the Student member functions. . . .

  12. Implementation file for Student // IMPLEMENTATION FILE ( student.cc ) // Implements the Student member functions. #include "student.h" // also must appear in client code #include <iostream> using namespace std; . . . void Student :: SetGradDate ( /* in */ int year ) { gradDate = year; } . . .

  13. Class Constructors initialize Data Members // In the file student.cc Student :: Student () { //Default Constructor numCourses = 0; gradDate = 0; strcpy(studentName, "John Doe"); } // This is invoked in client code at time of object declaration: // Student newStudent("Mary Contrary", 2010); Student :: Student(char name[ ], int year) { //Constructor with parameters //We will fill this out in lab! }

  14. Function stubs • Stubs allow you to test code that relies on unwritten functions. The stub often has no code in the function definition: • void Student :: AddCourse(string9 courseName) { //empty }

  15. Function drivers Function drivers allow you to test class functions individually before writing the entire client code: int main (void) { Student newStudent; newStudent.listInfo( ); }

More Related