Engineering Problem Solving with C++, Etter/Ingber
description
Transcript of Engineering Problem Solving with C++, Etter/Ingber
![Page 1: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/1.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
1
Engineering Problem Solving with C++, Etter/Ingber
Chapter 8
An Introduction to Classes
![Page 2: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/2.jpg)
An Introduction To Classes
Programmer Defined Types Design and Implementation of Classes Class Composition Constructors The vector class private Methods
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
2
![Page 3: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/3.jpg)
PROGRAMMER DEFINED TYPES
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
3
![Page 4: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/4.jpg)
Data Types
A data type is a concrete implementation of a concept.
Built in types include:– int, double, char
Pre-defined class types include:– string, istream, ostream
Real world applications work with concepts that are not available as built-in or pre-defined types.
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
4
![Page 5: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/5.jpg)
Programmer Defined Types Consider the concept of a point in a plane, defined as a:
– xCoordinate
– yCoordinate
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.
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
5
![Page 6: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/6.jpg)
04/21/23 Engineering 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.
Encapsulation hides the implementation from the user.
A good public interface and encapsulation allow for efficient maintenance and expandability of the class type.
![Page 7: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/7.jpg)
DESIGN AND IMPLEMENTATION OF CLASSES
class declaration
class implementation
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
7
![Page 8: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/8.jpg)
04/21/23 Engineering 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
![Page 9: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/9.jpg)
04/21/23 Engineering 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.
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.
![Page 10: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/10.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
10
Example - Point class Declaration//Point class declaration:// filename: Point.hclass Point{
private: double xCoord, yCoord;//private attributespublic:
//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!
![Page 11: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/11.jpg)
04/21/23 Engineering 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.
![Page 12: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/12.jpg)
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.
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
12
Syntax:return type class-name::method-name([parameter list])[modifier]
![Page 13: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/13.jpg)
04/21/23 Engineering 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.
The class implementation file must include the class declaration (.h file).
![Page 14: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/14.jpg)
04/21/23 Engineering 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 <cmath> //Required for sqrt(),pow()
//accessor methoddouble Point::getX() const{ return xCoord;}
//mutator methodvoid Point::setX(double newX){ xCoord = newX;}
![Page 15: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/15.jpg)
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;
}
04/21/23 Engineering 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.
![Page 16: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/16.jpg)
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 <iostream> //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;
}04/21/23 Engineering Problem Solving with
C++, second edition, J. Ingber16
Output:5.1
![Page 17: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/17.jpg)
UML CLASS DIAGRAM FOR POINT
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
17
Point
-xCoord-yCoord
+getX()+getY()+setX()+setY()
+operator –()
Note: - => private+ => public
![Page 18: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/18.jpg)
CLASS COMPOSITION
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
18
![Page 19: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/19.jpg)
Class Composition Object oriented problem solutions are built around class hierarchies and associations.
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.
Class composition allows for efficient building of complex programmer defined types.
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
19
![Page 20: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/20.jpg)
Example:
A rectangle is a shape that has a:– point of origin– width– height
Implement the concept of a rectangle by defining a programmer defined type named Rectangle.
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
20
![Page 21: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/21.jpg)
UML CLASS COMPOSITION DIAGRAM
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
21
Rectangle
Point
1
-double width-double height-Point origin
![Page 22: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/22.jpg)
CONSTRUCTORS
default constructors
parameterized constructors
function overloading
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
22
![Page 23: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/23.jpg)
Constructors
A constructor is a method designed to build an instance of a class.
A constructor has the same name as its class. A constructor has no specified return value. A constructor is called automatically each time an
object is defined.
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
23
Syntax:class-name::class-name([parameter list])[:initialization list]{ }
![Page 24: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/24.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
24
Constructors
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.
![Page 25: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/25.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
25
Default Constructor: Point
Prototype: add to Point.h
Point();
or
Point(double x=0, double y=0);
x=0 and y=0 are default parameters.
![Page 26: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/26.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
26
Default Constructor: Point Definition: add to Point.cpp
Point::Point(){ xCoord = yCoord = 0;}
or
Point::Point(double x, double y){ xCoord = x; yCoord = y;}
![Page 27: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/27.jpg)
Example: Default Constructor
Point p1; //Default constructor is called.
Memory snapshot:
Point p1
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
27
0.0
0.0
double xCoord
double yCoord
![Page 28: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/28.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
28
Parameterized Constructor: Point
Prototype: add to Point.h
Point(double xVal, double yVal); Definition: add to Point.cpp
Point::Point(double xVal, double yVal){ xCoord = xVal; yCoord = yVal;}
![Page 29: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/29.jpg)
Example: Parameterized Constructor
Point p1(1.5, 4.2); //Parameterized constructor.
Memory snapshot:
Point p1
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
29
1.5
4.2
double xCoord
double yCoord
![Page 30: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/30.jpg)
Point.h Revisited:
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
30
//Point class declaration:// filename: Point.hclass Point{
private: double xCoord, yCoord;//private attributespublic:
//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.
![Page 31: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/31.jpg)
Function Overloading Function overloading occurs when one or more
functions have the same name.– Point();– Point(double x, double y);
Overloaded functions share the same name, but each function must have a unique function signature.
A function signature includes the :– function name
– parameter list
– modifiers
– return value is NOT part of the function signature.04/21/23 Engineering Problem Solving with
C++, second edition, J. Ingber31
![Page 32: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/32.jpg)
04/21/23 Engineering 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();};
![Page 33: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/33.jpg)
THE VECTOR CLASSparameter passing
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
33
![Page 34: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/34.jpg)
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.
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
34
![Page 35: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/35.jpg)
04/21/23 J. Ingber Introduction to C++ 35
vector Objects We can define an instance of the vector class
in a type declaration statement. Syntax:vector<data-type> identifier[(size,initial-value)]
Examples:vector <double> list; //empty vectorvector<string> wordList(n); //capacity:n strings
//vector of 8 integers, each initialized to 0vector<int> intList(8,0);
![Page 36: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/36.jpg)
04/21/23 J. Ingber Introduction to C++ 36
vector class
#include <vector> //pre-defined in STL 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
![Page 37: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/37.jpg)
04/21/23 J. Ingber Introduction to C++ 37
Operators
The square bracket operator ( [ ] ) is defined for vector objects.
The assignment operator ( = )is defined for vectors of the same type.
Example:vector<int> list(10);vector<int>newList;list[0]=10;…newList = list;cout << newList.size() << endl; //method
![Page 38: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/38.jpg)
Parameter Passing
When a vector object is passed as an argument to a function, the default is pass by value.
Pass by reference can be specified with the & operator.
Example:void scale(vector<double>& dVec, double sFactor);
void print(vector<double> dVec);//pass by value
void print2(const vector<double>& dVec);
Point closeToOrigin(vector<Point> pointVec);
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
38
![Page 39: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/39.jpg)
PRIVATE METHODS
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
39
![Page 40: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/40.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
40
Helper Methods
private or protected member functions. Called by other member functions. Designed to "help" behind the scenes.
![Page 41: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/41.jpg)
04/21/23 Engineering 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 0 - 360 degrees.
Methods:translate(double dx, double dy):rotate (int dDegrees);
![Page 42: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/42.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
42
Translation - change position but not orientation
![Page 43: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/43.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
43
Rotation - change orientation around anchor point
![Page 44: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/44.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
44
Class Declarationclass 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};
![Page 45: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/45.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
45
Class Implementation
//ConstructorsUnitVector::UnitVector(double initX, double initY, int initO)) : x(initX), y(initY), orientation(initO){
fixOrientation();}UnitVector::UnitVector( ): x(0), y(0), orientation(0){ }
![Page 46: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/46.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
46
Member Function Definitions
// rotate the calling vectorvoid UnitVector::rotate(int d_orient) { orientation += d_orient;
fixOrientation();}// translate the calling vector void UnitVector::translate(double dx, double dy){ x += dx;
y += dy;}
![Page 47: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/47.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
47
Helper Function Definition
//This function adjusts the orientation value //Original orientation may be < 0 or >= 360) //Adjusted orientation is 0<=orientation < 360. void UnitVector::fixOrientation() {
if(orientation < 0) orientation = 360 + orientation%360;
elseorientation = orientation%360;
}
![Page 48: Engineering Problem Solving with C++, Etter/Ingber](https://reader035.fdocuments.us/reader035/viewer/2022062518/5681470b550346895db44315/html5/thumbnails/48.jpg)
04/21/23 Engineering 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;