Introduction to Programming

Slides:



Advertisements
Similar presentations
Engineering Problem Solving With C++ An Object Based Approach Additional Topics Chapter 10 Programming with Classes.
Advertisements

Operator overloading redefine the operations of operators
This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Operator Overloading. Introduction Operator overloading –Enabling C++’s operators to work with class objects –Using traditional operators with user-defined.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 8 - Operator Overloading Outline 8.1 Introduction 8.2 Fundamentals of Operator Overloading 8.3.
Introduction to Programming Lecture 39. Copy Constructor.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
. Plab – Tirgul 8 I/O streams Example: string class.
計算機概論實習 Files and Streams C++ views file as sequence of bytes Ends with end-of-file marker n-1 end-of-file marker 67 This is.
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
Multiple-Subscripted Array
 Wednesday, 10/16/02, Slide #1 CS106 Introduction to CS1 Wednesday, 10/16/02  QUESTIONS??  Today:  Return and discuss Test #1  Input from and output.
CMSC 2021 Stream I/O Operators and Friend Functions.
CSE202: Lecture 16The Ohio State University1 Two Dimensional Arrays.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 3: Input/Output.
Chapter 3: Input/Output
CS 117 Spring 2002 Review for Exam 3 arrays strings files classes.
Streams, Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output.
CSC241 Object-Oriented Programming (OOP) Lecture No. 10.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 3: Input/Output.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
C ++ Programming Languages Omid Jafarinezhad Lecturer: Omid Jafarinezhad Fall 2013 Lecture 2 Department of Computer Engineering 1.
Lecture 6: Expressions and Interactivity (Part II) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3.
TEXT FILES. CIN / COUT REVIEW  We are able to read data from the same line or multiple lines during successive calls.  Remember that the extraction.
CSE 332: C++ IO We’ve Looked at Basic Input and Output Already How to move data into and out of a program –Using argc and argv to pass command line args.
1 I/O  C++ has no built-in support for input/output input/output is a library (iostream)  C++ program views input and output as a stream of bytes  Input:
Chapter 3: Input/Output
1 Simple File I/O Chapter 11 Switch Statement Chapter 12.
Object Oriented Programming COP3330 / CGS5409.  Multiple Inheritance  Template Classes and Functions.
Introduction to Programming Lecture 40. Class Class is a user defined data type.
C++ REVIEW INPUT/OUTPUT (I/O). BRIEF NOTE “CLASSES” AND “STRUCTS” AND “TYPES” They are ADTs… They define data and operations on that data The ADTs in.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
1 Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
File Processing Files are used for data persistance-permanent retention of large amounts of data. Computer store files on secondary storage devices,such.
GE 211 Programming in C ++ Dr. Ahmed Telba Room :Ac -134
Lecture 14 Arguments, Classes and Files. Arguments.
Arrays Chapter 12. One-Dimensional Arrays If you wanted to read in 1000 ints and print them in reverse order, it would take a program that’s over 3000.
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.
Dale Roberts Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
CS 1430: Programming in C++ 1. File Input in VS Project Properties Debugging Command Arguments quiz8-1.out We want to know how to do it ourselves, right?
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني I/O and File management(cont.) Binary and random files.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 13: Exam 1 Preview.
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
Arrays float Scores[9]; ? index: element // one dimensional array 2.
2008YeungNam Univ. SE Lab. 1 C++ views each file as a sequence of bytes terminated by EOF-marker. Header files Files are opened by creating objects of.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
INPUT & OUTPUT 10/20/2016Department of Computer Science, UOM | Introduction | Fakhre Alam.
Introduction Every program takes some data as input and generate processed data as out put . It is important to know how to provide the input data and.
Introduction to Programming
Introduction to C++ (Extensions to C)
Department of Computer and Information Science, School of Science, IUPUI Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI
CPS120: Introduction to Computer Science
Dynamic Array Multidimensional Array Matric Operation with Array
Programming Fundamental
CS 1430: Programming in C++.
CPSC 231 D.H. C++ File Processing
Input and Output Chapter 3.
File I/O Streams, files, strings 1 1.
Programming with Data Files
Today’s Lecture I/O Streams Tools for File I/O
Chapter 3: Input/Output
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
Introduction to Programming
Operator Overloading.
File I/O.
C++ support for Object-Oriented Programming
Presentation transcript:

Introduction to Programming Lecture 44

Class Matrix class Matrix { private : int numRows , numCols ; double ** elements ; } ;

