Classes and Data Abstraction

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
C++ Classes & Data Abstraction
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Overview 10.1 Structures 10.2 Classes 10.3 Abstract Data Types.
 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.
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.
 2003 Prentice Hall, Inc. All rights reserved Introduction Object-oriented programming (OOP) –Encapsulates data (attributes) and functions (behavior)
C++ fundamentals.
1 Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University.
Chapter 11: Structured Data. Slide Introduction An array makes it possible to access a list or table of data of the same data type by using a single.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
Chapter 10 Defining Classes. User-defined Types Are data types defined by programmers. Include: – typedef: simple data definition – struct: a structure.
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;
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Structures  2 nd aggregate data type: struct  Recall:
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 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.
Structures (L30) u Syntax of a struct Declaration u Structure Variables u Accessing Members of Structures u Initialize Structure Variables u Array of Structures.
 2003 Prentice Hall, Inc. All rights reserved. 1 Vectors.
Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University 1
1 Lecture 6 Classes and Data Abstraction: Part II.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
 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.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 7: Classes Part II Outline 7.1 Introduction 7.2 const (Constant) Objects and const Member Functions.
 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.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
 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.
CSIS 123A Lecture 1 Intro To Classes Glenn Stevenson CSIS 113A MSJC.
Chapter 16: Classes and Data Abstraction
Structures and Classes
Introduction to C++ (Extensions to C)
What header file contains C++ file I/O instructions?
Lecture 5: Classes (continued) Feb 1, 2005
CO1401 Program Design and Implementation
Chapter 16: Classes and Data Abstraction
Andy Wang Object Oriented Programming in C++ COP 3330
CSC 113: Computer Programming (Theory = 03, Lab = 01)
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
Classes: A Deeper Look Outline
Lecture 5: Classes September 14, 2004
Chapter 6: Classes and Data Abstraction
Classes and Data Abstraction
Learning Objectives Classes Constructors Principles of OOP
4.9 Multiple-Subscripted Arrays
Chapter 6: Classes and Data Abstraction
Starting to think about objects...
Chapter 6: Classes and Data Abstraction
UNIT I OBJECT ORIENTED PROGRAMMING FUNDAMENTALS
CPS120: Introduction to Computer Science
Glenn Stevenson CSIS 113A MSJC
Capitolo 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
Arrays Arrays A few types Structures of related data items
Programming Structures.
Chapter 6: Classes and Data Abstraction
Chapter 6: Classes and Data Abstraction
Chapter 3 Arrays Dr. A. PHILIP AROKIADOSS Assistant Professor
Programming Structures.
Vectors.
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

Classes and Data Abstraction Topic 5

Introduction Object-oriented programming (OOP) Encapsulates data (attributes) and functions (behavior) into packages called classes The data and functions of a class are intimately tied together. A class is like a blue Print, With the help of blue print builder can make a house, out of a class a programmer can create an object.

Introduction One blue print can be reused many times to make many houses, similarly one class can be reused many times to make many objects of the same class. Procedural language programming tends to be action oriented, c++ programming is object oriented. The unit of the programming in c is the function where in C++ is the class Classes are also referred to as programmer defined types.

Introduction Information hiding Each class contains data as well as the set of functions that manipulate the data. The data components of a class are called data members. The function components of a class are called member function. Information hiding Class objects communicate across well-defined interfaces Implementation details hidden within classes themselves User-defined (programmer-defined) types: classes Data (data members) Functions (member functions or methods) Similar to blueprints – reusable Class instance: object

What are Classes? Class Data Operations Data Operations Data Object Data Operations Object Data Operations Object

What are Classes? Class User Defined Data Type Data Value Operations Member Functions Object Variable Data Value Operations Member Functions Object Variable Data Value Operations Member Functions Object Variable

What are Classes? A class is a data type You can use classes in the same way you use predefined data types (int, char, etc.) Defining your class the right way is important for it to behave like predefined data types  Abstract Data Type (ADT) An ADT is a user-defined data type that is well behaved as the predefined data types

Structures A data structure that can be used to store related data items with different types. The individual components of a struct is called a member.

Structures Students Student Student: ID variable Name Major 1111 Nora CS 2222 Sara IS 3333 Mona Student ID Name Major Student: ID variable Student: Name variable Student: Major variable

Structures Think of a structure as an object without any member functions Data Value Operations Member Functions Object Variable Here, we’ll have values of different data types that we would like to treat as a single item.

