Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 14. User Defined Classes.

Slides:



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

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.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
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)
CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.
1 Lab Session-XIV CSIT121 Spring 2002 b Namespaces b First Class Travel b Lab Exercise 14 (Demo) b Lab Exercise b Practice Problem.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Lecture 9 Concepts of Programming Languages
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
Review of C++ Programming Part II Sheng-Fang Huang.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R1. ADTs as Classes.
User-Defined Classes Chapter Class Definition t int, float, char are built into C+ t Declare and use –int x = 5; t Create user defined data.
Introduction to Classes in C++
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Procedural and Object-Oriented Programming 13.1.
1 Chapter 13 Introduction to Classes. 2 Topics 12.1 Procedural and Object-Oriented Programming 12.2 Introduction to Classes 12.3 Defining an Instance.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10: User-Defined Classes Problem Solving, Abstraction, and Design.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
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.
11 Introduction to Object Oriented Programming (Continued) Cats.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
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
Pointer to an Object Can define a pointer to an object:
CSCE 210 Data Structures and Algorithms
Procedural and Object-Oriented Programming
CSCE 210 Data Structures and Algorithms
Classes C++ representation of an object
Abstract Data Types Programmer-created data types that specify
Topic 7 Introduction to Classes and Objects
Introduction to Classes
More about OOP and ADTs Classes
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
Lecture 9 Concepts of Programming Languages
Introduction to Classes
More about OOP and ADTs Classes
User-Defined Classes Chapter 10.
Development and Implementation
Classes C++ representation of an object
Chapter 9 Introduction To Classes
Class rational part2.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Lecture 8 Object Oriented Programming (OOP)
Introduction to Classes and Objects
Presentation transcript:

Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 14. User Defined Classes

Prof. amr Goneid, AUC2 User Defined Classes

Prof. amr Goneid, AUC3 User Defined Classes OOP & Classes Data Encapsulation, Classes and objects Class Definition: Private & Public Members Constructors & Destructors Data and Function Members Accessors & Mutators Polymorphism and Overloading Example: Rational Numbers Class Example: Simple String Class

Prof. amr Goneid, AUC4 1. OOP and Classes Object-Oriented Programming (OOP) focuses on creating ADT’s called “Classes” that identify “objects” and how they work together. A class contains “data members” + “function members” in one object. A member function tells an object to “operate on itself” in some way. Objects are “self-contained”, carrying their own operations.

Prof. amr Goneid, AUC5 2. Data Encapsulation, Classes and Objects A Class of objects is a user-defined Abstract Data Type (ADT) An object is an instance of the class Once a class is defined, an object can be declared to be of that type. For example, we have encountered the string class before. Since it has been defined, we can declare: string message; So, now message is an object of that class Classes can be used by more than one program.

Prof. amr Goneid, AUC6 Sharing Classes Class Program Standard ClassesUser Classes

Prof. amr Goneid, AUC7 Classes & Encapsulation C++ classes are similar to structs, with the main difference being that classes can have member functions, or methods, as well as variables, or data members in their definitions. Combining data and operations (methods) together in an object is called encapsulation. An object of a class can operate on itself by the methods or member functions of that class. e.g., an object of class string can operate by:.find.length.at.erase etc

Prof. amr Goneid, AUC8 3. Class Definition: Private & Public Members  Classes use the technique of information hiding to avoid incorrect use of the class. This is done by creating two areas: a public area, and a private area public: Member Functions private: Data members Outside World

Prof. amr Goneid, AUC9 Private & Public Members External world has no access to the private area. Users are allowed to operate on the objects of the class only via public member functions. All member functions of a class have automatic access to all of the data members of that class. Once we make a member variable a private member variable, there is then no way to change its value except by using one of the member functions.

Prof. amr Goneid, AUC10 Class Definition A class definition contains only the prototype for its member functions and the definitions of the data members. It is declared in a class header file (.h) The implementations for the member functions are given elsewhere, in a class implementation file (.cpp) not in the header file. These two files form the Class Library.

