6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 8 An Introduction.

Slides:



Advertisements
Similar presentations
Engineering Problem Solving With C++ An Object Based Approach Additional Topics Chapter 10 Programming with Classes.
Advertisements

Lesson 13 Introduction to Classes CS1 Lesson Introduction to Classes1.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Class and Objects.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
תכנות מונחה עצמים Object Oriented Programming (OOP) אתגר מחזור ב ' Classes – - המשך.
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Lecture 5-6 OOP Overview. Outline A Review of C++ Classes (Lecture 5) OOP, ADTs and Classes Class Definition, Implementation and Use Constructors and.
Engineering Problem Solving With C++ An Object Based Approach Chapter 8 Introduction to Classes.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 15: Class diagrams; class relationships.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Review of C++ Programming Part II Sheng-Fang Huang.
Chapter 12: Adding Functionality to Your Classes.
Object Oriented Programming Concepts OOP – reasoning about a program as a set of objects rather than as a set of actions Object – a programming entity.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R1. ADTs as Classes.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
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.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Fall 2012 Lecture 8: File I/O; Introduction to classes.
Chapter 10 Introduction to Classes
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
CS 11 C++ track: lecture 1 Administrivia Need a CS cluster account sysadmin/account_request.cgi Need to know UNIX (Linux)
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
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();
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 29: Operator overloading.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Xiaoyan Li CSC211 Data Structures Lecture 2 ADT and C++ Classes (I) Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 2 ADT and C++ Classes (I) Instructor: Zhigang Zhu Department of Computer Science City.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
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 4, 2005 Lecture Number: 27.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Base Classes and Derived.
Copyright © 2012 Pearson Education, Inc. Chapter 10 Advanced Topics.
11/07/11Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 8 An Introduction.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Abstract Data Types Programmer-created data types that specify
Introduction to C++ Systems Programming.
A First C++ Class – a Circle
Introduction to Classes
More about OOP and ADTs Classes
group work #hifiTeam
Introduction to Classes
More about OOP and ADTs Classes
COMS 261 Computer Science I
Dr. R Z Khan Handout-3 Classes
Chapter 9 Introduction To Classes
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Lecture 8 Object Oriented Programming (OOP)
Classes and Objects Systems Programming.
Introduction to Classes and Objects
Presentation transcript:

6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 8 An Introduction to Classes

An Introduction To Classes 4 Programmer Defined Types 4 Design and Implementation of Classes 4 Class Composition 4 Constructors  The vector class  private M ethods 6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 2

PROGRAMMER DEFINED TYPES 6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 3

Data Types 4 A data type is a concrete implementation of a concept. 4 Built in types include: –int, double, char 4 Pre-defined class types include: –string, istream, ostream 4 Real world applications work with concepts that are not available as built-in or pre-defined types. 6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 4

Programmer Defined Types 4 Consider the concept of a point in a plane, defined as a: –xCoordinate –yCoordinate 4 Subtraction between points can be defined as a binary operation that returns the distance between two points in a plane. Point p1, p2; double dist; dist = p2-p1;  As a programmer, we can define a new type named Point by defining a C++ class named Point. 6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 5

6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 6 Programmer Defined Types  A well-designed class type provides: –a good public interface. –encapsulation of the implementation.  A good public interface provides a complete yet minimal set of public operations. 4 Encapsulation hides the implementation from the user.  A good public interface and encapsulation allow for efficient maintenance and expandability of the class type.

DESIGN AND IMPLEMENTATION OF CLASSES class declaration class implementation 6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 7

6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 8 Defining C++ Classes  The definition of a class consists of two parts: –The class declaration saved in a file with a.h extension –The class implementation saved in a file with a.cpp extension the.cpp file should #include the.h file

6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 9 The class Declaration  A class declaration begins with the key word class followed by an identifier that specifies the name of the new class. 4 The body of the class declaration is a statement block that includes: –declaration statements for the data members (attributes) –prototypes for the member functions (methods)  Keywords public, protected and private control the accessibility of the attributes and methods.

6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 10 Example - Point class Declaration //Point class declaration: // filename: Point.h class Point { private: double xCoord, yCoord;//private attributes public: //Two accessor methods: double getX() const; double getY() const; //Two mutator methods: void setX(double newX); void setY(double newY); //Distance Formula double operator –(const Point& p2) const; }; semicolon is required!

