CSCI-383 Object-Oriented Programming & Design Lecture 5.

Slides:



Advertisements
Similar presentations
1 Classes Object-oriented programming: Model the problem as a collection of objects that have certain attributes and interact with one another and/or the.
Advertisements

True or false A variable of type char can hold the value 301. ( F )
Computer Science 1620 Loops.
Computer Science 1620 Programming & Problem Solving.
1 Lecture 29 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Computer Science 1620 Arrays. Problem: Given a list of 5 student grades, adjust the grades so that the average is 70%. Program design: 1. read in the.
Libraries Programs that other people write that help you. #include // enables C++ #include // enables human-readable text #include // enables math functions.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Functions Modules in C++ are called functions and classes
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
Basic Elements of C++ Chapter 2.
Programming Introduction to C++.
Programming Languages and Paradigms Object-Oriented Programming.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
C++ Classes and Data Structures Jeffrey S. Childs
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Object-Oriented Programming in C++
Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 5 An Array Class Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 3 More About Classes Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
CSCI 383 Object-Oriented Programming & Design Lecture 6 Martin van Bommel.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
1 Lecture 19 Structs HW 5 has been posted. 2 C++ structs l Syntax:Example: l Think of a struct as a way to combine heterogeneous data values together.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Section 4 - Functions. All of the programs that we have studied so far have consisted of a single function, main(). However, having more than one function.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 2 Overloaded Operators, Class Templates, and Abstraction Jeffrey S. Childs Clarion University.
Exam Format  90 Total Points  60 Points Writing Programs  25 Points Tracing Code/Algorithms and determining results  5 Points Short Answer  Similar.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
C++ Classes and Data Structures Jeffrey S. Childs
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Lecture 7: Making Decisions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
1 Strings, Classes, and Working With Class Interfaces CMPSC 122 Penn State University Prepared by Doug Hogan.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
By Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 13 More on Classes Chapter 8 Week 13 More on Classes Chapter 8.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Lecture 12: Functions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Friend Functions.  An ordinary function that is given special access to the private members of a class  NOT a member function of the class  Prototype.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
Functions BICSE-6A Mr. Naeem Khalid Lecturer, Dept. of Computing.
1 Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch Read Style Guide (see course webpage)
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Class & Objects C++ offers another user-defined data type known class which is the most important feature of the object-oriented programming. A class can.
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.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 Pass-by-Value - default passing mechanism except.
1 C++ Classes and Data Structures Course link…..
LECTURE 3 PASS BY REFERENCE. METHODS OF PASSING There are 3 primary methods of passing arguments to functions:  pass by value,  pass by reference, 
Chapter 3 Selection Statements
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Basic Elements of C++.
Basic Elements of C++ Chapter 2.
DATA STRUCTURE : DAT JENIS DATA DAN JENIS DATA ABSTRAK (4JAM)
Chapter 15 Pointers, Dynamic Data, and Reference Types
Functions.
Programming Introduction to C++.
Presentation transcript:

