1 C++ Classes and Data Structures Course link…..

Slides:



Advertisements
Similar presentations
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Advertisements

Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
Computer Science 1620 Programming & Problem Solving.
CS 1400 Jan 2007 Chapter 4, sections Relational operators Less than< Greater than> Less than or equal= Equal== Not equal!=
Libraries Programs that other people write that help you. #include // enables C++ #include // enables human-readable text #include // enables math functions.
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Functions Modules in C++ are called functions and classes
Programming Introduction to C++.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
1 Object-Oriented Programming Using C++ CLASS 27.
Introduction to C++ Programming Introduction to C++ l C is a programming language developed in the 1970's alongside the UNIX operating system. l C provides.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Week 2 Questions from Last Week Control Structures Functions Lab 1.
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
C++ Tutorial Hany Samuel and Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Copyright © 2006 by Douglas.
Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return.
CSCI-383 Object-Oriented Programming & Design Lecture 5.
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 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 2 Overloaded Operators, Class Templates, and Abstraction Jeffrey S. Childs Clarion University.
C++ Classes and Data Structures Jeffrey S. Childs
Savitch - Chapter 11CS Extending the Class Capabilities Let’s extend the definition of the BankAccount class to illustrate some additional capabilities.
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.
Lecture 12: Functions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
April 11, 2005 More about Functions. 1.Is the following a function call or a function header? calcTotal(); 2.Is the following a function call or a function.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
CSE 332: C++ template examples Today: Using Class and Function Templates Two examples –Function template for printing different types –Class template for.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
1 Reference Variables Chapter 8 Page Reference Variables Safer version of C/C++ pointer. "Refers to" a variable. Like a pointer. Effectively.
Class Method Read class Student { private: string id; string firstName, lastName; float gpa; public: // Will the method change any data members? // Yes!
Chapter 05 (Part II) Control Statements: Part II.
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.
1 Object-Oriented Programming Using C++ CLASS 2 Honors.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
01/05/100 1 Loops/Iteration Used to repeat an action Must have a STOP condition Three flavors - for, while, do/while.
Chapter 6 Looping. 2 l A loop is a repetition control structure. l it causes a single statement or block to be executed repeatedly What is a loop?
Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 Pass-by-Value - default passing mechanism except.
Nested Structures struct TDate { int year, month, day; }; struct StudentType { string id, firstName, lastName; float gpa; TDate DOB; }; struct SectionType.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
1 Data Structures CSCI 132, Spring 2016 Notes_ 5 Stacks.
Introduction to C++ (Extensions to C)
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
CO1401 Programming Design and Implementation
School of EECS, Peking University
Multi-dimensional Array
Mr. Shaikh Amjad R. Asst. Prof in Dept. of Computer Sci. Mrs. K. S
Reserved Words.
CS 1430: Programming in C++.
Student Data Score First Name Last Name ID GPA DOB Phone ...
Introduction to Functions
DATA STRUCTURE : DAT JENIS DATA DAN JENIS DATA ABSTRAK (4JAM)
Chapter 5 Function Basics
Functions.
CS150 Introduction to Computer Science 1
Lab 1 Introduction to C++.
Standard Input/Output Stream
Classes.
Jeff West - Quiz Section 6
CS150 Introduction to Computer Science 1
Programming Introduction to C++.
CS150 Introduction to Computer Science 1
Presentation transcript:

1 C++ Classes and Data Structures Course link…..

2 Classes 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

3 Main Program Using Objects Object A Object B Object C Main Program

4 Main Program Using Objects Object A Object B Object C Main Program

5 Main Program Using Objects Object A Object B Object C Main Program

6 Main Program Using Objects Object A Object B Object C Main Program

7 Main Program Using Objects Object A Object B Object C Main Program

8 Main Program Using Objects Object A Object B Object C Main Program

9 Main Program Using Objects Object A Object B Object C Main Program

10 Main Program Using Objects Object A Object B Object C Main Program

11 Main Program Using Objects Object A Object B Object C Main Program

12 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

13 Main Program and Object Main Program Object public: functions private: data

14 Main Program Calls a Function in the Object Main Program Object public: functions private: data

15 The Function Accesses Data Main Program Object public: functions private: data

16 Function Returns a Value Back to the Main Program Main Program Object public: functions private: data

17 Main Program Calls a Different Function Main Program Object public: functions private: data

18 Function Calls Another Function Main Program Object public: functions private: data

19 Second Function Accesses Data Main Program Object public: functions private: data

20 Second Function Returns Back to First Function Main Program Object public: functions private: data

21 First Function Accesses Data Main Program Object public: functions private: data

22 Function Returns Back to Main Program Main Program Object public: functions private: data

23 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)

24 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.

25 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.

26 15 #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).

27 15 #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.

28 15 #include “checkbook.h” void Checkbook::setBalance( float amount ) 18 { 19balance = amount; 20 } Example of a Class (cont.) Special notation for class function definitions

29 21 bool Checkbook::writeCheck( float amount ) 22 { 23if ( amount > balance ) 24return false; 25balance -= amount; 26lastCheck = amount; 27return true; 28 } Example of a Class (cont.)

30 29 void Checkbook::deposit( float amount ) 30 { 31balance += amount; 32lastDeposit = amount; 33 } Example of a Class (cont.)

31 34 float Checkbook::getBalance( ) 35 { 36return balance; 37 } float Checkbook::getLastCheck( ) 40 { 41return lastCheck; 42 } Example of a Class (cont.) end of checkbook.cpp

32 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

33 16cout << "Enter the initial balance: $"; 17cin >> balance; 18cb.setBalance( balance ); 19 20cout << fixed << showpoint << setprecision( 2 ); A Program that Uses the Checkbook Class (cont.)

34 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

35 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

36 39 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

37 50 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.)