6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 11 The Point class Declaration  Two private attributes (encapsulation).  Five public methods (public interface).  The use of the const modifier in the prototypes of the accessor methods prohibits modification of the calling object.  The const modifier is not used with the mutator methods because the intent of a mutator method is modification of the calling object.

Method Definition  A method is a function that is a member of class.  A method header must include the name of a class, followed by the scope resolution operator (::) and the name of the method. 6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 12 Syntax: return type class-name::method-name([parameter list])[modifier]

6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 13 The class Implementation  The class implementation includes a definition for each of the methods declared in the class declaration.  The scope resolution operator (::) is required in the method header to associate the method name with the name of the class in which the method is declared to be a member. 4 The class implementation file must include the class declaration (.h file).

6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 14 Implementation of Point class //implementation for Point //filename: Point.cpp #include "Point.h" //Required for Point #include //Required for sqrt(),pow() //accessor method double Point::getX() const { return xCoord; } //mutator method void Point::setX(double newX) { xCoord = newX; }

Implementation of Point class //Distance Formula double Point::operator –(const Point& rhs) const { double t1, t2, d; t1 = rhs.xCoord – xCoord; //(x2-x1) t2 = rhs.yCoord – yCoord; //(y2-y1) d = std::sqrt( std::pow(t1,2) + std::pow(t2,2) ); return d; } 6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 15 std:: is required when the using namespace std; statement is not given. xCoord and yCoord are provided by the calling object.

The Calling Object  An object is an instance of a class.  Objects reference (call) the public methods defined for their class. Example: #include "Point.h" //Required for Point #include //Required for cout int main() { //A Point has an xCoord and a yCoord Point p1; //p1 is a Point p1.setX(5.1); //p1 is the calling object std::cout << p1.getX() << std::endl; return 0; } 6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 16 Output: 5.1

UML CLASS DIAGRAM FOR POINT 6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 17 Point -xCoord -yCoord +getX() +getY() +setX() +setY() +operator –() Note: - => private + => public

CLASS COMPOSITION 6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 18

Class Composition  Object oriented problem solutions are built around class hierarchies and associations. 4 Class composition is an association used to model a "has-a" relationship between classes: –the whole "has-a" component. –the component is "part-of" the whole. 4 Class composition allows for efficient building of complex programmer defined types. 6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 19

Example: 4 A rectangle is a shape that has a: – point of origin –width –height 4 Implement the concept of a rectangle by defining a programmer defined type named Rectangle. 6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 20

UML CLASS COMPOSITION DIAGRAM 6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 21 Rectangle Point 1 -double width -double height -Point origin

CONSTRUCTORS default constructors parameterized constructors function overloading 6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 22

Constructors  A constructor is a method designed to build an instance of a class.  A constructor has the same name as its class. 4 A constructor has no specified return value. 4 A constructor is called automatically each time an object is defined. 6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 23 Syntax: class-name::class-name([parameter list])[:initialization list] { }

6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 24 Constructors 4 There are two forms of constructors: –Default Constructor constructor with no formal parameters, other than default parameters. attributes are initialized to the default value. –Parameterized Constructor constructor with one or more formal parameters. constructor arguments determine the initial values of the attributes.

6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 25 Default Constructor: Point 4 Prototype: add to Point.h Point(); or Point(double x=0, double y=0); x=0 and y=0 are default parameters.

6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 26 Default Constructor: Point 4 Definition: add to Point.cpp Point::Point() { xCoord = yCoord = 0; } or Point::Point(double x, double y) { xCoord = x; yCoord = y; }

Example: Default Constructor Point p1; //Default constructor is called. Memory snapshot: Point p1 6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber double xCoord double yCoord

6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 28 Parameterized Constructor: Point 4 Prototype: add to Point.h Point(double xVal, double yVal); 4 Definition: add to Point.cpp Point::Point(double xVal, double yVal) { xCoord = xVal; yCoord = yVal; }

Example: Parameterized Constructor Point p1(1.5, 4.2); //Parameterized constructor. Memory snapshot: Point p1 6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber double xCoord double yCoord

