COMS 261 Computer Science I

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick Strings #include using namespace std; int main () { string word; cout
Advertisements

Engineering Problem Solving With C++ An Object Based Approach Additional Topics Chapter 10 Programming with Classes.
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
Abstract Data Type Fraction Example
More on Classes. COMP104 ADT / Slide 2 Abstract Data Type * An Abstract Data Type is a class with some special restrictions. * These restrictions can.
Operator Overloading Programming in C++ Fall 2008 Dr. David A. Gaitros
Class and Objects.
Abstract Data Types Development and Implementation JPC and JWD © 2002 McGraw-Hill, Inc.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Abstract Data Types Development and Implementation.
CS-240 Operator Overloading Dick Steflik. Operator Overloading What is it? –assigning a new meaning to a specific operator when used in the context of.
C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Templates and Polymorphism Generic functions and classes.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Pointers and Dynamic Objects Mechanisms for developing flexible list representations.
Abstract Data Type. COMP104 Slide 2 Summary l A class can be used not only to combine data but also to combine data and functions into a single (compound)
CMSC 2021 Stream I/O Operators and Friend Functions.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 14. User Defined Classes.
1 COMS 261 Computer Science I Title: Classes Date: November 7, 2005 Lecture Number: 28.
Templates and Polymorphism Generic functions and classes
CMSC 202 Lesson 12 Operator Overloading II. Warmup  Overload the + operator to add a Passenger to a Car: class Car { public: // some methods private:
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R1. ADTs as Classes.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Case Study - Fractions Timothy Budd Oregon State University.
CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO CH1, C++ INTERLUDE 1.
Pointers and Dynamic Objects Mechanisms for developing flexible list representations JPC and JWD © 2002 McGraw-Hill, Inc.
1 COMS 261 Computer Science I Title: String Class Date: October 3, 2005 Lecture Number: 14.
J. P. Cohoon and J. W. Davidson © 1997 McGraw-Hill, Inc. Templates Generic functions and classes.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Part 9:
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
1 Lecture 17 Operator Overloading. 2 Introduction A number of predefined operators can be applied to the built- in standard types. These operators can.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO , APP D, C++ BOOK.
Programming Abstract Data Types. COMP104 Lecture 30 / Slide 2 Review: class l Name one reason why we need the class construct. l Why do we need "inspector.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
1 COMS 261 Computer Science I Title: Classes Date: November 9, 2005 Lecture Number: 29.
1 COMS 261 Computer Science I Title: Classes Date: November 4, 2005 Lecture Number: 27.
Chapter 11 Mechanisms for developing flexible list representations Pointers and dynamic memory.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 15, 2004 Lecture Number: 11.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
CSE1002 – Problem Solving with Object Oriented Programming
Reference Parameters There are two ways to pass arguments to functions: pass- by-value and pass-by-reference. pass-by-value A copy of the argument’s value.
CSCE 210 Data Structures and Algorithms
Strings: C-strings vs. Strings as Objects
Operator Overloading Part 2
Operator Overloading CMSC 202.
Andy Wang Object Oriented Programming in C++ COP 3330
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
CISC181 Introduction to Computer Science Dr
Chapter 15: Overloading and Templates
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
Type Conversions Lecture 13 Wed, Feb 21, 2007.
Dynamic Memory Allocation Reference Variables
Andy Wang Object Oriented Programming in C++ COP 3330
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Fraction Abstract Data Type
Strings: C-strings vs. Strings as Objects
Advanced Program Design with C++
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
COMS 261 Computer Science I
CS148 Introduction to Programming II
COP 3330 Object-oriented Programming in C++
COMS 261 Computer Science I
Development and Implementation
COMS 261 Computer Science I
COMS 261 Computer Science I
Class rational part2.
C++ support for Object-Oriented Programming
Presentation transcript:

COMS 261 Computer Science I Title: Classes Date: November 5, 2004 Lecture Number: 30

Announcements

Review Classes

Outline Classes Abstract Data Type Rational

Rational Implementation #include <iostream> // Start of rational.cpp #include <string> using namespace std; #include "rational.h" // default constructor Rational::Rational() { numerator = 0; denominator = 1; } Example Rational r; // r = 0/1 Is this necessary? Which objects are being referenced? Show Me

Inspectors Where are the following legal in main()? int Rational::getNumerator() const { return numerator; } int Rational::getDenominator() const { return denominator; Where are the following legal in main()? int a = getNumerator(); int b = t.getNumerator(); Which object is being referenced? Why the const? Show Me

Numerator Mutator Where are the following legal in main()? void Rational::setNumerator(int numer) { numerator = numer; } Where are the following legal in main()? setNumerator(1); t.setNumerator(2); Why no const?

Default Constructor Example t.setDenominator(5); void Rational::setDenominator(int denom) { if (denom != 0) { denominator = denom; } else { cerr << "Illegal denominator: " << denom << "using 1" << endl; denominator = 1; } Example t.setDenominator(5);

Denominator Mutator void Rational() { setNumerator(1); setDenominator(1); }

Initial Value Constructor // (numer, denom) constructor Rational::Rational(int numer, int denom) { setNumerator(numer); setDenominator(denom); } Example Rational t(2,3); // t = 2/3 Rational u(2); // u = 2/1 (why?) Show Me

Addition Facilitator Example Show Me Rational Rational::add(const Rational &r) const { int a = GetNumerator(); int b = GetDenominator(); int c = r.GetNumerator(); int d = r.GetDenominator(); return Rational(a*d + b*c, b*d); } Example t.add(u); Show Me

Multiplication Facilitator Rational Rational::multiply(const Rational &r) const { int a = getNumerator(); int b = getDenominator(); int c = r.getNumerator(); int d = r.getDenominator(); return Rational(a*c, b*d); } Example t.multiply(u);

Insertion Facilitator void Rational::insert(ostream &sout) const { sout << getNumerator() << '/' << getDenominator(); } Example t.insert(cout); Why is sout a reference parameter? Show Me

Basic Extraction Facilitator void Rational::extract(istream &sin) { int numer; int denom; char slash; sin >> numer >> slash >> denom; assert(slash == '/'); SetNumerator(numer); SetDenominator(denom); return; } Example t.extract(cin); Show Me

Auxiliary Arithmetic Operators Rational operator+( const Rational &r, const Rational &s) { return r.add(s); } Rational operator*( return r.multiply(s); Example cout << (t + t) * t; Show Me

Auxiliary Insertion Operator ostream& operator<<( ostream &sout, const Rational &r) { r.insert(sout); return sout; } Why a reference return? t.insert(cout); cout << endl; // unnatural cout << t << endl; // natural

Auxiliary Extraction Operator // extracting a Rational istream& operator>>(istream &sin, Rational &r) { r.extract(sin); return sin; } Why a reference return? t.extract(cin); // unnatural cin >> t; // natural

What’s Happening Here? Suppose the following definitions are in effect Rational a(2,3); Rational b(3,4); Rational c(1,2); Why do the following statements work Rational s(a); Rational t = b; c = a C++ automatically provides a copy constructor and an assignment operator

Copy Construction Default copy construction Copy of one object to another in a bit-wise manner The representation of the source is copied to the target in a bit-by-bit manner This type of copy is called shallow copying Class developers are free to implement their own copy constructor Rational technically does need a one, but we will write one anyway. Always!!

A Rational Copy Constructor Rational::Rational(const Rational &r) { int a = r.getNumerator(); int b = r.getDenomiator(); setNumerator(a); setDenominator(b); } Rational s(a); Rational t = b;

Gang Of Three Also define Gang of three refers to the Assignment operator Copy source to target and return target A = B = C Destructor Clean up the object when it goes out of scope Gang of three refers to the Copy constructor, assignment operator, and the destructor

A Rational Assignment Operator Rational& Rational::operator =(const Rational &r) { int a = r.getNumerator(); int b = r.getDenomiator(); setNumerator(a); setDenominator(b); return *this; } a = b; a = b = c; *this is C++ syntax for the object whose member function was invoked