 2003 Prentice Hall, Inc. All rights reserved. 1 6.1 Introduction Object-oriented programming (OOP) –Encapsulates data (attributes) and functions (behavior)

Slides:



Advertisements
Similar presentations
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Advertisements

Class and Objects.
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.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
 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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
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.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 11 Structure. 2 Objectives You should be able to describe: Structures Arrays of Structures Structures as Function Arguments Dynamic Structure.
1 Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 4 - Classes Jan 27,
Review of C++ Programming Part II Sheng-Fang Huang.
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.
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;
Structured Data Types array array union union struct struct class class.
計算機程式語言 Lecture 13-1 國立臺灣大學生物機電系 13 Structures.
 2003 Prentice Hall, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship.
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 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
1 Classes and Data Abstraction Part I Introduction Object-oriented programming (OOP)  Encapsulates data (attributes) and functions (behavior)
 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.
Structured Data Types struct class Structured Data Types array – homogeneous container collections of only one type struct – heterogeneous data type.
A FIRST BOOK OF C++ CHAPTER 16 DATA STRUCTURES. OBJECTIVES In this chapter, you will learn about: Single Structures Arrays of Structures Structures as.
Structures - Part II aggregate operations arrays of type struct nested structures compared to classes.
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 CISC181 Introduction to Computer Science Dr. McCoy Lecture 25 December 1, 2009.
Chapter 7: Arrays. Outline Array Definition Access Array Array Initialization Array Processing 2D Array.
 2003 Prentice Hall, Inc. All rights reserved. 1 Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 4: Classes Jan 27,
1 Lecture 6 Classes and Data Abstraction: Part II.
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 20 November 10, 2009.
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 5: Pointer Outline Chapter 5 Pointer continue Call by reference Pointer arithmatic Debugging.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
 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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
 2000 Prentice Hall, Inc. All rights reserved. NHTI CP 107 Fall 2001 M. Saleem Yusuf Introduction Object-oriented programming (OOP) –Objects Encapsulates.
C Structs Programming in C++ Fall 2008 Dr. David A. Gaitros
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Chapter 9 Classes: A Deeper Look, Part 1 Seventh Edition C++ How to Program © by Pearson Education, Inc. All Rights Reserved.
 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.
Topic 4 Data Structures Program Development and Design Using C++, Third Edition.
Procedural and Object-Oriented Programming
Lecture 5: Classes (continued) Feb 1, 2005
Classes and Data Abstraction
Chapter 16: Classes and Data Abstraction
Structures - Part II aggregate operations arrays of type struct
Classes: A Deeper Look Outline
Lecture 5: Classes September 14, 2004
Classes and Data Abstraction
Chapter 6: Classes and Data Abstraction
Structured Data Types array union struct class.
Chapter 6: Classes and Data Abstraction
Chapter 6: Classes and Data Abstraction
Chapter 6: Classes and Data Abstraction
Presentation transcript:

 2003 Prentice Hall, Inc. All rights reserved Introduction Object-oriented programming (OOP) –Encapsulates data (attributes) and functions (behavior) into packages called classes 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

Structured Data Types ] struct = an abstract data type with a fixed number of components that are accessed by name, not by index.

 2003 Prentice Hall, Inc. All rights reserved Structure Definitions Structures –Aggregate data types built using elements of other types struct Time { int hour; int minute; int second; }; Structure member naming –In same struct : must have unique names –In different struct s: can share name struct definition must end with semicolon Structure tag Structure members

 2003 Prentice Hall, Inc. All rights reserved Structure Definitions Self-referential structure –Structure member cannot be instance of enclosing struct –Structure member can be pointer to instance of enclosing struct (self-referential structure) Used for linked lists, queues, stacks and trees struct definition –Creates new data type used to declare variables –Structure variables declared like variables of other types –Examples: Time timeObject; Time timeArray[ 10 ]; Time *timePtr; Time &timeRef = timeObject;

 2003 Prentice Hall, Inc. All rights reserved Accessing Structure Members Member access operators –Dot operator (. ) for structure and class members –Arrow operator ( -> ) for structure and class members via pointer to object –Print member hour of timeObject : cout << timeObject.hour;