Class Matrix class Matrix { private : int numRows , numCols ; double ** elements ; public : Matrix ( int = 0 , int = 0 ) ; // Default constructor Matrix ( const Matrix & ) ; // Copy constructor ~ Matrix ( ) ; // Destructor

Class Matrix // Utility functions of Matrix class int getRows ( void ) const ; int getCols ( void ) const ; // Input output functions for Matrix class const Matrix & input ( istream & is = cin ) ; const Matrix & input ( ifstream & is ) ; void output ( ostream & os = cout ) const ; void output ( ofstream & os ) const ;

Class Matrix // Plus Operator Matrix operator + ( Matrix & m ) const ; Matrix operator + ( double d ) const ;

d is a variable of type double ‘A’ is an object of a class Matrix A + d ;

d is a variable of type double ‘a’ is an object of a class Matrix d + A ;

Class Matrix // Plus Operator Matrix operator + ( Matrix & m ) const ; Matrix operator + ( double d ) const ; friend Matrix operator + ( double d , Matrix & m ) ; const Matrix & operator += ( Matrix & m ) ;

i += 3 ; i = i + 3 ; A += B ; // A and B are Matrices

Where A and B are both matrices A – B Where A and B are both matrices

d is a variable of type double ‘A’ is an object of a class Matrix A – d ;

d is a variable of type double ‘a’ is an object of a class Matrix d – A ;

Class Matrix // Minus Operator Matrix operator - ( Matrix & m ) const ; Matrix operator - ( double d ) const ; friend Matrix operator - ( double d , Matrix & m ) ;

A * B ; Where A and B are both matrices

d is a variable of type double ‘A’ is an object of a class Matrix A * d ;

d is a variable of type double ‘a’ is an object of a class Matrix d * A ;

Class Matrix // Multiplication Operator Matrix operator * ( const Matrix & m ) ; Matrix operator * ( double d ) const ; friend Matrix operator * ( const double d , const Matrix & m ) ;

‘A’ is an object of a class Matrix d is a variable of type double A / d ;

Class Matrix // Division Operator Matrix operator / ( const double d ) ;

Example // Where m is a matrix // Stream Insertion and Extraction Operator cin >> m ; // Where m is a matrix

Class Matrix // Stream Insertion and Extraction Operator friend istream & operator >> ( istream & , Matrix & ) ; friend ifstream & operator >> ( ifstream & , Matrix & ) ; friend istream & operator << ( istream & , Matrix & ) ; friend ifstream & operator << ( ifstream & , Matrix & ) ;

Class Matrix const Matrix & operator = ( const Matrix & m ) ; const Matrix & transpose ( void ) ;

Class Matrix Matrix :: Matrix ( int row , int col ) // Default Constructor { numRows = row ; numCols = col ; elements = new ( double * ) [ numRows ] ; for ( int i = 0 ; i < numRows ; i ++ ) elements [ i ] = new double [ numCols ] ; for ( int j = 0 ; j < numCols ; j ++ ) elements [ i ] [ j ] = 0.0 ; }

Matrix A ( B ) ;

Matrix A = B ;

Class Matrix Matrix :: Matrix ( const Matrix & m ) { numRows = m.numRows ; numCols = m.numCols ; elements = new ( double * ) [ numRows ] ; for ( int i = 0 ; i < numRows ; i ++ ) elements [ i ] = new double [ numCols ] ; for ( int j = 0 ; j < numCols ; j ++ ) elements [ i ] [ j ] = m.elements [ i ] [ j ] ; }

Class Matrix Matrix :: ~ Matrix ( void ) { delete [ ] elements ; }

Class Matrix int Matrix :: getRows ( ) const { return numRows ; } int Matrix :: getCols ( ) const return numCols ;

Class Matrix void Matrix :: output ( ostream & os ) const { // Print first row with special characters os.setf ( ios :: showpoint ) ; os.setf ( ios :: fixed , ios :: floatfield ) ; os << ( char ) 218 ; for ( int j = 0 ; j < numCols ; j ++ ) os << setw ( 10 ) << " “ ; os << ( char ) 191 << "\n" ;

Class Matrix // Print remaining rows with vertical bars only for ( int i = 0 ; i < numRows ; i ++ ) { os << ( char ) 179 ; for ( int j = 0 ; j < numCols ; j ++ ) os << setw ( 10 ) << setprecision ( 2 ) << elements [ i ] [ j ] ; os << ( char ) 179 << "\n" ; }

Class Matrix // Print last row with special characters os << ( char ) 192 ; for ( int j = 0 ; j < numCols ; j ++ ) os << setw ( 10 ) << " " ; os << ( char ) 217 << "\n" ; }

Class Matrix void Matrix :: output ( ofstream & os ) const { os.setf ( ios :: showpoint ) ; os.setf ( ios :: fixed , ios :: floatfield ) ; os << numRows << " " << numCols << "\n" ; for ( int i = 0 ; i < numRows ; i ++ ) for ( int j = 0 ; j < numCols ; j ++ ) os << setw ( 6 ) << setprecision ( 2 ) << elements [ i ] [ j ] ; os << "\n" ; }

Class Matrix const Matrix & Matrix :: input ( istream & is ) { cout << "Input Matrix size: " << numRows << " rows by " << numCols << " columns \n" ; for ( int i = 0 ; i < numRows ; i ++ ) cout << "Please enter " << numCols << " values separated by spaces for row no." << i+1 << ": " ; for ( int j = 0 ; j < numCols ; j ++ ) cin >> elements [ i ] [ j ] ; } return * this ;

Class Matrix const Matrix & Matrix :: input ( ifstream & is ) { int Rows , Cols ; is >> Rows ; is >> Cols ; if ( Rows > 0 && Cols > 0 ) Matrix temp ( Rows , Cols ) ; * this = temp ; for ( int i = 0 ; i < numRows ; i ++ ) for ( int j = 0 ; j < numCols ; j ++ ) is >> elements [ i ] [ j ] ; } return * this ;

Class Matrix const Matrix & Matrix :: transpose ( ) { if ( numRows == numCols ) // Square Matrix double temp ; for ( int i = 0 ; i < numRows ; i ++ ) for ( int j = i + 1 ; j < numCols ; j ++ ) temp = elements [ i ] [ j ]; elements [ i ] [ j ] = elements [ j ] [ i ] ; elements [ j ] [ i ] = temp ; }

Class Matrix else { Matrix temp(numCols, numRows); for ( int i = 0 ; i < numRows ; i ++ ) for ( int j = 0 ; j < numCols ; j ++ ) temp.elements [ j ] [ i ] = elements [ i ] [ j ] ; } * this = temp ; return * this ;