Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 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)

Similar presentations


Presentation on theme: "1 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)"— Presentation transcript:

1 1 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)

2 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 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 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 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 7 Use of C++ data type class 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 8 #include "student.h" // includes specification of the class #include 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 Client Code Using Student

9 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 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 11 2 separate files generally used for class 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 12 Implementation file for Student // IMPLEMENTATION FILE ( student.cc ) // Implements the Student member functions. #include "student.h" // also must appear in client code #include using namespace std;... void Student :: SetGradDate ( /* in */ int year ) { gradDate = year; }...

13 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 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 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( ); }


Download ppt "1 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)"

Similar presentations


Ads by Google