13年题。

This commit is contained in:
unlockable
2023-06-02 00:03:58 +08:00
parent 955977fef2
commit 9cecf352c2
11 changed files with 401 additions and 0 deletions

56
OOP/13Exam/3.5.cpp Normal file
View File

@@ -0,0 +1,56 @@
#include <iostream>
#include <string>
using namespace std;
class Book {
char *title;
char *author;
int numsold;
public:
Book() {
}
Book(const char *str1, const char *str2, const int num) {
int len = strlen(str1);
title = new char[len + 1];
strcpy(title, str1);
len = strlen(str2);
author = new char[len + 1];
strcpy(author, str2);
numsold = num;
}
void setbook(const char *str1, const char *str2, const int num) {
int len = strlen(str1);
title = new char[len + 1];
strcpy(title, str1);
len = strlen(str2);
author = new char[len + 1];
strcpy(author, str2);
numsold = num;
}
~Book() {
delete title;
delete author;
}
void print(ostream &output) {
output << "book:" << title << endl;
output << "author:" << author << endl;
output << "total:" << numsold << endl;
}
};
int main() {
Book obj1("Physics", "LiFu", 200), obj2;
obj1.print(cout);
obj2.setbook("C++ Programming", "SunJuason", 300);
obj2.print(cout);
return 0;
}
/*
book:Physics
author:LiFu
total:200
book:C++ Programming
author:SunJuason
total:300
*/