OOP HomeWork

This commit is contained in:
e2hang
2025-08-11 00:01:30 +08:00
commit e8a5ca2363
119 changed files with 3187 additions and 0 deletions

27
oop_hw2/oop_hw2_2/Q2.cpp Normal file
View File

@@ -0,0 +1,27 @@
#include <iostream>
using namespace std;
class Book {
private:
int totalPages;
int pagesRead;
public:
Book(int total) : totalPages(total), pagesRead(0) {}
void Read(int pages) { pagesRead += pages; if (pagesRead > totalPages) pagesRead = totalPages; }
int GetTotalPages() const { return totalPages; }
int GetPagesRead() const { return pagesRead; }
int GetPagesLeft() const { return totalPages - pagesRead; }
};
void Test() {
Book b(100);
b.Read(30);
std::cout << "Read: " << b.GetPagesRead() << ", Left: " << b.GetPagesLeft() << std::endl;
b.Read(80);
std::cout << "Read: " << b.GetPagesRead() << ", Left: " << b.GetPagesLeft() << std::endl;
}
int main(){
Test();
return 0;
}

BIN
oop_hw2/oop_hw2_2/Q2.exe Normal file

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 104 KiB