Can Assign Aggregate Values in the declaration Date myBirth = {2, 29, 1963}; Date today = {11, 25, 1999}; Date bill_Date = {3, 25, 2001}; Date lily_Bday = {1, 20, 1995};

Assigning Values myBirth. month = 2; myBirth. day = 29; myBirth. year = 1963; bill_Date.month = 3; bill_Date.day = 25; lily_Bday.year = 1995; today.month = 4; * instance of Date member of myBirth instance

Alternate assignments: struct { int month; int day; int year; } mybirth,billday,today; Variables of type struct struct Date { int month; int day; int year; }; Date mybirth, billday,today;

 2003 Prentice Hall, Inc. All rights reserved. Structures as Arguments // in a prototype int Overdue(Date, int); * * * * // in a function call Overdue(today, bill_Date.year); // in a function header int Overdue(Date now, int purchased)

 2003 Prentice Hall, Inc. All rights reserved. Aggregate Operations OperationArrays Structs I/ONo ( except strings )No AssignmentNo Yes ArithmeticNo No ComparisonNo No Parameter pass.Ref. only Either value or ref. Funct. return value No Yes

 2003 Prentice Hall, Inc. All rights reserved Implementing a User-Defined Type Time with a struct Default: structures passed by value –Pass structure by reference Avoid overhead of copying structure C-style structures –No “interface” If implementation changes, all programs using that struct must change accordingly –Cannot print as unit Must print/format member by member –Cannot compare in entirety Must compare member by member

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

 2003 Prentice Hall, Inc. All rights reserved. Outline 13 fig06_01.cpp (2 of 3) 24 int main() 25 { 26 Time dinnerTime; // variable of new type Time 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 cout << "Dinner will be held at "; 33 printUniversal( dinnerTime ); 34 cout << " universal time,\nwhich is "; 35 printStandard( dinnerTime ); 36 cout << " standard time.\n"; dinnerTime.hour = 29; // set hour to invalid value 39 dinnerTime.minute = 73; // set minute to invalid value cout << "\nTime with invalid values: "; 42 printUniversal( dinnerTime ); 43 cout << endl; return 0; } // end main 48 Use dot operator to initialize structure members. Direct access to data allows assignment of bad values.

 2003 Prentice Hall, Inc. All rights reserved. Outline 14 fig06_01.cpp (3 of 3) fig06_01.cpp output (1 of 1) 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; } // end function printUniversal // print time in standard-time format 59 void printStandard( const Time &t ) 60 { 61 cout << ( ( t.hour == 0 || t.hour == 12 ) ? : t.hour % 12 ) << ":" << setfill( '0' ) 63 << setw( 2 ) << t.minute << ":" 64 << setw( 2 ) << t.second 65 << ( t.hour < 12 ? " AM" : " PM" ); } // end function printStandard 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 Use parameterized stream manipulator setfill. Use dot operator to access data members.

Arrays of Structures struct Salaried { char dept[5]; int salary; int vac_days; }; // array of 20 elements, each of type Salaried Salaried emp[20];

Arrays of Structures emp[0].dept = “Purc”; emp[0].salary = 34560; emp[0].vac_days = 14;... emp[2].salary = 32100; emp[2].dept = “Ship”; emp[2].vac_days = 10; … emp[19].dept = “Acct”; emp[19].salary = 22500; emp[19].vac_days = 12;

Arrays of Structures struct Payroll { int id; char name[15]; double payrate; }; // an array of 3 records of type Payroll Payroll employee[3];

Arrays of Structures // load array -- there are other ways to load the array Payroll employee[3] = { {11, “Mickey”, 7.25}, {12, “Minny”, 6.50}, {13, “Donald”, 9.00} }; // display array for(ndx = 0; ndx < 3; ndx++) cout << ‘\n’ << employee[ndx].id << setw(20) << employee[ndx].name << setw(20) << employee[ndx].payrate;

