Classes and Data Abstraction. Topic 5

Similar documents
Introduction to Programming session 24

Classes and Data Abstraction

Classes and Data Abstraction. Topic 5

Chapter 6: Classes and Data Abstraction

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. 1

Classes: A Deeper Look

IS 0020 Program Design and Software Tools

More C++ Classes. Systems Programming

Computer Programming with C++ (21)

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

Fundamentals of Programming Session 24

clarity. In the first form, the access specifier public : is needed to {

IS 0020 Program Design and Software Tools

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

Classes and Data Abstraction: struct

1 // Fig. 6.13: time2.cpp 2 // Member-function definitions for class Time. 3 #include <iostream> Outline. 4 5 using std::cout; 6 7 #include <iomanip>

Classes and Objects. Types and Classes. Example. Object Oriented Programming

A Deeper Look at Classes

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

Pointer Assignments. CISC181 Introduction to Computer Science. Dr. McCoy. Lecture 18 October 29, The new Operator

I/O Streams and Standard I/O Devices (cont d.)

Chapter 6 Structures and Classes. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

Deitel Dive-Into Series: Dive-Into Cygwin and GNU C++

CHAPTER 3 ARRAYS. Dr. Shady Yehia Elmashad

Chapter 9 Classes : A Deeper Look, Part 1

Arrays. Outline. Multidimensional Arrays Case Study: Computing Mean, Median and Mode Using Arrays Prentice Hall, Inc. All rights reserved.

IS 0020 Program Design and Software Tools

The American University in Cairo Computer Science & Engineering Department CSCE Dr. KHALIL Exam II Spring 2010

C++ As A "Better C" Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

Lab 12 Object Oriented Programming Dr. John Abraham

Chapter 10 Defining Classes

Pointers and Strings Prentice Hall, Inc. All rights reserved.

Arrays. Week 4. Assylbek Jumagaliyev

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES II

Data Types (C/C++) Structures

Object Oriented Design

Classes: A Deeper Look. Systems Programming

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program) A few types

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

Chapter 15 - C++ As A "Better C"

57:017, Computers in Engineering C++ Classes

C++ basics Getting started with, and Data Types.

How to engineer a class to separate its interface from its implementation and encourage reuse.

Introduction to C++ Programming. Adhi Harmoko S, M.Komp

Chapter 16: Classes and Data Abstraction

An inline function is one in which the function code replaces the function call directly. Inline class member functions

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Pointers and Strings. Adhi Harmoko S, M.Komp

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

Problem Solving: Storyboards for User Interaction

this Pointer, Constant Functions, Static Data Members, and Static Member Functions this Pointer (11.1) Example of this pointer

Lecture 7. Log into Linux New documents posted to course webpage

Unit 1 : Principles of object oriented programming

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Dr. Khalil Exam II Fall 2011

Introduction to C++ (Extensions to C)

Getting started with C++ (Part 2)

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program)

Dot and Scope Resolution Operator

Data Structures using OOP C++ Lecture 3

CSC 330 Object Oriented Programming. Operator Overloading Friend Functions & Forms

Ch 6. Structures and Classes

Fundamentals of Programming CS-110. Lecture 2

Dr. Md. Humayun Kabir CSE Department, BUET

CS24 Week 3 Lecture 1

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

CMSC 202 Midterm Exam 1 Fall 2015

Functions and Recursion

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

CMPS 221 Sample Final

As an example using arrays, let s write some code to get started with the Upthrust game. We can use a 2D array to represent the game board.

Functions, Arrays & Structs

COMP322 - Introduction to C++

Fast Introduction to Object Oriented Programming and C++

CS242 COMPUTER PROGRAMMING

9.1 Introduction. Integrated Time class case study Preprocessor wrapper Three types of handles on an object. Class functions

Objectivities. Experiment 1. Lab6 Array I. Description of the Problem. Problem-Solving Tips

CS 141, Introduction to Computer Science Fall Midterm Exam

Computer Programming C++ Classes and Objects 6 th Lecture

C++ Programming: From Problem Analysis to Program Design, Third Edition

Chapter 2 - Control Structures

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

CPE 112 Spring 2015 Exam III (100 pts) April 8, True or False (12 Points)

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

Chapter 2 - Control Structures

Chapter 2 - Control Structures

CSCE Practice Midterm. Data Types

Chapter Five: Functions. by Cay Horstmann Copyright 2018 by John Wiley & Sons. All rights reserved

Chapter 2 - Control Structures

Classes: A Deeper Look, Part 2

Character Functions & Manipulators Arrays in C++ CS 16: Solving Problems with Computers I Lecture #10

Introduction to Programming

Computer Science II Lecture 1 Introduction and Background

1. HotDog 판매량출력프로그램. numsold = newnumsold; ID = newid; HotDogStand(); HotDogStand(int newid, int newnnumsold); return ID; void SetID(int newid);

Fundamentals of Programming Session 25

CSC1322 Object-Oriented Programming Concepts

CHAPTER 2.1 CONTROL STRUCTURES (SELECTION) Dr. Shady Yehia Elmashad

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.7. User Defined Functions II

Functions, Arrays & Structs

Partha Sarathi Mandal

Computer Programming Class Members 9 th Lecture

Transcription:

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 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 Object Data Operations Object Data Operations Object Data Operations

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

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 ID Name Major 1111 Nora CS 2222 Sara IS 3333 Mona CS Student: ID variable Student: Name variable Student: Major variable Student ID Name Major

Structures Think of a structure as an object without any member functions Object Variable Data Value Operations Member Functions 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 struct Student { int id; char name[10]; char major[2]; };

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

Structures Using Structures Declare: 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) 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) { using namespace std; 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 Define structure type Time with three integer members. Pass references to constant Time objects to eliminate copying overhead. fig06_01.cpp (1 of 3)

24 int main() 25 { Use dot operator to initialize structure members. 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 Direct access to data allows assignment of bad values. fig06_01.cpp (2 of 3)

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 << ":" Dinner 64 will << be setw( held 2 ) << at t.second 18:30:00 universal time, which 65 is 6:30:00 << ( t.hour PM < 12 standard? " AM" : " PM" time. ); 66 Time 67 with } // end invalid function printstandard values: 29:73:00 fig06_01.cpp (3 of 3) Use parameterized fig06_01.cpp stream manipulator setfill. output (1 of 1) Use dot operator to access data members.