ADT Specification Example

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
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Introduction to Programming Lecture 39. Copy Constructor.
Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
CMSC 202, Version 2/02 1 Operator Overloading Strong Suggestion: Go over the Array class example in Section 8.8 of your text. (You may ignore the Array.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Abstract Data Type Fraction Example
Operator Overloading Fundamentals
C++ Classes & Data Abstraction
Operator Overloading (2) Operator function can be implemented as class member function Then the left most (or only) operand must be an class object( or.
Important Definitions Class: A structured data type that is used to represent an abstract data type (ADT) Class member: A components of a class. It can.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Operator Overloading Enable C++’s operators to work with class object >, +, -, *, / This operators perform differently depending on their context in integer,
Wednesday, 10/9/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/9/02  QUESTIONS ??  Today:  Discuss HW #02  Discuss test question types  Review 
1 Lecture 29 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
1 ADT Specification Example TYPE Student DOMAIN Each student can be represented by first name, last name, id and credit hour. OPERATIONS Set each data.
What is the out put #include using namespace std; void main() { int i; for(i=1;i
1 Abstract Data Type (ADT) a data type whose properties (domain and operations) are specified (what) independently of any particular implementation (how)
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
Functions g g Data Flow g Scope local global part 4 part 4.
CSC241 Object-Oriented Programming (OOP) Lecture No. 10.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
C++ Tutorial Hany Samuel and Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Copyright © 2006 by Douglas.
Case Study - Fractions Timothy Budd Oregon State University.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n.
Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.
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+
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 4 – August 30, 2001.
1 Chapter Structured Types, Data Abstraction and Classes Dale/Weems.
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.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
CSC241 Object-Oriented Programming (OOP) Lecture No. 5.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
1 Examples of class Instructor: Mainak Chaudhuri
Const Member Functions Which are read-only? //fraction.h... class Fraction { public: void readin(); void print();
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
C:\Temp\Templates 4 5 Use This Main Program 6.
Lecture #6 Classes and Objects.
1 Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch Read Style Guide (see course webpage)
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Introduction to Programming Lesson 3. #include #include main ( ) { cout
1 Another Example: Complex Class #ifndef _Complex_H #define _Complex_H class Complex { float re, im; // by default private public: Complex(float x = 0,
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The Unified Modeling Language
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
1 C++ Classes and Data Structures Course link…..
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Chapter 12 Classes and Abstraction
Section 3 Review Mr. Crone.
Intro To Classes Review
COMPUTER 2430 Object Oriented Programming and Data Structures I
Chapter Structured Types, Data Abstraction and Classes
null, true, and false are also reserved.
Fraction Abstract Data Type
Classes and Data Abstraction
COMPUTER 2430 Object Oriented Programming and Data Structures I
Programming Structures.
Classes.
Fundamental Programming
Review: C++ class represents an ADT
(4 – 2) Introduction to Classes in C++
Presentation transcript:

ADT Specification Example TYPE ComplexNumber DOMAIN Each complex number can represented by real and imaginary part. OPERATIONS Set the real part set the imaginary part get the real part get the imaginary part plus another complex number minus another complex number multiply another complex number divide another complex number compare with another complex number

Representations of ComplexNumber 2 double variables 2 float variables actual choice of representation depends on actual application (possible values of complex number ) 10 45

class Complex Specification // SPECIFICATION FILE class Complex // declares a class data type { // does not allocate memory public : // public function members void setReal ( double r ) ; void setImg( double im); double getReal(); double getImg(); Complex plus( Complex c); Complex minus(Complex c); Complex multiply(Complex c); Complex divide( Complex c); void print() const ; bool Equal ( Complex c) const ; private : // private data members double real ; double img ; } ; 3

Use of C++ data Type class software that uses the class is called a client variables of the class type are called class objects or class instances client code uses public member functions to handle its class objects

Client Code Using Complex #include “comlex.h” // includes specification of the class int main ( ) { Complex num1, num2; Complex sum; double real, img; cout<<“Enter the real and imaginary parts for the first numer\n”; cin>>ral >> img; num1.setReal(real); num1.setImg(img); cout<<“Enter the real and imaginary parts for the second number\n”; cin>>real>>img; num2.setreal(real); num2.setImg(img); sum = num1.plus(num2); cout<< “the num1 + num2 =“<< sum.print()<,endl; return 0; } 5

Implementation of plus function Complex Complex::plus(Complex c) { Complex sum; sum.real = real + c.real; sum.img = img + c.img; return sum; } // you complete implementations for minus, multiply and divide

Implementation of print function Void Complex::print() const { cout<<“ (“<<real <<“, “<<img<<“)”’ }

Implementation of set functions void Complex::setReal(double r) { real = r; } // you write the implementation of // setImg( double i)

Implementation of Compare bool Complex::Equal(Complex c) { if ( real == c.real && img == c.img) return true; else return false; }

Specification of Complex Class Constructors class Complex // Complex.h { public : // function members Complex( double real, double img ) ; // constructor Complex ( ) ; // default constructor private : // 2 data members double real ; double img ; } ; 10

Implementation of Complex Default Constructor Complex :: Complex ( ) { real = 0 ; img = 0 ; } 11

Implementation of Another Complex Class Constructor Complex :: Complex ( /* in */ double r, /* in */ double i) { real = r ; img = i ; } 12