1 #include <iostream>
2 #include <fstream>
3 using namespace std;
4
5 void file_wr(){
6 char data[100];
7 //打开文件
8 ofstream outfile;
9 outfile.open("demo.dat");
10 cout<<"write to the file"<<endl;
11
12 cout<<"put in some world one"<<endl;
13 cin.getline(data,100);
14
15 //向文件输入用户输入值
16 outfile<<data<<endl;
17
18 cout<<"put in some world two"<<endl;
19 cin>>data;
20 cin.ignore();
21
22 //向文件输入用户输入值
23 outfile<<data<<endl;
24 outfile.close();//关闭
25
26 //读模式打开
27 ifstream infile;
28 infile.open("demo.dat");
29
30 cout<<"read from the file"<<endl;
31
32 infile>>data;
33 cout<<data<<endl;
34
35 infile>>data;
36 cout<<data<<endl;
37
38 infile.close();
39 // return 0;
40 }
41
42 int main(){
43 file_wr();
44 return 0;
45 }
46
47 #if 0
48 class Adder{
49 public:
50 //构造函数
51 Adder(int i=0){
52 total=i;
53 }
54
55 void addNum(int number){
56 total +=number;//在此处累加
57 }
58 int getTotal(){
59 return total;
60 }
61 private:
62 //对外隐藏的数据
63 int total;
64
65 };
66
67
68 //基类
69 class Box{
70 public:
71 //纯虚函数
72 virtual int getArea()=0;
73
74 void setBreadth(int b){
75 breadth=b;
76 }
77 void setHeight(int h){
78 height=h;
79 }
80 void setLength(int l){
81 length=l;
82 }
83 protected:
84 int height;
85 int length;
86 int breadth;
87 };
88
89 //派生类
90 class Renct:public Box{
91 public:
92 //与纯虚函数同名
93 int getArea(){
94 return (breadth * height *length);
95 }
96 };
97
98 class Tenct:public Box{
99 public:
100 //与纯虚函数同名
101 int getArea(){
102 return (height * length)/2;
103 }
104 };
105
106 void open(const char *filename,ios::openmode mode){
107
108 }
109
110 int main(void)
111 {
112 #if 0
113 Adder b;
114 b.addNum(10);
115 b.addNum(20);
116 b.addNum(50);
117 b.addNum(120);
118
119 cout<<"Total:"<<b.getTotal()<<endl;
120 #endif
121
122 Renct acc;
123 Tenct bcc;
124
125 acc.setBreadth(5);
126 acc.setHeight(4);
127 acc.setLength(3);
128
129 bcc.setHeight(2);
130 bcc.setLength(3);
131
132 cout<<"Tenct:"<<acc.getArea()<<endl;
133 cout<<"Teb:"<<bcc.getArea()<<endl;
134
135 return 0;
136 }
137 #endif