Arrays of Structures int main() { //declare array of 3 records of type Payroll // and initialize the first record Payroll employee[3] = { 11, “Mickey", 7.25 }; loadarray(employee);// calls showarray(employee); cout << endl<<endl;// for formatting } // end main()

Arrays of Structures // load array - data typically entered via file input void loadarray(Payroll staff[3]) { // begin at 1 because [0] already entered for(int ndx = 1; ndx < 3; ndx++) { cout << "Enter the ID, name, and pay rate: "; cin >> staff[ndx].id >> staff[ndx].name >> staff[ndx].payrate; cout << endl; } }

Nested Structures struct Date {int month; int day; int year; }; struct vital_Data {char name[15]; char owner[10]; int ID; Date birth; // Date must be previously defined double strength; }; *

Nested Structures // declaration of an object vital_Data Digimon; // declaration of an object // assignments of data to an object strcpy(Digimon.name,“wormmon”); strcpy(Digimon.owner,“ken”); Digimon.ID = 1234; Digimon.birth.day= 10;// this is a struct Digimon.evolution = 12.75;

#include using namespace std; struct Employee { int idNum; double payRate; double hours; }; Employee getValues(); int main() { Employee emp; emp = getValues(); cout << "\nThe employee id number is " << emp.idNum << "\nThe employee pay rate is $" << emp.payRate << "\nThe employee hours are " << emp.hours << endl; return 0; } //return a struct Employee getValues() { Employee next; next.idNum = 6789; next.payRate = 16.25; next.hours = 38.0; return next; }

 2003 Prentice Hall, Inc. All rights reserved. 24 Pointers to structures –Print member hour of timeObject : cout << timeObject.hour; OR timePtr = &timeObject; cout hour; –timePtr->hour same as ( *timePtr ).hour Parentheses required –* lower precedence than.

Pointers to Structures struct Employee { int idNum; double payRate; double hours; } Employee *pt; (*pt).idnum pt->idnum “the structure whose address is in pt” (*pt).payRate pt->payRate (*pt).hours pt->hours Alternate notation Use: Declaration:

 2003 Prentice Hall, Inc. All rights reserved. 26 Structures which include self referential pointers For linked lists

Self referential structures The records are linked by including the address of the next record (I.e. a pointer). struct Beers { char beername[10]; Beer *nextbeer; } Pointer to another structure

Main () { Beer t1 = {“bud”}; Beer t2 = {“miller”}; Beer t3 = {“coors”}; Beer *first; first = &t1; t1.nextbeer = &t2; t2.nextbeer = &t3; t3.nextbeer = NULL; //sets pointer to zero cout beername name name; Bud 0xff1Miller 0xaf3Coors \0 0xff1 0xaf3 t1 t2 t3 0xaaa first 0xaaa

Bud 0xff1Miller 0xaf3Coors \0 0xff1 0xaf3 t1 t2 t3 0xaaa first 0xaaa first = first->nextbeer; cout beername<<endl; first = first->nextbeer; cout beername<<endl; first = first->nextbeer; cout beername<<endl; \0 0xff10xaf3 ****

4 p,x 7 NULL q,y el next struct nodetype { int el; nodetype *next; }; nodetype *p, *q, x,y,z; p= &x; p->el = 4; q = &y; q->el = 7; q->next = NULL; p->next = q; cout el << endl; p=p->next; cout el << endl; q 4 7 NULL p q 4 p ***

Dynamic allocation new reserves the number of bytes required by the requested data type and returns the address of the first reserved location or NULL if there is no memory delete releases the block of bytes

(1) p = new nodetype; (2) q = p; (3) p->el = 4; (4) r = new nodetype; (5) r->el = 9; (6) s = new nodetype; (7) s->el = q->el + p->el; (8) p->next = r; (9) r->next = s; (10) r->el = 3.0; (11) s=p; (12) q = q->next; (13) q->el = 6.0; (14) q = q->next; (15) cout el << endl; (16) cout el << endl; (17) cout el << endl; (18) cout el << endl; p q 4 |9 | r 13 | s s p q 4 |3 | r 8 | s p 4 |6 | r q 8 | q s p 4 |6 | r 8 | final *****