-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo8.cpp
85 lines (77 loc) · 1.67 KB
/
demo8.cpp
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
#include <iostream>
#include <string>
using namespace std;
class Person
{
public:
Person(const string &nm): name(nm){}
private:
string name;
int age;
};
class Sales_item
{
public:
Sales_item(const string &book, int units, double price)
: isbn(book),units_sold(units),revenue(units * price)
{}
explicit Sales_item(const string &book=""): isbn(book),units_sold(0),revenue(0.0){}
explicit Sales_item(istream &is) { is >> *this; }
Sales_item(int units, double price)
{
this->units_sold = units;
this->revenue = units * price;
}
//Sales_item():units_sold(0),revenue(0.0){}
friend istream& operator >> (istream&, Sales_item&);
private:
string isbn;
unsigned units_sold;
double revenue;
};
inline istream& operator >> (istream& in, Sales_item& s)
{
double price;
in >> s.isbn >> s.units_sold >> price;
if(in)
s.revenue = s.units_sold * price;
else
s = Sales_item();
return in;
}
class Dog
{
public:
// 1. 引用类型的成员必须用初始化列表进行初始化。
// 2. 没有默认构造函数的类类型。
// 3. 类中的const 成员变量必须用初始化列表进行初始化, 自带的类型可以不在列表中。
Dog():legs(4)
{}
private:
// 初始化列表初始化成员变量的顺序 由下面定义成员变量的顺序决定。
string name;
const int legs;
};
class Data // Data v = {9, "hello"} 条件:1. 没有构造函数. 2. 数据成员全部为public.
{
public:
int ival;
char *ptr;
};
class Eata
{
public:
Eata(int i, char *p): ival(i), ptr(p){}
private:
int ival;
char *ptr;
};
int main()
{
Person p("zhangfei");
Sales_item item1;
Sales_item item2("0-201-82470-1");
Sales_item *ps = new Sales_item();
delete ps;
return 0;
}