160 likes | 349 Views
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
E N D
Data StructuresCSCI 132, Spring 2014Lecture 2Classes and Abstract Data TypesRead Ch 1.1 - 1.6Read Style Guide (see course webpage)
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.
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 . . .
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
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
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; } ;
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.
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
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.
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.
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. . . .
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; } . . .
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! }
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 }
Function drivers Function drivers allow you to test class functions individually before writing the entire client code: int main (void) { Student newStudent; newStudent.listInfo( ); }