Homework #4: Operator Overloading and Strings By J. H. Wang May 8, 2012.

Slides:



Advertisements
Similar presentations
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Advertisements

CS110 Programming Language I
Copyright © 2003 Pearson Education, Inc. Slide 1.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 1 Overview 8.1 An Array Type for Strings 8.2 The Standard string.
Homework #2: Functions and Arrays
True or false A variable of type char can hold the value 301. ( F )
Wednesday, 10/23/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/23/02  QUESTIONS??  Today:  Discussion of HW #3  The const modifier for functions.
1 9/15/06CS150 Introduction to Computer Science 1 Combined Assignments, Relational Operators, and the If Statement.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8 Strings and Vectors.
1 Lecture 5: Input/Output (I) Introduction to Computer Science Spring 2006.
Lecture 3 August 31 Chapter 3. Chapter 3 – numbers, string, booleans integer: MATLAB stores numeric data as double-precision floating point (double) by.
Definition Class In C++, the class is the construct primarily used to create objects.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 12 More.
Administrative MUST GO TO CORRECT LAB SECTION! Homework due 11:59pm on Tuesday. 25 points off if late (up to 24 hours) Cannot submit after 11:59pm on Wednesday.
Table of Contents Logarithm Properties - Simplifying Using Combinations The three basic properties for logarithms are... It is important to be able to.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 4.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Chapter 1 Working with strings. Objectives Understand simple programs using character strings and the string library. Get acquainted with declarations,
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
Homework #3: Classes and Constructors
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
10.4 Addition and Subtraction: Like Denominators Goal: to add and subtract rational expressions with like denominators.
Homework #5: Pointers, Dynamic Arrays and Inheritance
Lecture #6 OPERATORS AND ITS TYPES By Shahid Naseem (Lecturer)
1 Programming for Engineers in Python Autumn Lecture 6: More Object Oriented Programming.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 8 Strings and Vectors.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8 Strings and Vectors.
Homework #1: C++ Basics, Flow of Control, and Function Basics By J. H. Wang Mar. 13, 2012.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
Operator Overloading Week 5.
Homework #4: Operator Overloading and Strings By J. H. Wang Apr. 17, 2009.
Homework #2: Functions and Arrays By J. H. Wang Mar. 24, 2014.
Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,
CS Class 19 Today  Practice with classes Announcements  Turn in algorithm for Project 5 in class today  Project 5 due 11/11 by midnight – .
Homework #3: Classes and Constructors By J. H. Wang Apr. 14, 2014.
Homework #4: Operator Overloading and Strings By J. H. Wang May 12, 2014.
Chapter 7 Continued Arrays & Strings. Strings as Class Members Strings frequently appear as members of classes. The next example, a variation of the objpart.
Homework #5: Pointers, Dynamic Arrays and Inheritance By J. H. Wang Jun. 5, 2009.
An Array Type For Strings. Two ways to represent strings – i.e. “Hello” cstring An array with base type char Older way of processing strings Null character.
More about strings in C++. String member functions The next three slides present information about functions that are members of the C++ string class.
Homework #5: Pointers, Dynamic Arrays and Inheritance By J. H. Wang May 31, 2011.
Homework #2 J. H. Wang Mar. 29, Homework #2 Chap.3 –3.5 (a) –3.8 (b) –3.16 –3.23 (a)
10.4 Addition and Subtraction: Like Denominators.
Chapter 8 Strings and Vectors. Slide 8- 2 Overview 8.1 An Array Type for Strings 8.2 The Standard string Class 8.3 Vectors.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Homework #3: Classes and Constructors
Homework #3: Classes and Constructors By J. H. Wang Apr. 24, 2015.
Strings, and the string Class. C-Strings C-string: sequence of characters stored in adjacent memory locations and terminated by NULL character The C-string.
Exception Handling How to handle the runtime errors.
Homework #4: Operator Overloading and Strings By J. H. Wang May 22, 2015.
Slide 1 Chapter 9 Strings. Slide 2 Learning Objectives  An Array Type for Strings  C-Strings  Character Manipulation Tools  Character I/O  get, put.
C++ Memory Management – Homework Exercises
Adding and Subtracting Rational Expressions
Chapter 8 Strings and Vectors 1
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Structures Cont. Dr. Xiao Qin Auburn.
Objective The student will be able to:
group work #hifiTeam
Adding AND Subtracting Rational Expressions
Class Examples.
Section 5.4 Study Materials
Find the product or quotient.
Programming Assignment #5
If Statements.
Algorithms computer as the tool process – algorithm
Summary of what we learned yesterday
Using string type variables
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Video: The Sound of Raining
Presentation transcript:

Homework #4: Operator Overloading and Strings By J. H. Wang May 8, 2012

Part I: Hand-Written Exercises 1.What is the difference between an operator and a function? 2.What can’t we overload >> or << as member operators?

3. Consider the following code (and assume that it is embedded in a complete and correct program and then run): (a) string s1, s2; cout > s1 >> s2; cout << s1 << “*” << s2 << “<End of Output”; If the dialogue begins as follows, what will be the next line of output? Enter a line of input: Programming languages are a medium of expression.

(b) Consider the following code: string s; cout << “Enter a line of input:\n”; getline(cin, s); cout << s << “<End of Output”; If the dialogue begins as follows, what will be the next line of output? Enter a line of input: Programming languages are a medium of expression.

Part II: Programming Exercises 4. Define a class Rational for rational numbers. A rational number is a number that can be represented as the quotient of two integers, for example, 1/2, 3/4, 64/2, and so forth. Represent rational numbers as two values of type int, one for the numerator and one for the denominator. Include the following functions in your code: [… to be continued …]

[… continued from previous slide …] (1) a constructor with two arguments that can be used to set the member variables of an object to any legitimate values. (2) a constructor with only a single parameter of type int ; call this single parameter wholeNumber and define the constructor so that the object will be initialized to the rational number wholeNumber/1. (3) a default constructor that initializes an object to 0 (that is, to 0/1). [… to be continued …]

[… continued from previous slide …] (4) overloaded input and output operators >> and <<. Numbers are to be input and output in the form 1/2, 15/32, 300/401, and so forth. (Note that the numerator, the denominator, or both may contain a minus sign, so -1/2, 15/-32, and -300/- 401 are also possible inputs.) (5) overloaded operators: ==,, >=, +, -, *, and /. [… to be continued …]

[… continued from previous slide …] (6) a function to normalize the values stored so that, after normalization, the denominator is positive and the numerator and denominator are as small as possible. (For example, after normalization 4/-8 would be represented the same as -1/2.) Write a test C++ program to test your class. [ Hint: Two rational numbers a/b and c/d are equal if a*d equals c*b. If b and d are positive rational numbers, a/b is less than c/d provided a*d is less than c*b.]

5. Write a function to compare two C-strings for equality. The function should return true if the strings are equal and false if they are not. Your function should ignore case, punctuation, and whitespace characters. Test your function with a variety of input strings. [Note: Do not use the standard library function strcmp(). ]

Homework Submission Due: 2 weeks (May 22, 2012) Hand-written exercises –Please write your names and answers on papers Programs –Please submit to homework submission Web site:

Any Questions?