Prof. amr Goneid, AUC11 class ClassName { public: function prototypes of methods and data members that are public and can be used by statements outside the class definition. private: prototypes of functions and type definitions and variable declarations of data members that are private and can be used only by statements inside the class definition. }; // a semicolon must appear here General Format of a Class Declaration (in Header File)

Prof. amr Goneid, AUC12 The name of the header file is the class name followed by “.h “, e.g. ClassName.h The name of the implementation file matches that of the header file with an extension of “.cpp”, e.g. ClassName.cpp Application programs using the class are called “client programs”. These programs must include: #include “ClassName.h” #include “Classname.cpp” Class Library Files

Prof. amr Goneid, AUC13 Two special functions in the public part with the same name as the class: The constructor is used to create and initialize objects declared to be of that class There could be more than one constructor to allow for different ways of initializing objects. The destructor is used to remove the objects (specially when the data is allocated dynamically). Only one destructor is allowed. 4. Constructors and Destructors

Prof. amr Goneid, AUC14 //File: Time.h Time Class Header File #ifndef TIME_H// used to avoid multiple definitions #define TIME_H// not part of the class class Time { public: Time(); // constructor, a must ~Time(); // destructor // Function prototypes void setTime (int, int, int); void displayTime (); const private: int hour, minute, second; }; // a semicolon must appear here #endif // TIME_H #include “Time.cpp" 5. Data & Function Members: Example Header File Function cannot change private members

Prof. amr Goneid, AUC15 Implementing Member Functions A member function is implemented in the implementation file (.cpp). The format is: :: (param list) {..function body..} The Scope resolution operator: :: prefix for each member function Informs the compiler that the function is a member of the class

Prof. amr Goneid, AUC16 Example Implementation File //File: Time.cpp Time Class Implementation File #include using namespace std; Time :: Time() {hour = minute = second = 0; } Time::~Time() { }// do nothing void Time :: setTime(int h, int m, int s) { hour = (h >= 0 && h < 24) ? h : 0; minute = (m >= 0 && m < 60) ? m : 0; second = (s >= 0 && s < 60) ? s : 0; } void Time :: displayTime() const { cout << hour << “:“ << minute << “:“ << second << endl; }

Prof. amr Goneid, AUC17 Objects Object: a particular instance of the class. To declare an object in a client program: The same way we declare a variable but with the type = class name, e.g. Time t1; Time T[20]; This will also invoke the constructor. The Dot Operator allows an object to access its public members, e.g. t1.displayTime();

Prof. amr Goneid, AUC18 Example of Application (Client) File //File: TimeAppl.cpp Time Class Application File #include “Time.h“ #include using namespace std; int main() { Time t1, t2; cout << “Start Time is: “; t1.displayTime ( ); t2.setTime (5, 10, 30); cout << “End Time is: “;t2.displayTime ( ); return 0; }

Prof. amr Goneid, AUC19 Remarks Every class should have a default constructor (without parameters). The default constructor may also be implemented as: Time ::Time( ) : hour(0), minute(0), second(0) { } When used in the application program as: Time t1; then t1.hour, t1.minute and t1.second will be set initially to zero member initializers

Prof. amr Goneid, AUC20 Remarks An explicit value constructor may also be added and defined as: Time( int, int, int); and is implemented as: Time :: Time( int h, int m, int s) { hour = h; minute = m; second = s; } We can use it in the application to initialize an object: Time t1(7,45,0); const functions cannot modify private data members

Prof. amr Goneid, AUC21 6. Accessors and Mutators It is possible to extract a private data member using an accessor function. For example, to access “hour”: int getHour( ) const; // Prototype int Time::getHour( ) const// function definition {return hour; } Time t1; int h = t1.getHour( );// invoking the function A mutator member function (like setTime( )) will be able to change the private data members.

