abstraction and data hiding

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

Upload: minerva-shelton

Post on 02-Jan-2016

24 views

Category:

Documents


0 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Abstraction and Data Hiding

1

Data Structures

CSCI 132, Spring 2014Lecture 2

Classes and Abstract Data TypesRead Ch 1.1 - 1.6

Read Style Guide (see course webpage)

Page 2: Abstraction and Data Hiding

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.

Page 3: Abstraction and Data Hiding

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 . . .

Page 4: Abstraction and Data Hiding

4

A class example

classStudent

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

Page 5: Abstraction and Data Hiding

5

An object is an instance of a class

For example:

objectstudentA

Name: Andrea Student

Graduation Year: 2007

List of Courses: CSCI132, MATH331

Number of courses: 2

Page 6: Abstraction and Data Hiding

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 membersvoid AddCourse(string9 CourseName);void ListCourses(void) const;void ListInfo(void) const;void SetGradDate(int year);Student(); // Constructor FunctionStudent(char name[ ], int year);

private : // 4 private data memberschar studentName[30];string9 courses[6];int numCourses;int gradDate;

} ;

Page 7: Abstraction and Data Hiding

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.

Page 8: Abstraction and Data Hiding

8

#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 datecout << "Enter course to be added: ";cin >> newCourse;newStudent.AddCourse(newCourse); // Add a coursenewStudent.ListInfo(); // List Student informationnewStudent.ListCourses(); // List coursesreturn 0;

} // end of main

Client Code Using Student

Page 9: Abstraction and Data Hiding

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.

Page 10: Abstraction and Data Hiding

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.

Page 11: Abstraction and Data Hiding

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.

. . .

Page 12: Abstraction and Data Hiding

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; }

. . .

Page 13: Abstraction and Data Hiding

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!

}

Page 14: Abstraction and Data Hiding

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

}

Page 15: Abstraction and Data Hiding

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

}