Untitled
unknown
plain_text
a year ago
1.5 kB
7
Indexable
[5/31, 10:50 AM] +91 78010 25352: #ifndef POINT_H #define POINT_H class Point { public: int x, y; Point(); Point(int x, int y); void display() const; }; #endif // POINT_H [5/31, 10:50 AM] +91 78010 25352: #include "Point.h" #include <iostream> // Default constructor Point::Point() : x(0), y(0) {} // Parameterized constructor Point::Point(int x, int y) : x(x), y(y) {} // Display function void Point::display() const { std::cout << "(" << x << ", " << y << ")" << std::endl; } [5/31, 10:50 AM] +91 78010 25352: #include <iostream> #include <fstream> #include <vector> #include "Point.h" int main() { // Create a vector to hold multiple Point objects std::vector<Point> points; // Add some Point objects to the vector points.emplace_back(1, 2); points.emplace_back(3, 4); points.emplace_back(5, 6); points.emplace_back(7, 8); // Open a file in binary mode to write the points std::ofstream outFile("Points.dat", std::ios::binary); if (!outFile) { std::cerr << "Cannot open file!" << std::endl; return 1; } // Write the size of the vector first size_t size = points.size(); outFile.write(reinterpret_cast<char*>(&size), sizeof(size)); // Write each Point object to the file for (const auto& point : points) { outFile.write(reinterpret_cast<const char*>(&point), sizeof(Point)); } // Close the file outFile.close(); std::cout << "Points have been written to Points.dat" << std::endl; return 0; }
Editor is loading...
Leave a Comment