721
edits
No edit summary |
No edit summary |
||
Line 28: | Line 28: | ||
cout << "Thats the letter 'A': " << letter_A << endl; | cout << "Thats the letter 'A': " << letter_A << endl; | ||
cout << text; | cout << text; | ||
return 0; | |||
} | |||
The code about classes and pointers: | |||
#include <iostream> | |||
using namespace std; | |||
class Point2D { | |||
public: | |||
Point2D(float newX, float newY){ | |||
_x = newX; | |||
_y = newY; | |||
}; | |||
float getX() { | |||
return _x; | |||
}; | |||
float getY() { | |||
return _y; | |||
}; | |||
void print() { | |||
cout << "(" << getX() << ", " << getY()<< ")" << endl; | |||
} | |||
private: | |||
float _x; | |||
float _y; | |||
}; | |||
int main() | |||
{ | |||
Point2D point1(5,1); | |||
Point2D point2(10,100); | |||
Point2D point3(27,2); | |||
point1.print(); | |||
point2.print(); | |||
int integerArray[5] = { 0,1,2,3,4 }; | |||
cout << "first element: " << integerArray[0] << endl; | |||
Point2D pointArray[3] = {point1, point2, point3 }; | |||
pointArray[2].print(); | |||
// pointer version of the code | |||
Point2D* p2d1_ptr = new Point2D(123, 234); | |||
Point2D* p2d2_ptr = new Point2D(123, 234); | |||
Point2D* p2d3_ptr = new Point2D(123, 234); | |||
(*p2d1_ptr).print(); | |||
p2d1_ptr->print(); | |||
Point2D* point_ptr_Array[3] = {p2d1_ptr, p2d2_ptr, p2d3_ptr }; | |||
cout << "pointer address: " << point_ptr_Array[0] << "x component of the point at that address: " << point_ptr_Array[0]->getX() << endl; | |||
return 0; | return 0; | ||
} | } |