COSC 181 Foundations of Computer Programming. Class 6



Similar documents
Computer Programming C++ Classes and Objects 15 th Lecture

Calling the Function. Two Function Declarations Here is a function declared as pass by value. Why use Pass By Reference?

Appendix K Introduction to Microsoft Visual C++ 6.0

Data Structures using OOP C++ Lecture 1

Ch 7-1. Object-Oriented Programming and Classes

Member Functions of the istream Class

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 3: Input/Output

Chapter 6. Functions and an Introduction to Recursion

Basics of I/O Streams and File I/O

MS Visual C++ Introduction. Quick Introduction. A1 Visual C++

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

Comp151. Definitions & Declarations

Ubuntu. Ubuntu. C++ Overview. Ubuntu. History of C++ Major Features of C++

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

Chapter One Introduction to Programming

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

The C++ Language. Loops. ! Recall that a loop is another of the four basic programming language structures

PIC 10A. Lecture 7: Graphics II and intro to the if statement

13 Classes & Objects with Constructors/Destructors

ATM Case Study OBJECTIVES Pearson Education, Inc. All rights reserved Pearson Education, Inc. All rights reserved.

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Sequential Program Execution

Common Beginner C++ Programming Mistakes

CISC 181 Project 3 Designing Classes for Bank Accounts

6. Control Structures

Class 16: Function Parameters and Polymorphism

Conditions & Boolean Expressions

Note: Syntactically, a ; is needed at the end of a struct definition.

EP241 Computer Programming

Chapter 8 Selection 8-1

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Curriculum Map. Discipline: Computer Science Course: C++

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Appendix M: Introduction to Microsoft Visual C Express Edition

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

1. The First Visual C++ Program

Moving from C++ to VBA

Object Oriented Software Design II

Compiler Construction

Course Syllabus. COSC 1437 Programming Fundamentals II. Revision Date: August 21, 2013

Passing 1D arrays to functions.

Problems and Measures Regarding Waste 1 Management and 3R Era of public health improvement Situation subsequent to the Meiji Restoration

While Loop. 6. Iteration

Moving from CS 61A Scheme to CS 61B Java

7.7 Case Study: Calculating Depreciation

For the next three questions, consider the class declaration: Member function implementations put inline to save space.

Subtopics - Functions Function Declaration Function Arguments Return Statements and values. By Hardeep Singh

ATM Case Study Part 1

Part 1 Foundations of object orientation

5 CLASSES CHAPTER. 5.1 Object-Oriented and Procedural Programming. 5.2 Classes and Objects 5.3 Sample Application: A Clock Class

Constructor, Destructor, Accessibility and Virtual Functions

Visual Studio 2008 Express Editions

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Test #4 November 20, True or False (2 points each)

An Incomplete C++ Primer. University of Wyoming MA 5310

C++ Input/Output: Streams

Sequence Diagrams. Massimo Felici. Massimo Felici Sequence Diagrams c

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

C++ Outline. cout << "Enter two integers: "; int x, y; cin >> x >> y; cout << "The sum is: " << x + y << \n ;

Answers to Review Questions Chapter 7

Lesson Name: Introduction of OOP

Introduction to C++ Introduction to C++ Week 7 Dr Alex Martin 2013 Slide 1

Introduction to Programming (in C++) Loops. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

2.2: Bitwise Logical Operations

Example. Introduction to Programming (in C++) Loops. The while statement. Write the numbers 1 N. Assume the following specification:

Arrays. number: Motivation. Prof. Stewart Weiss. Software Design Lecture Notes Arrays

Object-Oriented Programming in Java

In this Lecture you will Learn: Implementation. Software Implementation Tools. Software Implementation Tools

A technical discussion on modeling with UML 06/11/03 Entity Relationship Modeling with UML

CS 101 Computer Programming and Utilization

Chapter 1: Key Concepts of Programming and Software Engineering

Function Overloading I

Schedule. Structures and Classes in C++ Outline. Goals for This Topic. Another Example of a Structure. What is a Structure? Classes May 12-17, 2005

How to Make a Domain Model. Tutorial

COMPUTER SCIENCE 1999 (Delhi Board)

11 November

Creating a Simple Visual C++ Program

Embedded SQL. Unit 5.1. Dr Gordon Russell, Napier University

Functional Programming in C++11

Copyright 2001, Bill Trudell. Permission is granted to copy for the PLoP 2001 conference. All other rights reserved.

earlier in the semester: The Full adder above adds two bits and the output is at the end. So if we do this eight times, we would have an 8-bit adder.

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

CS 106 Introduction to Computer Science I

Learning Computer Programming using e-learning as a tool. A Thesis. Submitted to the Department of Computer Science and Engineering.

Introduction to Object-Oriented Programming

