Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 25P. 1Winter Quarter C++: I/O and Classes Lecture 25.

Slides:



Advertisements
Similar presentations
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions.
Advertisements

CS 6301 Lecture 2: First Program1. CS Topics of this lecture Introduce first program  Explore inputs and outputs of a program Arithmetic using.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 13P. 1Winter Quarter Scope of Variables.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 27P. 1Winter Quarter I/O Manipulation Lecture.
By: Mr. Baha Hanene Chapter 3. Learning Outcomes We will cover the learning outcome 02 in this chapter i.e. Use basic data-types and input / output in.
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 3: Requirements Specification, C++ Basics.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 24P. 1Winter Quarter C++ Lecture 24.
Week 1 Algorithmization and Programming Languages.
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1 st semester H 1 King Saud University College of Applied studies and Community Service Csc 1101 By:
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
Lecture 3: Getting Started & Input / Output (I/O) “TRON” Copyright 1982 (Walt Disney Productions)
CPSC 252 Operator Overloading and Convert Constructors Page 1 Operator overloading We would like to assign an element to a vector or retrieve an element.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Chapter 2 part #1 C++ Program Structure
1 Simple Input/Output  C++ offers the iostream library, which defines a system of character-oriented Input/Output (I/O) using object oriented programming.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 What is a Named Constant? A named constant is a location in memory that we can refer to by an identifier, and in which a data value that cannot be changed.
File Processing Files are used for data persistance-permanent retention of large amounts of data. Computer store files on secondary storage devices,such.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
File I/O in C++. Using Input/Output Files A computer file  is stored on a secondary storage device (e.g., disk);  is permanent;  can be used to provide.
File I/O in C++ I. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; can be used to provide.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
Object-Oriented Programming (OOP) and 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.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
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.
Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
Lecture 3: Getting Started & Input / Output (I/O)
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 1.2 Introduction to C++ Programming
Review 1.
Introduction to C++ (Extensions to C)
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Topic Pre-processor cout To output a message.
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE
Chapter 1: Introduction to computers and C++ Programming
Chapter 1.2 Introduction to C++ Programming
Introduction to C++ Systems Programming.
Chapter 2 part #1 C++ Program Structure
Concepts and Basics of C++ Programming
Concepts and Basics of C++ Programming
(5 - 1) Object-Oriented Programming (OOP) and C++
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Introduction to C++ Programming
(5 - 1) Object-Oriented Programming (OOP) and C++
C++ Programming Lecture 3 C++ Basics – Part I
Programs written in C and C++ can run on many different computers
Lecture 2 Fall 2011 September 13-15, 2011 Ghufran Ahmed
Capitolo 1 – Introduction C++ Programming
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Chapter 1 c++ structure C++ Input / Output
Classes and Objects Systems Programming.
Chapter 2 part #1 C++ Program Structure
Presentation transcript:

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 25P. 1Winter Quarter C++: I/O and Classes Lecture 25

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 25P. 2Winter Quarter Keyboard & Screen I/O in C++ It is perfectly valid to use the same I/O statements in C++ as in C -- The very same printf, scanf, and other stdio.h functions that have been used until now. However, C++ provides an alternative with the new stream input/output features. The header file is named iostream and the stream I/O capabilities are accessible when you use the pre-processor declaration: #include // No “.h” on std headers using namespace std;// To avoid things like // std::cout and std::cin

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 25P. 3Winter Quarter Keyboard & Screen I/O in C++ Several new I/O objects available when you include the iostream header file. Two important ones are: –cin// Used for keyboard input (std::cin) –cout// Used for screen output (std::cout) Both cin and cout can be combined with other member functions for a wide variety of special I/O capabilities in program applications.

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 25P. 4Winter Quarter Keyboard & Screen I/O in C++ Since cin and cout are C++ objects, they are somewhat "intelligent": –They do not require the usual format strings and conversion specifications. –They do automatically know what data types are involved. –They do not need the address operator, &. –They do require the use of the stream extraction ( >> ) and insertion ( << ) operators. The next slide shows an example of the use of cin and cout.

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 25P. 5Winter Quarter Example using cin and cout #include using namespace std;// replace every cin and cout // with std::cin and std::cout // without this line int main ( ) { int a, b; float k; char name[30]; cout << "Enter your name\n" ; cin >> name ; cout << "Enter two integers and a float\n" ; cin >> a >> b >> k ; cout << "Thank you, " << name << ", you entered\n " ; cout << a << ", " << b << ", and " << k << '\n' ; }

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 25P. 6Winter Quarter Example Program Output Enter your name Rick Enter two integers and a float Thank you, Rick, you entered 20, 30, and 45.67

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 25P. 7Winter Quarter Input Stream Object Member Functions cin.getline (array_name, max_size) ; Example: char name[40] ; cin.getline (name, 40); // gets a string from // keyboard and assigns // to name

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 25P. 8Winter Quarter Classes in C++ Classes enable a C++ program to model objects that have: –attributes (represented by data members). –behaviors or operations (represented by member functions). Types containing data members and member function prototypes are normally defined in a C++ program by using the keyword class.

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 25P. 9Winter Quarter Classes in C++ A class definition begins with the keyword class. The body of the class is contained within a set of braces, { } ; (notice the semi-colon). Within the body, the keywords private: and public: specify the access level of the members of the class. Classes default to private. Usually, the data members of a class are declared in the private: section of the class and the member functions are in public: section. Private members of the class are normally not accessible outside the class, i.e., the information is hidden from "clients" outside the class.

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 25P. 10Winter Quarter Classes in C++ A member function prototype which has the very same name as the name of the class may be specified and is called the constructor function. The definition of each member function is "tied" back to the class by using the binary scope resolution operator ( :: ). The operators used to access class members are identical to the operators used to access structure members, e.g., the dot operator (.).

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 25P. 11Winter Quarter Classes Example #include #include // This is the same as string.h in C using namespace std; class Numbers// Class definition { public:// Can be accessed by a "client". Numbers ( ) ; // Class "constructor" void display ( ) ; void update ( ) ; private:// Cannot be accessed by "client" char name[30] ; int a ; float b ; } ;

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 25P. 12Winter Quarter Classes Example (continued) Numbers::Numbers ( ) // Constructor member function { strcpy (name, "Unknown") ; a = 0; b = 0.0; } void Numbers::display ( ) // Member function { cout << "\nThe name is " << name << "\n" ; cout << "The numbers are " << a << " and " << b << endl ; }

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 25P. 13Winter Quarter Classes Example (continued) void Numbers::update ( )// Member function { cout << "Enter name" << endl ; cin.getline (name, 30) ; cout << "Enter a and b" << endl ; cin >> a >> b; }

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 25P. 14Winter Quarter Classes Example (continued) int main ( )// Main program { Numbers no1, no2 ; // Create two objects of // the class "Numbers" no1.update ( ) ;// Update the values of // the data members no1.display ( ) ;// Display the current no2.display ( ) ;// values of the objects }

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 25P. 15Winter Quarter Example Program Output r1tel (~) freuler 53> example.out The name is Rick Freuler The numbers are 9876 and The name is Unknown The numbers are 0 and 0

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 25P. 16Winter Quarter More Detailed Classes Example #include using namespace std; class Numbers // Class definition { public: Numbers (char [ ] = "Unknown", int = 0, float = 0.0) ; void display ( ) ; void update ( ) ; private: char name[30]; int a; float b; } ;

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 25P. 17Winter Quarter More Detailed Classes Example (continued) Numbers::Numbers (char nm[ ], int j, float k ) { strcpy (name, nm) ; a = j ; b = k ; } void Numbers::update ( ) { cout << "Enter a and b" << endl ; cin >> a >> b ; }

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 25P. 18Winter Quarter More Detailed Classes Example (continued) void Numbers::display( ) { cout << "\nThe name is " << name << '\n' ; cout << "The numbers are " << a << " and " << b << endl ; } int main ( ) { Numbers no1, no2 ("John Demel", 12345, 678.9); no1.display ( ) ; no2.display ( ) ; }

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 25P. 19Winter Quarter More Detailed Example Program Output r1rhl (~) freuler 76> example.out The name is Unknown The numbers are 0 and 0 The name is John Demel The numbers are and 678.9

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 25P. 20Winter Quarter Some Help for G23 Use #include using namespace std; When using these, use the following compile command g++ –o g23.out g23.cpp

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 25P. 21Winter Quarter Some Guidance on G23 class Complex { private: float fReal; float fImag; public: Complex(); // Constructor function prototype void mult(_______); // rest of function prototypes }; What goes here?

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 25P. 22Winter Quarter Hints on Multiplying Complex Numbers For the function void Complex::mult (Complex imag_n) { // you need to use the distributive property! // (a + b*i)*(c + d*i) = (a*c – b*d) + (b*c + a*d)*i // real part + i imag part }