More on Classes. COMP104 ADT / Slide 2 Abstract Data Type * An Abstract Data Type is a class with some special restrictions. * These restrictions can.

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

Abstract Data Type Fraction Example
Operator Overloading Back to Fractions.... Implementing an Object We’ve talked at length about object- orientation. – We’ve looked heavily at encapsulation.
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.
C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric.
If Statements & Relational Operators Programming.
True or false A variable of type char can hold the value 301. ( F )
Classes. COMP104 Lecture 25 / Slide 2 Motivation  Types such as int, double, and char are simple objects. * They can only answer one question: “What.
Classes. COMP104 Class / Slide 2 Motivation  Types such as int, double, and char are “stupid” objects. * They can only answer one question: “What value.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ (4) 10 September.
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)
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
Abstract Data Type (ADT). 2 An Abstract Data Type (ADT) is a data structure with a set of operations –Operations specify how the ADT behaves, but does.
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
Chapter 6—Objects and Classes The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Objects and Classes C H A P T E R 6 To beautify.
Object Oriented Programming in C++ Chapter5 Operator Overloading.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 14. User Defined Classes.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R1. ADTs as Classes.
M. Taimoor Khan #include void main() { //This is my first C++ Program /* This program will display a string message on.
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.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
CHAPTER 13 CLASSES AND DATA ABSTRACTION. In this chapter, you will:  Learn about classes  Learn about private, protected, and public members of a class.
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Part 9:
CSC241 Object-Oriented Programming (OOP) Lecture No. 8.
Class Miscellanea Details About Classes. Review We’ve seen that a class has two sections: class Temperature { public: //... public members private: //...
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Lecture 7: Making Decisions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
1 Lecture 17 Operator Overloading. 2 Introduction A number of predefined operators can be applied to the built- in standard types. These operators can.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
1 2/21/05CS250 Introduction to Computer Science II Destructors, Get and Set, and Default Memberwise Assignment.
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
Class Example - Rationals Rational numbers are represented by the ratio of two integers, a numerator and a denominator, e.g., 2/3. This is opposed to irrational.
Fundamental Programming Fundamental Programming More Expressions and Data Types.
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.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
Constructors A constructor is a method that has the same name as the class itself It is automatically called when an object is instantiated (in other words,
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
Andy Wang Object Oriented Programming in C++ COP 3330
The Ohio State University
CSCE 210 Data Structures and Algorithms
Struct Properties The C struct mechanism is vaguely similar to the Java/C++ class mechanisms: - supports the creation of user-defined data types - struct.
Chapter 7: Expressions and Assignment Statements
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Chapter 7: Expressions and Assignment Statements
Introduction to C++ October 2, 2017.
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
Struct Properties The C struct mechanism is vaguely similar to the Java/C++ class mechanisms: - supports the creation of user-defined data types - struct.
Operators and Expressions
نوع داده هاي انتزاعي Abstract Data Types
Fraction Abstract Data Type
Advanced Program Design with C++
Summary Two basic concepts: variables and assignments Basic types:
If Statements.
COMS 261 Computer Science I
Engineering Problem Solving with C++ An Object Based Approach
Development and Implementation
CS 144 Advanced C++ Programming February 21 Class Meeting
Class rational part2.
Struct Properties The C struct mechanism is vaguely similar to the Java/C++ class mechanisms: - supports the creation of user-defined data types - struct.
Presentation transcript:

More on Classes

COMP104 ADT / Slide 2 Abstract Data Type * An Abstract Data Type is a class with some special restrictions. * These restrictions can make programming easier. * One of these restrictions is called information hiding, and it helps avoid common problems (e.g., a denominator of zero should not be allowed in rational numbers). * In information hiding, the user should not be allowed to access the data members directly (they should be private). * An Abstract Data Type is used in Object-Oriented Programming (COMP151).

COMP104 ADT / Slide 3 n Multiplication n Division Rational Review * Rational number Ratio of two integers: a/b  Numerator over the denominator * Standard operations n Addition n Subtraction

COMP104 ADT / Slide 4 Rational Representation  Represent a numerator and denominator with two int data members Numerator and Denominator n Data members private (information hiding) * Public arithmetic member functions n Rational addition, subtraction, multiplication, division * Public relational member functions n Equality and less than comparisons

COMP104 ADT / Slide 5 Rational Representation * Member functions n Constructors  Default-value constructor Rational r;  Explicit-value constructor Rational r(3, 4);  Copy constructor (provided automatically: simply copies data members) Rational r(t);Rational r = t; n Assignment (provided automatically: simply copies data members) r = t; n Inputting and displaying object

a Values: Numerator = 1 Denominator = 2 b Values Numerator = 2 Denominator = 3 Class Rational Public interface: Add(), Subtract(), Multiply(),Divide(), Equal(), LessThan(), Display(), Get() Data members: Numerator, Denominator Rational a(1,2); Rational b(2,3);

COMP104 ADT / Slide 7 void main(){ Rational r; Rational s; cout << "Enter two rationals(a/b): "; r.Get(); s.Get(); Rational t(r); Rational sum = r.Add(s); r.Display(); cout << " + "; s.Display(); cout << " = "; sum.Display();cout << endl; Rational product = r.Multiply(s); r.Display(); cout << " * "; s.Display(); cout << " = "; product.Display();cout << endl; } main()

COMP104 ADT / Slide 8 Rational Overview class Rational { public: // for Rational member functions // for everybody (like "global" variables) private: // for Rational data members // like "local" variables } ;

COMP104 ADT / Slide 9 Rational Class class Rational{ public: // default-value constructor Rational(); // explicit-value constructor Rational(int numer, int denom = 1); // arithmetic functions Rational Add(const Rational r) const; Rational Subtract(const Rational r) const; Rational Multiply(const Rational r) const; Rational Divide(const Rational r) const; // relational functions bool Equal(const Rational r) const; bool LessThan(const Rational r) const; // i/o functions void Display() const; void Get(); private:// data members int Numerator; int Denominator; };

COMP104 ADT / Slide 10 const  You can use const on user-defined types as usual: const Rational OneHalf(1,2); OneHalf.Display(); // no problem OneHalf.Get(); // illegal: OneHalf is a const

COMP104 ADT / Slide 11 Default-Value Constructor // default-value constructor Rational::Rational(){ Numerator = 0; Denominator = 1; } * Example Rational r; // r = 0/1

COMP104 ADT / Slide 12 Explicit-Value Constructor // explicit-value constructor Rational::Rational(int numer, int denom){ Numerator = numer; Denominator = denom; if( Denominator == 0){ cout << "Illegal denominator of zero, " << "using 1 instead" << endl; Denominator = 1; } * Example Rational t(2,3); // t = 2/3

COMP104 ADT / Slide 13 Arithmetic Functions Rational Rational::Add(const Rational r) const{ int a = Numerator; int b = Denominator; int c = r.Numerator; int d = r.Denominator; Rational result(a*d + b*c, b*d); return result; } * Example Rational t(1,2), u(3, 4); Rational v = t.Add(u);

COMP104 ADT / Slide 14 Arithmetic Functions Rational Rational::Multiply(const Rational r) const{ int a = Numerator; int b = Denominator; int c = r.Numerator; int d = r.Denominator; Rational result(a*c, b*d); return result; } * Example Rational t(1,2), u(3, 4); Rational v = t.Multiply(u);

COMP104 ADT / Slide 15 Arithmetic Functions Rational Rational::Subtract(const Rational r) const{ int a = Numerator; int b = Denominator; int c = r.Numerator; int d = r.Denominator; Rational result(a*d - b*c, b*d); return result; } * Example Rational t(1,2), u(3, 4); Rational v = t. Subtract (u);

COMP104 ADT / Slide 16 Arithmetic Functions Rational Rational::Divide(const Rational r) const{ int a = Numerator; int b = Denominator; int c = r.Numerator; if( c == 0){ cout << "Illegal denominator of zero, " << "using 1 instead" << endl; c = 1; } int d = r.Denominator; Rational result(a*d, b*c); return result; } * Example Rational t(1,2), u(3, 4); Rational v = t. Divide (u);

COMP104 ADT / Slide 17 Relational Functions bool Rational::Equal(const Rational r) const{ double a, b; a = double(Numerator)/Denominator; b = double(r.Numerator)/r.Denominator; if(a == b) return true; else return false; } * Example if(s.Equal(t)) cout << "They are the same!";

COMP104 ADT / Slide 18 Relational Functions bool Rational::LessThan(const Rational r) const{ double a, b; a = double(Numerator)/Denominator; b = double(r.Numerator)/r.Denominator; if(a < b) return true; else return false; } * Example if(s.LessThan(t)) cout << "The first is less than the second!";

COMP104 ADT / Slide 19 I/O Functions void Rational::Display() const{ cout << Numerator << '/' << Denominator; } * Example t.Display();

COMP104 ADT / Slide 20 I/O Functions void Rational::Get(){ char slash; cin >> Numerator >> slash >> Denominator; if( Denominator == 0){ cout << "Illegal denominator of zero, " << "using 1 instead" << endl; Denominator = 1; } * Example t.Get();