Point.h Revisited: 6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 30 //Point class declaration: // filename: Point.h class Point { private: double xCoord, yCoord;//private attributes public: //Two constructors: Point(); Point(double x, double y); //Two accessor methods: double getX() const; double getY() const; //Two mutator methods: void setX(double newX); void setY(double newY); //Distance Formula double operator –(const Point& p2) const; }; Point constructors are example of overloaded methods.

Function Overloading 4 Function overloading occurs when one or more functions have the same name. –Point(); –Point(double x, double y); 4 Overloaded functions share the same name, but each function must have a unique function signature. 4 A function signature includes the : –function name –parameter list –modifiers –return value is NOT part of the function signature. 6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 31

6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 32 Practice! Implement the following class declaration: class Rational {private: int num, denom; public: rational( );// initialize to 1/1 rational(int n, int d); int getNum(); int getDenom(); };

THE VECTOR CLASS parameter passing 6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 33

The vector class  The vector class is a pre-defined class template included in the C++ Standard Template Library.  The vector class provides a generic implementation of the concept of an array.  vector objects can increase and decrease their size dynamically. 6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 34

6/24/2016J. Ingber Introduction to C++35 vector Objects  We can define an instance of the vector class in a type declaration statement. Syntax: vector identifier[(size,initial-value)] Examples: vector list; //empty vector vector wordList(n); //capacity:n strings //vector of 8 integers, each initialized to 0 vector intList(8,0);

6/24/2016J. Ingber Introduction to C++36 vector class  #include //pre-defined in STL 4 Common member functions: empty() // returns true if vector contains no values pop_back() // deletes last element in vector push_back() // add element to end of vector resize() // changes the size of vector size() // returns the size of vector capacity() // returns the capacity of vector

6/24/2016J. Ingber Introduction to C++37 Operators  The square bracket operator ( [ ] ) is defined for vector objects. 4 The assignment operator ( = )is defined for vectors of the same type. Example: vector list(10); vector newList; list[0]=10; … newList = list; cout << newList.size() << endl; //method

Parameter Passing  When a vector object is passed as an argument to a function, the default is pass by value. 4 Pass by reference can be specified with the & operator. Example: void scale(vector & dVec, double sFactor); void print(vector dVec);//pass by value void print2(const vector & dVec); Point closeToOrigin(vector pointVec); 6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 38

PRIVATE METHODS 6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 39

6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 40 Helper Methods  private or protected member functions. 4 Called by other member functions. 4 Designed to "help" behind the scenes.

6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 41 Example Design a class to implement the concept of a unit vector. Attributes: an anchor point (the base of the vector) an orientation of degrees. Methods: translate(double dx, double dy): rotate (int dDegrees);

6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 42 Translation - change position but not orientation

6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 43 Rotation - change orientation around anchor point

6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 44 Class Declaration class UnitVector { public: UnitVector(); // contstructors UnitVector(double init_x, double init_y, int init_orientation); void rotate(int d_orient); // rotate the vector void translate(double dx, double dy); // translate the // vector. private: //helper function void fixOrientation(); // Calculate a legal orientation double x, y; // The anchor point of the object. int orientation; //orientation };

6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 45 Class Implementation //Constructors UnitVector::UnitVector(double initX, double initY, int initO)) : x(initX), y(initY), orientation(initO) { fixOrientation(); } UnitVector::UnitVector( ): x(0), y(0), orientation(0) { }

6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 46 Member Function Definitions // rotate the calling vector void UnitVector::rotate(int d_orient) {orientation += d_orient; fixOrientation(); } // translate the calling vector void UnitVector::translate(double dx, double dy) {x += dx; y += dy; }

6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 47 Helper Function Definition //This function adjusts the orientation value //Original orientation may be = 360) //Adjusted orientation is 0<=orientation < 360. void UnitVector::fixOrientation() { if(orientation < 0) orientation = orientation%360; else orientation = orientation%360; }

6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 48 Practice! Which of the statements on the right is valid within a main program which contains an include for this header file. class rational {private: int num, denom; int LCD( ); public: rational( ); rational(int n, int d); void input (istream & istr ); void output (ostream & ostr); void reduce ( ); };  rational A;  rational B(5, 9);  input(A);  B.output(cout);  int div=B.LCD();  A.denom = 3;  B.reduce;