Prof. amr Goneid, AUC22 7. Polymorphism & Overloading Defining several functions with the same name is called function overloading The presence of more than one constructor for the class is an example of function overloading. Polymorphism is what allows functions with the same name to do different things based on its arguments

Prof. amr Goneid, AUC23 8. Example: ADT rational Abstraction: A rational number (fraction) is a rational representation of two integers (x,y). Elements or Members: A numerator (x) and a denominator (y), both are integers. (y) cannot be zero Relationship: The representation is equivalent to x / y

Prof. amr Goneid, AUC24 ADT rational (continued) Fundamental Operations: Read a fraction from keyboard Display a fraction on the screen Add Fractionsf = f1 + f2 (e.g. ½ + ¼ = ¾) Subtract Fractionsf = f1 – f2 (e.g. ½ - 1/3 = 1/6) Multiply Fractionsf = f1 * f2 (e.g. ½ * ¾ = 3/8 ) Divide Fractionsf = f1 / f2 (e.g. 1/5 / ¼ = 4/5 ) Reduce Fractions (e.g. 2/6 = 1/3)

Prof. amr Goneid, AUC25 Implementing a rational Class We will have 3 files: “rational.h” to contain the class definition. “rational.cpp” to contain the implementation of the member functions. “RationalTest.cpp” an application file to test the class.

Prof. amr Goneid, AUC26 The Header File: rational.h // File: rational.h // Rational class definition #ifndef RATIONAL_H// used to avoid multiple definitions #define RATIONAL_H// not part of the class class rational { public: // Member functions // Constructors rational();// Default Constructor rational(int);// Initialize numerator with denom = 1 rational(int, int);// Initialize both numerator and denom.

Prof. amr Goneid, AUC27 The Header File: rational.h (cont.) void setNum(int);// Set numerator and denominator void setDenom(int); rational multiply(const rational &f);// Multiply fractions rational divide(const rational &f);// Divide fractions rational add(const rational &f);// Add Fractions rational subtract(const rational &f);// Subtract Fractions void readRational();// Read a fraction void displayRational() const;// Display a fraction rational reduce() const;// Reduce fraction // Accessors int getNum() const; int getDenom() const;

Prof. amr Goneid, AUC28 The Header File: rational.h (cont.) // Operator Style // Add object to parameter rational operator + (const rational &); // Test equality of object and parameter bool operator == (const rational &); private: // Data members (attributes) int num;// private data field int denom;// private data field };// Note -- a class definition MUST end with a semicolon #endif // RATIONAL_H #include "rational.cpp"

Prof. amr Goneid, AUC29 The Implementation File: rational.cpp // File: Rational.cpp // Rational class implementation #include using namespace std; // Member functions // Constructors rational::rational()// Default Costructor { num = 0; denom = 0; } rational::rational(int n)// Class Constructor { num = n; denom = 1; } rational::rational(int n, int d)// Class Constructor { num = n; denom = d; }

Prof. amr Goneid, AUC30 The Implementation File: rational.cpp // Set numerator and denominator void rational::setNum(int n) { num = n; } void rational::setDenom(int d) { denom = d; } // Multiply fractions rational rational::multiply(const rational &f) { rational temp(num * f.num, denom * f.denom); return temp; } // Divide fractions rational rational::divide(const rational &f) { rational temp(num * f.denom, denom * f.num); return temp; }

Prof. amr Goneid, AUC31 The Implementation File: rational.cpp // Add fractions rational rational::add(const rational &f) { rational temp(num * f.denom + f.num * denom, denom * f.denom); return temp; } // Subtract Fractions rational rational::subtract(const rational &f) { rational temp(num * f.denom - f.num * denom, denom * f.denom); return temp; }

Prof. amr Goneid, AUC32 The Implementation File: rational.cpp // Read a fraction void rational::readRational() { char slash; // storage for / do { cout << "Enter numerator / denominator: "; cin >> num >> slash >> denom; } while (slash != '/'); } // Display a fraction void rational::displayRational() const { cout << num << '/' << denom; }