The While Loop. Objectives. Textbook. WHILE Loops

Developing an ODBC C++ Client with MySQL Database

QUIZ-II QUIZ-II. Chapter 5: Control Structures II (Repetition) Objectives. Objectives (cont d.) 20/11/2015. EEE 117 Computer Programming Fall

Angel Master Course Template

Transcription:

COSC 181 Foundations of Computer Programming Class 6

Defining the GradeBook Class Line 9 17 //GradeBook class definition class GradeBook { public: //function that displays a message void displaymessage() { }; cout << Welcome to the Grade Book! << endl; } Classes have identifiers, just like variables Follow same rules By convention, class identifiers begin with a capital letter. Uses camel case notation FirstSecond Easier to read public: indicates that the affected part of the class (members and functions) are accessible by functions outside the class (i.e. main()) Every class is enclosed in { }, with a ; after the closing bracket

Using the GradeBook class in a program Want to call displaymessage() to display the message on the screen Can t call a member function until you create an instance of the class Lines 22 and 23 in Fig 3.1 GradeBook mygradebook; GradeBook type is defined because we included the GradeBook class in the code mygradebook.displaymessage(); dot operator

UML: Class Diagrams Classes represented as a rectangle with 3 compartments Classes Name Classes Attributes C++ Data Members (more on this later) Classes Operations

Defining Member Function w/ Parameters #include <string> //allows use of string type using std::string using std::getline Fig 3.3 Lines 16-22 public: // function displays a welcome message void displaymessage(string coursename) { cout << Welcome to the grade book for \n << coursename <<! << endl; }

main() function lines 26-40 int main(){ string nameofcourse; GradeBook mygradebook; cout << Please enter the course name: <<endl; getline( cin, nameofcourse ); cout << endl; mygradebook.displaymessage( nameofcourse) return 0; } getline(cin, nameofcourse); Can t use cin >> nameofcourse;

Output and Input Please enter the course name: COSC181 C++ Programming Welcome to the grade book for COSC181 C++ Programming

Function Definition and Calls Note: Function Definition void displaymessage(string coursename) Function Call mygradebook.displaymessage( nameofcourse) Good Programming Practices Use different names for passed arguments and corresponding variables in function definition Use meaningful function and parameter identifiers

UML: Class Diagram (Updated) Remember Class Name Class Attributes Class Methods

Data Members : set and get Functions Fig 3.5 lines 15-40 class GradeBook { public: void setcoursename(string name) { coursename = name; } string getcoursename() { return coursename; } void displaymessage() { cout << Welcome to the grade book for\n << getcoursename() <<! << endl; } private: string coursename; };

New main() function int main(){ string nameofcourse; GradeBook mygradebook; cout << Initial course name is: << mygradebook.getcoursename()<<endl; cout << \nplease enter the course name: << endl; getline( cin, nameofcourse ); mygradebook.setcoursename(nameofcourse); cout << endl; mygradebook.displaymessage(); return 0; }

New: Output and Input Initial course name is: Please enter the course name: COSC181 C++ Programming Welcome to the grade book for COSC181 C++ Programming

Additional Main() Function int main(){ string nameofcourse; GradeBook mygradebook181; GradeBook mygradebook381; cout << \nplease enter the course name of 181: << endl; getline( cin, nameofcourse ); mygradebook181.setcoursename(nameofcourse); cout << \nplease enter the course name of 381: << endl; getline( cin, nameofcourse ); mygradebook381.setcoursename(nameofcourse); cout << endl; mygradebook181.displaymessage(); mygradebook381.displaymessage(); return 0; }

Output What is the output of the previous program? Please enter the course name of 181: COSC181 C++ Programming Please enter the course name of 381: COSC381 C++ Programming Welcome to the grade book for COSC181 C++ Programming Welcome to the grade book for COSC381 C++ Programming

GradeBook Class Fig 3.5 (pg. 92) private: vs. public: fundamental approach for Object-Oriented design important for Software Engineering purposes Creates a layer of abstraction that protects an objects data from outside code Object clients may have bugs that could be destructive If we don t use objects with private members then clients have total access to the values Always localize affects if possible Clients need not know how data is being stored

GradeBook Updated Class Diagram Name Class Attributes - means its private Class Member Functions + means its public () include parameters here : - include type (if any) after here

Try It Type in and test Fig 3.5. Compile and Run When you are done, do not close DevC++

In Class Exercise Modify class GradeBook 1. Include a second string data member that represents the course instructor s name 2. Provide a set function to change the instructor s name 3. Modify function displaymessage to output the welcome message and course name, then the string This course is presented by: followed by the instructor s name. 4. Compile and Run with the modified class. Output: Initial course name is: C++ Instructor's name is: Dr. Ray Welcome to the grade book for C++ This course is presented by: Dr. Ray