Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 11: Examples—creating a basic class.

Similar presentations


Presentation on theme: "ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 11: Examples—creating a basic class."— Presentation transcript:

1 ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 11: Examples—creating a basic class

2 Lecture outline Announcements / reminders  Lab 3 due Monday, 10/1  No Monday class (Columbus day) next week  Exam 1: 10/10 (review lecture on Friday, 10/05) Last time  Constructors  Using classes 3/13/2016 ECE 264: Lecture 11 2

3 Classes: Programmer Defined Types Classes allow programmer to define their own types Constructor(s): function called at object creation  Default constructor takes no arguments Should always define—set default value(s) for data member(s)  Parameterized constructors initializes data members to specific values Once class is created, can  Create objects (instances of class)  Call member functions  Change data members using functions Can’t directly access if private 3/13/2016 ECE 264: Lecture 11 3

4 Example: Creating a class Say we have a class to represent a point in a 2-dimensional plane  What data should this class hold?  What should the constructor(s) look like?  How would we write the mutator function(s)?  How would we write the accessor function(s)?  How would we write additional functions to: “Move” the point by a constant amount? Print the point in the format (X, Y)? 3/13/2016 ECE 264: Lecture 11 4

5 Example: Creating a class (Point.h) // Class to represent points. class Point { private: double xval, yval; public: Point(); // Default Construcutor // Constructor uses default arguments to allow //two values. Point(double x, double y); // mutator function void Setpoint (double x, double y); // Accessor. double x(); double y(); 3/13/2016 ECE 264: Lecture 10 5

6 Example: Creating a class (Point.h) // Distance to another point. double dist(Point other) ; // Add or subtract two points. Point add(Point b); Point sub(Point b); // Move the existing point. void move(double a, double b); // function that displays a welcome message void displayMessage(); } 3/13/2016 ECE 264: Lecture 10 6

7 Example: Creating a class (Point.cpp) #include #include “Point.h” // Constructor Point :: Point() { xval = 0; yval = 0; } // Parameterized Constructor Point :: Point(double x, double y ) { xval = x; yval = y; } void Point :: Setpoint(double x, double y ) { xval = x; yval = y; } // Extractors. double Point:: double x() { return xval; } double Point :: y() { return yval; } 3/13/2016 ECE 264: Lecture 11 7

8 Example: Creating a class (Point.cpp) // Distance to another point. double Point :: dist(Point other) { double xd = xval - other.xval; double yd = yval - other.yval; return sqrt(xd*xd + yd*yd); } // Add or subtract two points. Point Point :: add(Point b) { return Point(xval + b.xval, yval + b.yval); } Point Point :: sub(Point b) { return Point(xval - b.xval, yval - b.yval); } 3/13/2016 ECE 264: Lecture 11 8

9 Example: Creating a class (Point.cpp) // Move the existing point. void Point::move(double a, double b) { xval += a; yval += b; } // Print the point on the stream void Point::displayMessage() { cout << "(" << xval << "," << yval << ")"; } 3/13/2016 ECE 264: Lecture 11 9

10 Example: Main Program (test.cpp) main() { // Some points. Point a(5.2, -4.8); Point b(3.0, 9.0); Point c(-3.38); Point d; // Some arith. on the points. d = b.sub(c); // Point variables are not references. Point fred[5]; for(int m = 0; m < 5; m++) fred[m] = a; double w = 4.5; double x = -2.31; for(int m = 0; m < 5; m++) { fred[m].move(w, x); w += 3.4; x -= 1.3; } for(int m = 0; m < 5; m++) { fred[m].displayMessage(); } 3/13/2016 ECE 264: Lecture 11 10

11 Final notes Next time More class code examples UML class diagrams (if the time is permited) 3/13/2016 ECE 264: Lecture 11 11


Download ppt "ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 11: Examples—creating a basic class."

Similar presentations


Ads by Google