Thursday, May 30, 2019

different type of object

Header File: Point.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/* The Point class Header file (Point.h) */
#ifndef POINT_H
#define POINT_H
 
class Point {
private:
   int x, y;  // Private data members
 
public:
   Point(int x = 0, int y = 0); // Constructor with default arguments
   int getX() const;  // Getter
   void setX(int x);  // Setter
   int getY() const;
   void setY(int y);
   void setXY(int x, int y);
   void print() const;
};
 
#endif
Implementation File: Point.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/* The Point class Implementation file (Point.cpp) */
#include "Point.h"
#include <iostream>
using namespace std;
 
// Constructor - The default values are specified in the declaration
Point::Point(int x, int y) : x(x), y(y) { }
 
// Getters
int Point::getX() const { return x; }
int Point::getY() const { return y; }
 
// Setters
void Point::setX(int x) { this->x = x; }
void Point::setY(int y) { this->y = y; }
 
// Public Functions
void Point::setXY(int x, int y) { this->x = x; this->y = y; }
 
void Point::print() const {
   cout << "Point @ (" << x << "," << y << ")";
}
A Test Driver: TestPoint.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* A test driver program (TestPoint.cpp) */
#include "Point.h"
#include <iostream>
using namespace std;
 
int main() {
   // Instances (Objects)
   Point p1;       // Invoke default constructor
                   // OR Point p1 = Point(); NOT Point p1();
   Point p2(2, 2); // Invoke constructor
                   // OR Point p2 = Point(2, 2);
   p1.print();     // Point @ (0,0)
   cout << endl;
   p2.print();     // Point @ (2,2)
   cout << endl;
 
   // Object Pointers with dynamic allocation
   Point * ptrP3, * ptrP4; // Declare two Point pointers
   ptrP3 = new Point();    // Dynamically allocate storage via new
                           // with default constructor
   ptrP4 = new Point(4, 4);
   ptrP3->print();  // Point @ (0,0)
                    // prtPoint1->print() is the same as (*ptrP3).print()
   cout << endl;
   ptrP4->print();  // Point @ (4,4)
   cout << endl;
 
   delete ptrP3;    // Remove storage via delete
   delete ptrP4;
 
   // Object Reference (Alias)
   Point & p5 = p2; // Reference (alias) to an existing object
   p5.print();      // Point @ (2,2)
   cout << endl;
 
   /********************
    * ARRAYS           *
    ********************/
 
   // Array of Objects - Static Memory Allocation
   Point ptsArray1[2];     // Array of Point objects
                           // Use default constructor for all elements of the array
   ptsArray1[0].print();   // Point @ (0,0)
   cout << endl;
   ptsArray1[1].setXY(11, 11);
   (ptsArray1 + 1)->print(); // Point @ (11,11)
                             // same as ptsArray1[1].print()
   cout << endl;
 
   Point ptsArray2[3] = {Point(21, 21), Point(22, 22), Point()};
                           // Initialize array elements via constructor
   ptsArray2[0].print();   // Point @ (21,21)
   cout << endl;
   (ptsArray2 + 2)->print(); // Point @ (0,0)
                             // same as ptsArray2[2].print()
   cout << endl;
 
   // Array of Object Pointers - Need to allocate elements dynamically
   Point * ptrPtsArray3 = new Point[2];
   ptrPtsArray3[0].setXY(31, 31);
   ptrPtsArray3->print();  // Point @ (31,31)
                           // same as ptrPtsArray3[0].print()
   cout << endl;
   (ptrPtsArray3 + 1)->setXY(32, 32); // same as ptrPtsArray3[1].setXY(32, 32)
   ptrPtsArray3[1].print();           // Point @ (32,32)
   cout << endl;
 
   delete[] ptrPtsArray3; // Free storage
 
   // C++ does not support Array of References
// Point & pts[2] = {p1, p2};  // error: declaration of 'pts' as array of references
}

No comments:

Post a Comment