Prof. amr Goneid, AUC33 The Implementation File: rational.cpp // Reduce rational rational rational::reduce() const { int n,m,rem,gcd; // Get the two integers n = abs(num); m = abs(denom); while (n > 0) { rem = m % n; m = n; n = rem; } gcd = m; rational g (num/gcd, denom/gcd); return g; }

Prof. amr Goneid, AUC34 The Implementation File: rational.cpp // Accessors int rational::getNum() const { return num; } int rational::getDenom() const { return denom; } // Operator-Like rational rational::operator + (const rational &f2) { rational temp (num * f2.denom + f2.num * denom, denom * f2.denom); return temp; } bool rational::operator == (const rational &f) { return (num == f.num && denom == f.denom); }

Prof. amr Goneid, AUC35 Remarks It is possible to create a temporary class object within a member function and initialize it using a constructor, e.g. rational rational::multiply(const rational &f) { rational temp (num * f.num, denom * f.denom); return temp; } A regular C++ operator can be “overloaded” to perform a different action on class objects. A member function can be defined to do this. If  is an operator, the prototype will be: operator  (parameter); e.g. bool operator == (const rational &);

Prof. amr Goneid, AUC36 Remarks The definition will be: ::operator  (parameter); e.g. bool rational::operator == (const rational &f) { return (num == f.num && denom == f.denom); } For example: rational a, b; An expression of the form (a == b) will be evaluated as: (a.num == b.num) && (a.denom == b.denom)

Prof. amr Goneid, AUC37 The Application File: RationalTest.cpp // File: RationalTest.cpp // Tests the rational class #include #include "rational.h“ using namespace std; int main() { rational f1, f2; rational f3; // Read two rational numbers cout << "Enter 1st fraction:" << endl; f1.readRational(); cout << "Enter 2nd fraction:" << endl; f2.readRational();

Prof. amr Goneid, AUC38 The Application File: RationalTest.cpp // Fraction Arithmetic f3 = f1.multiply(f2); f1.displayRational(); cout << " * "; f2.displayRational(); cout << " = "; f3.displayRational(); cout << " = "; f3 = f3.reduce(); f3.displayRational(); cout << endl; f3 = f1.divide(f2); f1.displayRational(); cout << " / "; f2.displayRational(); cout << " = "; f3.displayRational(); cout << " = ";f3 = f3.reduce(); f3.displayRational(); cout << endl;

Prof. amr Goneid, AUC39 The Application File: RationalTest.cpp f3 = f1.add(f2); f1.displayRational(); cout << " + "; f2.displayRational(); cout << " = "; f3.displayRational(); cout << " = ";f3 = f3.reduce(); f3.displayRational(); cout << endl; f3 = f1 + f2;// uses operator “+” for addition f1.displayRational(); cout << " + "; f2.displayRational(); cout << " = "; f3.displayRational(); cout << " = ";f3 = f3.reduce(); f3.displayRational(); cout << endl;

Prof. amr Goneid, AUC40 The Application File: RationalTest.cpp f3 = f1.subtract(f2); f1.displayRational(); cout << " - "; f2.displayRational(); cout << " = "; f3.displayRational(); cout << " = "; f3 = f3.reduce(); f3.displayRational(); cout << endl; return 0; }

Prof. amr Goneid, AUC41 Sample Run of RationalTest.cpp Enter 1st fraction: Enter numerator / denominator: 2/6 Enter 2nd fraction: Enter numerator / denominator: 3/8 2/6 * 3/8 = 6/48 = 1/8 2/6 / 3/8 = 16/18 = 8/9 2/6 + 3/8 = 34/48 = 17/24 2/6 - 3/8 = -2/48 = -1/24 Press any key to continue

