OOP HomeWork
This commit is contained in:
27
oop_hw2/oop_hw2_2/Q2.cpp
Normal file
27
oop_hw2/oop_hw2_2/Q2.cpp
Normal 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
BIN
oop_hw2/oop_hw2_2/Q2.exe
Normal file
Binary file not shown.
BIN
oop_hw2/oop_hw2_2/测试截图2.png
Normal file
BIN
oop_hw2/oop_hw2_2/测试截图2.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 104 KiB |
Reference in New Issue
Block a user