-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwork3.cpp
More file actions
86 lines (73 loc) · 1.57 KB
/
Copy pathwork3.cpp
File metadata and controls
86 lines (73 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <cstring>
using namespace std;
class Book
{
private:
char *name; //**书名
char *author; //**作者
int sale; //**销售量
public:
Book(); //**无参构造函数
Book(const char *, const char *, int); //**有参构造函数
Book(const Book &); //**拷贝构造函数
void print() const; //**显示数据
~Book(); //**析构函数
};
Book::Book()
{
name = new char[10];
strcpy(name,"No name");
author = new char[10];
strcpy(author,"No author");
sale=0;
}
void Book::print() const
{
cout << "Name: " << name << "\tAuthor: " << author << "\tSale: " << sale << endl;
}
Book::Book(const char *n, const char *a, int s)
{
name = new char[strlen(n) + 1];
strcpy(name, n);
author = new char[strlen(a) + 1];
strcpy(author, a);
sale = s;
}
Book::Book(const Book&other)
{
name = new char[strlen(other.name) + 1];
strcpy(name, other.name);
author = new char[strlen(other.author) + 1];
strcpy(author, other.author);
sale = other.sale;
}
Book::~Book()
{
delete[] name;
delete[] author;
}
int main()
{
char name[101], author[101];
int sale;
cin.getline(name, 101);
cin.getline(author, 101);
cin >> sale;
if (strcmp(name, "-1") == 0 && strcmp(author, "-1") == 0 && sale == -1)
{
Book bk;
bk.print();
}
else if (strcmp(name, "0") == 0 && strcmp(author, "0") == 0 && sale == 0)
{
Book bk1;
Book bk2(bk1);
bk2.print();
}
else
{
Book bk(name, author, sale);
bk.print();
}
}