Prof. amr Goneid, AUC42 9. Example: Simple String Class Here we build a simple string class to do few tasks on our own string objects, e.g. read, write, get the character at a given location, etc. We implement the string as a dynamic array of characters. We will have 3 files: “simpleString.h” to contain the class definition. “simpleString.cpp” to contain the implementation of the member functions. “simpleStringTest.cpp” an application file to test the class.

Prof. amr Goneid, AUC43 The Header File: simpleString.h // File simpleString.h // Simple string class definition #ifndef SIMPLESTRING_H #define SIMPLESTRING_H class simpleString { public: // Member Functions // Constructors simpleString(); simpleString(int ); // Destructor ~simpleString();

Prof. amr Goneid, AUC44 The Header File: simpleString.h // Function Prototype definition // Read a simple string void readString(); // Display a simple string void writeString() const; // Retrieve the character at a specified position // Returns the character \0 if position is out of bounds char at(int) const; // Return the string length int getLength() const; // Return the string capacity int getCapacity() const; // Get the contents into an array void getContents(char[ ]) const;

Prof. amr Goneid, AUC45 The Header File: simpleString.h private: // Data members (attributes) // maximum size int capacity; // pointer to a dynamic storage array char *s; // current length int length; }; #endif //SIMPLESTRING_H #include "simpleString.cpp"

Prof. amr Goneid, AUC46 The Implementation File: simpleString.cpp // File: simplestring.cpp // Simple string class implementation #include using namespace std; // Member Functions... // default constructor, capacity = 255 simpleString::simpleString() { s = new char[255]; capacity = 255; length = 0; } // Constructor with argument, capacity is mVal simpleString::simpleString(int mVal) { s = new char [mVal]; capacity = mVal; length = 0;} // Class Destructor simpleString::~simpleString() { delete [ ] s;}

Prof. amr Goneid, AUC47 The Implementation File: simpleString.cpp // Read a simple string void simpleString::readString() { char next; int pos = 0; cin.get(next); while ((next != '\n') && (pos < capacity)) { // Insert next in array contents s[pos] = next; pos++; cin.get(next); } length = pos; }

Prof. amr Goneid, AUC48 The Implementation File: simpleString.cpp // Write a simple string void simpleString::writeString() const { for (int pos = 0; pos < length; pos++) cout << s[pos]; } // Character at (pos). Returns \0 if position is out of bounds char simpleString::at(int pos) const { const char nullcharacter = '\0'; if ((pos = length)) { cerr << "position " <<pos << " not defined." << endl; return nullcharacter; } else return s[pos]; }

Prof. amr Goneid, AUC49 The Implementation File: simpleString.cpp // Return the string length int simpleString::getLength() const { return length; } // Return the string capacity int simpleString::getCapacity() const { return capacity; } // Get the contents into an array void simpleString::getContents(char str[ ]) const { for (int i = 0; i < length; i++) str[i] = s[i]; }

Prof. amr Goneid, AUC50 The Application File: simpleStringTest.cpp // File: simpleStringTest.cpp // Tests the simple string class #include "simpleString.h“ #include using namespace std; int main() { simpleString S1; simpleString S2(20); cout << S1.getCapacity() <<" "<<S1.getLength() << endl; cout << S2.getCapacity() <<" "<<S2.getLength() << endl;

Prof. amr Goneid, AUC51 The Application File: simpleStringTest.cpp // Read in a string. cout << "Enter a string and press RETURN: "; S1.readString(); // Display the string just read. cout << "The string read was: "; S1.writeString(); cout << endl; // Display each character on a separate line. cout << "The characters in the string follow:" << endl; for (int pos = 0; pos < S1.getLength(); pos++) cout << S1.at(pos) << endl; return 0; }

Prof. amr Goneid, AUC52 Sample Run of simpleStringTest.cpp Enter a string and press RETURN: User Classes The string read was: User Classes The characters in the string follow: U s e r C l a s e s Press any key to continue