In many programming situations, objects in programs represent C++ Objects as Physical Objects: things that can be felt or seen. These situations provide vivid examples of the correspondence between the program and the real world. We’ll look at two such situations: widget parts and graphics circles.
Widget Parts as Objects
The smallobj class in the last example had only one data item. Let’s look at an example of a somewhat more ambitious class. (These are not the same ambitious classes discussed in political science courses.) We’ll create a class based on the structure for the widget parts inventory, last seen in such examples as PARTS in before post, “Structures.” Here’s the listing for OBJPART:
// objpart.cpp // widget part as an object #include <iostream> using namespace std; //////////////////////////////////////////////////////////////// class part //define class { private: int modelnumber; //ID number of widget int partnumber; //ID number of widget part float cost; //cost of part public: void setpart(int mn, int pn, float c) //set data { modelnumber = mn; partnumber = pn; cost = c; } void showpart() //display data { cout << “Model “ << modelnumber; cout << “, part “ << partnumber; cout << “, costs $” << cost << endl; } }; //////////////////////////////////////////////////////////////// int main() { part part1; //define object // of class part part1.setpart(6244, 373, 217.55F); //call member function part1.showpart(); //call member function return 0; }
This program features the class part. Instead of one data item, as SMALLOBJ had, this class has three: modelnumber, partnumber, and cost. A single member function, setpart(), supplies values to all three data items at once. Another function, showpart(), displays the values stored in all three items.
In this example only one object of type part is created: part1. The member function setpart() sets the three data items in this part to the values 6244, 373, and 217.55. The member function showpart() then displays these values. Here’s the output: Model 6244, part 373, costs $217.55
This is a somewhat more realistic example than SMALLOBJ. If you were designing an inventory program you might actually want to create a class something like part. It’s an example of a C++ object representing a physical object in the real world a widget part.
Circles as Objects
In our next example we’ll examine an object used to represent a circle: the kind of circle displayed on your computer screen. An image isn’t quite as tangible an object as a widget part, which you can presumably hold in your hand, but you can certainly see such a circle when your program runs.
Our example is an object-oriented version of the CIRCSTRC program from before. (As in that program, you’ll need to add the appropriate Console Graphics Lite files to your project. These files can be downloaded from the publisher’s Web site as described in the Introduction. Appendix E, “Console Graphics Lite,” describes these files. See also the appendix for your particular compiler.) The program creates three circles with various characteristics and displays them. Here’s the listing for CIRCLES:
// circles.cpp // circles as graphics objects #include “msoftcon.h” // for graphics functions //////////////////////////////////////////////////////////////// class circle //graphics circle { protected: int xCo, yCo; //coordinates of center int radius; color fillcolor; //color fstyle fillstyle; //fill pattern public: //sets circle attributes void set(int x, int y, int r, color fc, fstyle fs) { xCo = x; yCo = y; radius = r; fillcolor = fc; fillstyle = fs; } void draw() //draws the circle { set_color(fillcolor); //set color set_fill_style(fillstyle); //set fill draw_circle(xCo, yCo, radius); //draw solid circle } }; //////////////////////////////////////////////////////////////// int main() { init_graphics(); //initialize graphics system circle c1; //create circles circle c2; circle c3; //set circle attributes c1.set(15, 7, 5, cBLUE, X_FILL); c2.set(41, 12, 7, cRED, O_FILL); c3.set(65, 18, 4, cGREEN, MEDIUM_FILL); c1.draw(); //draw circles c2.draw(); c3.draw(); set_cursor_pos(1, 25); //lower left corner return 0; }
The output of this program is the same as that of the CIRCSTRC program, shown in Figure in that chapter. You may find it interesting to compare the two programs. In CIRCLES, each circle is represented as a C++ Objects as physical objects rather than as a combination of a structure variable and an unrelated circ_draw() function, as it was in CIRCSTRC. Notice in CIRCLES how everything connected with a circle attributes and functions is brought together in the class definition.
In CIRCLES, besides the draw() function, the circle class also requires the five-argument set() function to set its attributes. We’ll see later that it’s advantageous to dispense with this function and use a constructor instead.
Read More Topics |
Abstract class in C++ |
Relational operators in C++ |
Characteristics of OOP |
C++ Inheritance |