1 Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University.

Slides:



Advertisements
Similar presentations
Department of Computer Engineering Faculty of Engineering, Prince of Songkla University 1 5 – Abstract Data Types.
Advertisements

C++ Classes & Data Abstraction
1 CS 201 Introduction to c++ (1) Debzani Deb. 2 History of C++ Extension of C (C++ for better C) Early 1980s: Bjarne Stroustrup (Bell Laboratories) Provides.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6: Classes and Data Abstraction Outline 6.1 Introduction 6.2 Structure Definitions 6.3 Accessing.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 16: Classes and Data Abstraction Outline 16.1Introduction.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 16: Classes and Data Abstraction Outline 16.1Introduction 16.2Implementing a Time Abstract Data.
Introduction to Classes and Data Abstraction
1 Review (Week2) - Control Structures C++ has only seven control structures: –Sequence structure Programs executed sequentially by default –Selection structures.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
 2003 Prentice Hall, Inc. All rights reserved Introduction Object-oriented programming (OOP) –Encapsulates data (attributes) and functions (behavior)
Classi - Esempi1 // SalesPerson class definition // Member functions defined in salesp.cpp #ifndef SALESP_H #define SALESP_H class SalesPerson { public:
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
1 C++ Structures Starting to think about objects...
C++ Classes & Object Oriented Programming. Object Oriented Programming  Programmer thinks about and defines the attributes and behavior of objects. 
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 14. User Defined Classes.
PART I CHAPTER 16 CS116 SENEM KUMOVA METİN 1. Structures Structures : Aggregate data types built using elements of other types struct Time { int hour;
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 24P. 1Winter Quarter C++ Lecture 24.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Introduction To Classes Chapter Procedural And Object Oriented Programming Procedural programming focuses on the process/actions that occur in a.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Data Structures Using C++1 Chapter 1 -Software Engineering Principles -ADT and Classes.
C++ Lecture 4 Tuesday, 15 July Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l.
1 Chapter 4 CLASSES AND OBJECTS. 2 Outlines Procedural vs. Object ‑ Oriented Programming C++ Structures versus C Structures Classes –Accessing Class Members.
SMIE-121 Software Design II School of Mobile Information Engineering, Sun Yat-sen University Lecture.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
1 Classes and Data Abstraction Part I Introduction Object-oriented programming (OOP)  Encapsulates data (attributes) and functions (behavior)
1 Classes and Data Abstraction 2 Objectives Understand encapsulation and data hiding Understand data abstraction and Abstract Data Types (ADTs) Create.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 6: Classes and Data Abstraction Outline 6.1Introduction 6.2Structure Definitions 6.3Accessing.
CLASSES : A DEEPER LOOK Chapter 9 Part I 1. 2 OBJECTIVES In this chapter you will learn: How to use a preprocessor wrapper to prevent multiple definition.
Class 3 (L33) u Client of a Class u Purpose of Public Members u Private Class Members u Controlling Access u Access Functions u Predicate Functions u Utility.
Structures (L30) u Syntax of a struct Declaration u Structure Variables u Accessing Members of Structures u Initialize Structure Variables u Array of Structures.
1 const and this Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures Series.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Class 4 (L34) u Constructors u Default Constructor u Example of Default Constructors u Destructors u Constructors Are Called in Global Scope u Constructors.
Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University 1
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
1 Lecture 6 Classes and Data Abstraction: Part II.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight – don’t be late! Notes and clarifications for Project.
EEL 3801 Part VI Fundamentals of C and C++ Programming Classes and Objects.
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 20 November 10, 2009.
 2000 Prentice Hall, Inc. All rights reserved. NHTI CP 107 Fall 2001 M. Saleem Yusuf Introduction Object-oriented programming (OOP) –Objects Encapsulates.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
1 Chapter 9 Introduction to Classes and Objects Program Development and Design Using C++, Third Edition.
 2003 Prentice Hall, Inc. All rights reserved. ECE 2552 Dr. Këpuska Summer 2004 from Dr. S. Kozaitis Spring 2003 slides 1 Summary of Chapter 6: Classes.
1 Example Original Array Array After 2 nd Pass Array After 1 st Pass Array After 3 rd Pass.
Chapter 16: Classes and Data Abstraction
Lecture 5: Classes (continued) Feb 1, 2005
Classes and Data Abstraction
Chapter 16: Classes and Data Abstraction
Topic 7 Introduction to Classes and Objects
C++ Classes & Object Oriented Programming
Classes: A Deeper Look Outline
Lecture 5: Classes September 14, 2004
Chapter 6: Classes and Data Abstraction
Classes and Data Abstraction
Introduction to Classes and Objects
Chapter 6: Classes and Data Abstraction
Starting to think about objects...
Object-oriented programming (OOP)
Chapter 6: Classes and Data Abstraction
CS360 Client/Server Programming Using Java
#include <iostream.h>
Chapter 6: Classes and Data Abstraction
Chapter 6: Classes and Data Abstraction
Separating Interface from Implementation
Presentation transcript:

1 Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University

2 Contents n 1. What is Data Abstraction? n 2. C++: Classes and Data Abstraction n 3. Implementing a User-Defined Type Time with a Struct n 4. Implementing a Time Abstract Data Type with a Class n 5. Classes as User-Defined Types n 6. Using Constructors

3 1. What is Data Abstraction? n Abstract Data Types (ADTs) –type implementation & operations –hidden implementation n types are central to problem solving n a weapon against complexity n built-in and user-defined types are ADTs

4 Clients and Manufacturers client Implementation Interface manufacturer’s responsibility ADT use client

5 Benefits n Manufacturer Benefits: –easy to modify, maintain –profitable –reusable n Client Benefits: –simple to use, understand –familiar –cheap –component-based

6 How Well are ADTs Supported in C? n Does C enforce the use of the ADTs interface and the hiding of its implementation? n No

7 C++ (Chapters 15-21) n C++ is a superset of C, which has added features to support object-oriented programming. n C++ supports classes. –things very like ADTs

8 2. C++: Classes and Data Abstraction n C++ supports Object-Oriented Programming (OOP). n OOP models real-world objects with software counterparts. n OOP encapsulates data (attributes) and functions (behavior) into packages called objects. n Objects have the property of information hiding.

9 n Objects communicate with one another across interfaces. n The interdependencies between the classes are identified –makes use of –a part of –a specialisation of –a generalisation of –etc

10 C and C++ n C programmers concentrate on writing functions. n C++ programmers concentrate on creating their own user-defined types called classes. Classes in C++ are a natural evolution of the C notion of struct.

11 3. Implementing a User-Defined Type Time with a Struct // FIG16_1.CPP // Create a structure, set its members, and print it. #include struct Time { // structure definition int hour; // 0-23 int minute; // 0-59 int second; // 0-59 }; void printMilitary(const Time &); // prototype void printStandard(const Time &); // prototype

12 main() { Time dinnerTime; // variable of new type Time // set members to valid values dinnerTime.hour = 18; dinnerTime.minute = 30; dinnerTime.second = 0; cout << "Dinner will be held at "; printMilitary(dinnerTime); cout << " military time,\nwhich is "; printStandard(dinnerTime); cout << " standard time." << endl;

13 // set members to invalid values dinnerTime.hour = 29; dinnerTime.minute = 73; dinnerTime.second = 103; cout << "\nTime with invalid values: "; printMilitary(dinnerTime); cout << endl; return 0; }

14 // Print the time in military format void printMilitary(const Time &t) { cout << (t.hour < 10 ? "0" : "") << t.hour << ":" << (t.minute < 10 ? "0" : "") << t.minute << ":" << (t.second < 10 ? "0" : "") << t.second; }

15 // Print the time in standard format void printStandard(const Time &t) { cout << ((t.hour == 0 || t.hour == 12) ? 12 : t.hour % 12) << ":" << (t.minute < 10 ? "0" : "") << t.minute << ":" << (t.second < 10 ? "0" : "") << t.second << (t.hour < 12 ? " AM" : " PM"); }

16 Comments n Initialization is not required --> can cause problems. n A program can assign bad values to members of Time. If the implementation of the struct is changed, all the programs that use the struct must be changed. [No “interface”]

17 4. Implementing a Time Abstract Data Type with a Class #include // Time abstract data type (ADT) definition class Time { public: Time(); // default constructor void setTime(int, int, int); void printMilitary(); void printStandard(); private: int hour; // int minute; // int second; // };

18 // Time constructor initializes each data member to zero. // Ensures all Time objects start in a consistent state. Time::Time() { hour = minute = second = 0; } // Set a new Time value using military time. // Perform validity checks on the data values. // Set invalid values to zero (consistent state) void Time::setTime(int h, int m, int s) { hour = (h >= 0 && h < 24) ? h : 0; minute = (m >= 0 && m < 60) ? m : 0; second = (s >= 0 && s < 60) ? s : 0; }

19 // Print Time in military format void Time::printMilitary() { cout << (hour < 10 ? "0" : "") << hour << ":" << (minute < 10 ? "0" : "") << minute << ":" << (second < 10 ? "0" : "") << second; } // Print time in standard format void Time::printStandard() { cout << ((hour == 0 || hour == 12) ? 12 : hour % 12) << ":" << (minute < 10 ? "0" : "") << minute << ":" << (second < 10 ? "0" : "") << second << (hour < 12 ? " AM" : " PM"); }

20 // Driver to test simple class Time main() { Time t; // instantiate object t of class Time cout << "The initial military time is "; t.printMilitary(); cout << "\nThe initial standard time is "; t.printStandard(); t.setTime(13, 27, 6); cout << "\n\nMilitary time after setTime is "; t.printMilitary(); cout << "\nStandard time after setTime is "; t.printStandard();

21 t.setTime(99, 99, 99); // attempt invalid settings cout << "\n\nAfter attempting invalid settings:\n" << "Military time: "; t.printMilitary(); cout << "\nStandard time: "; t.printStandard(); cout << endl; return 0; }

22 Output n The initial military time is 00:00:00 n The initial standard time is 12:00:00 AM n Military time after setTime is 13:27:06 n Standard time after setTime is 1:27:06 PM n After attempting invalid settings: n Military time: 00:00:00 n Standard time: 12:00:00 AM

23 Comments hour, minute, and second are private data members. They are normally not accessible outside the class. [Information Hiding] n Use a constructor to initiailize the data members. This ensures that the object is in a consistate state when created. n Outside functions set the values of data members by calling the setTime method, which provides error checking.

24 5. Classes as User-Defined Types n Once the class has been defined, it can be used as a type in declarations as follows: Time sunset, //object of type Time arrayOfTimes[5], //array of Time objects *pointerToTime, //pointer to a Time object

25 6. Using Constructors n Constructors can be overloaded, providing several methods to initialize a class. n Time example: Interface Time(); Time(int hr, int min, int sec); Implementation Time::Time() { hour = minute = second = 0; } Time::Time(int hr, int min, int sec) { setTime(hr, min, sec); }

26 Use: Time t1; Time t2(08,15,04);

27