Structures How do I…. Define a structure? Use a structure? Student ID Name Major How do I…. Define a structure? Use a structure? struct Student { int id; char name[10]; char major[2]; };

Structures Syntax: struct Structure_Tag { Type1 Member_Variable1; Typen Member_Variablen; };

Structures Using Structures Declare: Assignment: Student1 = Student2; StudentRecord Student1, Student2; Assignment: Student1 = Student2; Student1.id = Student2.id; Student1.grade = Student2.grade; Read: cin >> Student1.id; Write: cout << Student1.id; Initialize: Student1 = {666,’A’}

Structures Syntax: Structure_Variable_Name.Member_Variable_Name Example: struct StudentRecord { int id; char grade; }; int main () StudentRecord Student1; Student1.id = 555; Student1.grade = ‘B’; cout<< Student1.id<< ‘, ‘<< Student1.grade<<endl; } Dot Operator

Structures Two or more structure types may use the same member names struct FertilizerStock { double quantity; double nitrogen_content; }; FertilizerStock Item1; Item1.quantity struct CropYield { int quantity; double size; }; CropYield Apples; Apples.quantity

Structures Structures within structures (nested) Employee person1; struct Date { int month; int day; int year; }; struct Employee { int id; Date birthday; }; Employee person1; cout<< person1.birthday.year;

Structures #include <iostream> struct StudentRecord { int id; char grade; }; StudentRecord Get_Data (StudentRecord in_student); int main () { using namespace std; StudentRecord Student1; Student1 = Get_Data (Student1); cout<< Student1.id<< ","<<Student1.grade<< endl; return 0; } StudentRecord Get_Data (StudentRecord in_student) cout<<"Enter ID: "; cin>> in_student.id; cout<<"Enter Grade: "; cin>> in_student.grade; return (in_student);

1 // Fig. 6.1: fig06_01.cpp 2 // Create a structure, set its members, and print it. 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 #include <iomanip> 9 10 using std::setfill; 11 using std::setw; 12 13 // structure definition 14 struct Time { 15 int hour; // 0-23 (24-hour clock format) 16 int minute; // 0-59 17 int second; // 0-59 18 19 }; // end struct Time 20 21 void printUniversal( const Time & ); // prototype 22 void printStandard( const Time & ); // prototype 23 fig06_01.cpp (1 of 3) Define structure type Time with three integer members. Pass references to constant Time objects to eliminate copying overhead.

24 int main() 25 { 26 Time dinnerTime; // variable of new type Time 27 28 dinnerTime.hour = 18; // set hour member of dinnerTime 29 dinnerTime.minute = 30; // set minute member of dinnerTime 30 dinnerTime.second = 0; // set second member of dinnerTime 31 32 cout << "Dinner will be held at "; 33 printUniversal( dinnerTime ); 34 cout << " universal time,\nwhich is "; 35 printStandard( dinnerTime ); 36 cout << " standard time.\n"; 37 38 dinnerTime.hour = 29; // set hour to invalid value 39 dinnerTime.minute = 73; // set minute to invalid value 40 41 cout << "\nTime with invalid values: "; 42 printUniversal( dinnerTime ); 43 cout << endl; 44 45 return 0; 46 47 } // end main 48 Use dot operator to initialize structure members. fig06_01.cpp (2 of 3) Direct access to data allows assignment of bad values.

49 // print time in universal-time format 50 void printUniversal( const Time &t ) 51 { 52 cout << setfill( '0' ) << setw( 2 ) << t.hour << ":" 53 << setw( 2 ) << t.minute << ":" 54 << setw( 2 ) << t.second; 55 56 } // end function printUniversal 57 58 // print time in standard-time format 59 void printStandard( const Time &t ) 60 { 61 cout << ( ( t.hour == 0 || t.hour == 12 ) ? 62 12 : t.hour % 12 ) << ":" << setfill( '0' ) 63 << setw( 2 ) << t.minute << ":" 64 << setw( 2 ) << t.second 65 << ( t.hour < 12 ? " AM" : " PM" ); 66 67 } // end function printStandard fig06_01.cpp (3 of 3) fig06_01.cpp output (1 of 1) Use parameterized stream manipulator setfill. Use dot operator to access data members. Dinner will be held at 18:30:00 universal time, which is 6:30:00 PM standard time.   Time with invalid values: 29:73:00