CSCI-383 Object-Oriented Programming & Design Lecture 5

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 2 Structs A struct holds data, like an array Each unit of data in a struct is called a data member (or member) –they are called “elements” in arrays In a struct, each data member can have a different data type –in arrays, the data type of each element is the same

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 3 Example Using a Struct 1 #include 2 #include 3 #include 4 5 using namespace std; 6 7 struct CarType { 8string maker; 9int year; 10float price; 11 }; void getYourCar( CarType & car ); 14 Don’t forget this semicolon.

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 4 15 int main( ) 16 { 17 CarType myCar, yourCar; myCar.maker = "Mercedes"; // I wish 20 myCar.year = 2005; 21 myCar.price = ; 22 Example Using a Struct (cont.)

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 5 23 getYourCar( yourCar ); cout << "Your car is a: " << yourCar.maker << endl; 26 cout << fixed << showpoint << setprecision( 2 ) << 27 "I'll offer $" << yourCar.price << 28 " for your car." << endl; return 0; 31 } 32 Example Using a Struct (cont.)

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 6 33 void getYourCar( CarType & car ) 34 { 35cout << "Enter your maker: "; 36cin >> car.maker; 37cout << "Enter the year: "; 38cin >> car.year; 39cout << "Enter the price: $"; 40cin >> car.price; 41 } Example Using a Struct (cont.)

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 7 Object Assignment An object of a struct can be assigned to another object of the same struct type: myCar = yourCar; This assigns each data member in yourCar to the corresponding data member of myCar Also assigns any array data members

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 8 Classes A class is similar to a struct A class contains data members, but it also contains function members Objects are made from classes, similarly to the way that objects are made from structs The main program communicates with the objects –data is passed from main program to object and from object back to the main program

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 9 Main Program Using Objects Object A Object B Object C Main Program

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 10 Main Program Using Objects Object A Object B Object C Main Program

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 11 Main Program Using Objects Object A Object B Object C Main Program

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 12 Main Program Using Objects Object A Object B Object C Main Program

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 13 Main Program Using Objects Object A Object B Object C Main Program

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 14 Main Program Using Objects Object A Object B Object C Main Program

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 15 Main Program Using Objects Object A Object B Object C Main Program

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 16 Main Program Using Objects Object A Object B Object C Main Program

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 17 Main Program Using Objects Object A Object B Object C Main Program

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 18 How the Main Program Uses A Class Object The main program does not access the data within a class object The main program only accesses the functions of a class object –communication occurs by passing data as parameters into the object’s function –the object passes data to the main program through its return type

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 19 Main Program and Object Main Program Object public: functions private: data

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 20 Main Program Calls a Function in the Object Main Program Object public: functions private: data

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 21 The Function Accesses Data Main Program Object public: functions private: data

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 22 Function Returns a Value Back to the Main Program Main Program Object public: functions private: data

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 23 Main Program Calls a Different Function Main Program Object public: functions private: data

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 24 Function Calls Another Function Main Program Object public: functions private: data

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 25 Second Function Accesses Data Main Program Object public: functions private: data

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 26 Second Function Returns Back to First Function Main Program Object public: functions private: data

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 27 First Function Accesses Data Main Program Object public: functions private: data

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 28 Function Returns Back to Main Program Main Program Object public: functions private: data

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 29 Example of a Class 1 class Checkbook 2 { 3 public: 4 void setBalance( float amount ); 5 bool writeCheck( float amount ); 6 void deposit( float amount ); 7 float getBalance( ); 8 float getLastCheck( ); 9 float getLastDeposit( ); 10 private: 11 float balance; 12 float lastCheck; 13 float lastDeposit; 14 }; This class definition is placed into its own file, called the class specification file, named checkbook.h (by convention)

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 30 Example of a Class (cont.) 1 class Checkbook 2 { 3 public: 4 void setBalance( float amount ); 5 bool writeCheck( float amount ); 6 void deposit( float amount ); 7 float getBalance( ); 8 float getLastCheck( ); 9 float getLastDeposit( ); 10 private: 11 float balance; 12 float lastCheck; 13 float lastDeposit; 14 }; The writeCheck function returns false if the amount of the check is greater than the balance; returns true otherwise.

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 31 Example of a Class (cont.) 1 class Checkbook 2 { 3 public: 4 void setBalance( float amount ); 5 bool writeCheck( float amount ); 6 void deposit( float amount ); 7 float getBalance( ); 8 float getLastCheck( ); 9 float getLastDeposit( ); 10 private: 11 float balance; 12 float lastCheck; 13 float lastDeposit; 14 }; Don’t forget the semicolon.

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall #include “checkbook.h” void Checkbook::setBalance( float amount ) 18 { 19balance = amount; 20 } Example of a Class (cont.) The function definitions are placed into a separate file called the class implementation file. This file would be called checkbook.cpp (by convention).

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall #include “checkbook.h” void Checkbook::setBalance( float amount ) 18 { 19balance = amount; 20 } Example of a Class (cont.) The balance variable is declared in the private section of the class definition.

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall #include “checkbook.h” void Checkbook::setBalance( float amount ) 18 { 19balance = amount; 20 } Example of a Class (cont.) Special notation for class function definitions

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall bool Checkbook::writeCheck( float amount ) 22 { 23if ( amount > balance ) 24return false; 25balance -= amount; 26lastCheck = amount; 27return true; 28 } Example of a Class (cont.)

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall void Checkbook::deposit( float amount ) 30 { 31balance += amount; 32lastDeposit = amount; 33 } Example of a Class (cont.)

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall float Checkbook::getBalance( ) 35 { 36return balance; 37 } float Checkbook::getLastCheck( ) 40 { 41return lastCheck; 42 } Example of a Class (cont.) end of checkbook.cpp

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 38 1 #include 2 #include 3 #include "checkbook.h" 4 5 using namespace std; 6 7 int menu( ); 8 9 const int CHECK = 1, DEPOSIT = 2, BALANCE = 3, QUIT = 4; int main( ) 12 { 13Checkbook cb; 14float balance, amount; 15int choice; A Program that Uses the Checkbook Class A main program that uses the Checkbook class is placed into a separate.cpp file

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 39 16cout << "Enter the initial balance: $"; 17cin >> balance; 18cb.setBalance( balance ); 19 20cout << fixed << showpoint << setprecision( 2 ); A Program that Uses the Checkbook Class (cont.)

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 40 21choice = menu( ); 22while ( choice != QUIT ) { 23 if ( choice == CHECK ) { 24 cout << "Enter check amount: $"; 25 cin >> amount; 26 if ( cb.writeCheck( amount ) ) 27cout << "Check accepted." << endl; 28 else { 29cout << "Your balance is not high "; 30cout << "enough for that check." << endl; 31} 32 } A Program that Uses the Checkbook Class (cont.) body of the while loop continues

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 41 33else if ( choice == DEPOSIT ) { 34 cout << "Enter deposit amount: $"; 35 cin >> amount; 36 cb.deposit( amount ); 37 cout << "Deposit accepted." << endl; 38 } A Program that Uses the Checkbook Class (cont.) body of the while loop continues

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall else { // must be a balance request 40 amount = cb.getBalance( ); 41 cout << "Your balance is: $" << amount << endl; 42 } choice = menu( ); 45} 46 47return 0; 48 } 49 A Program that Uses the Checkbook Class (cont.) end of while loop

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall int menu( ) 51 { 52int choice; 53 54cout << endl; 55cout << "1Write a check" << endl; 56cout << "2Make a deposit" << endl; 57cout << "3Get the balance" << endl; 58cout << "4Quit" << endl << endl; 59cout << "Enter a number between 1 and 4: "; 60cin >> choice; 61return choice; 62 } A Program that Uses the Checkbook Class (cont.)

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 44 Keep In Mind The data members of a class cannot be accessed by a main program The object always retains the current values of its data members, even when object code is no longer executing Each function of a class can use the data members of the class as though they have been declared within the function

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 45 If Data Members were Accessed Directly… Class int a; int b; int c; Main Program Suppose the variables of a class were accessed by 100 places in a program

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 46 If Data Members were Accessed Directly… (cont.) Class int arr[10] Main Program Then we need to change the way the data is repre- sented (for maintenance)

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 47 If Data Members were Accessed Directly… (cont.) Class int arr[10] Main Program We need to change 100 lines of code in the main program!

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 48 Data Members Should Be Private Class private: int a; int b; int c; Main Program Here, the main program calls foo from 100 places. int foo( int x )

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 49 Data Members Should Be Private (cont.) Class private: int a; int b; int c; Main Program Then foo accesses the private data members. int foo( int x )

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 50 Data Members Should Be Private (cont.) Class private: int arr[10] Main Program If the data needs to change, the body of foo will need to be rewritten. int foo( int x )

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 51 Data Members Should Be Private (cont.) Class private: int arr[10] Main Program However, the function call of foo and return type will stay the same. int foo( int x )

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 52 Data Members Should Be Private (cont.) Class private: int arr[10] Main Program No changes need to be made in the main program! int foo( int x )

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 53 Data Members Should Be Private (cont.) Class private: int arr[10] Main Program Program maintenance is easier this way… int foo( int x )

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 54 Data Members Should Be Private (cont.) Class private: int arr[10] Main Program especially if there is more than one program using the class. int foo( int x )

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 55 Struct vs. Class Functions can be placed in a struct, but only when necessary The public and private keywords can be left out of a class (rare) The public and private keywords can be placed into a struct (rare) So what is the difference between a struct and a class?

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 56 If public and private are not used in a struct, all data members are public by default If public and private are not used in a class, all data members are private by default Struct vs. Class (cont.)

Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall 57 Conventions By convention, we use structs when we want all data members to be public –structs are typically defined and used within the client’s program, not in a separate file –typically used for records of information By convention, we use classes when we want all data members to be private (for maintenance purposes)