C++书上例题分析总结

algorain

这是考试前总结的面向对象程序设计书上的例题,以及特殊函数和自己的分析;这是考试前总结的面向对象程序设计书上的例题,以及特殊函数和自己的分析;这是考试前总结的面向对象程序设计书上的例题,以及特殊函数和自己的分析

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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
//函数重载 1.6

#include "iostream"
using namespace std;
int max(int a,int b){
return a>b?a:b;
}
float max(float a,float b){
return a>b?a:b;
}
long max(long a,long b){
return a>b?a:b;
}
int main(){
int a,b;
cin>>a>>b;
cout<<max(a,b)<<endl;
return 0;
}
//函数模板 1.8
//使用模板时不能只使用两个参数,会报错,如
//[Error] call of overloaded 'max(int&, int&)' is ambiguous
#include "iostream"
using namespace std;
template <typename T>
T max(T a,T b,T c){
if(b>a) a=b;
if(c>a) a=c;
return a;
}
int main(){
int i1=8,i2 = 5,i3 = 6,i;
double d1 = 56.9,d2 = 90.765,d3 = 43.1 ,d;
long g1 = 678435,g2 = -1555,g3 = 784354,g;
i = max(i1,i2,i3);
d = max(d1,d2,d3);
g = max(g1,g2,g3);
cout<<i<<endl;
cout<<d<<endl;
cout<<g<<endl;
return 0;
}
//使用变量的引用来传递值进行转换,也可以传递地址,使用指针获取地址再使用
//变量的引用 1.12
#include "iostream"
using namespace std;
int swap(int &a,int &b){
return a>b?a:b;
}
int main(){

int n,m;
cin>>n>>m;
cout<<swap(n,m);
return 0;
}

//一个简单的类 2.4
#include "iostream"
#include "algorithm"
using namespace std;
class Array_max{
public:
void set_value();
void max_value();
void show_value();
private:
int array[100];
int maxn;
};
void Array_max::set_value(){
for(int i=0;i<10;i++){
cin>>array[i];
}
}
void Array_max::max_value(){
sort(array,10);
maxn = array[9];
}
void Array_max::show_value(){
cout<<"max="<<maxn<<endl;
}
int main(){
Array_max arr;
arr.set_value();
arr.max_value();
arr.show_value();
return 0;
}
简单的构造函数 3.1
#include "iostream"
using namespace std;
class Time{
public:
Time(){ hour = minute = sec = 0;}
void set_time();
void show_time();
private:
int hour;
int minute;
int sec;
};
void Time::set_time(){
cin>>hour>>minute>>sec;
}
void Time::show_time(){
cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
int main(){
Time t;
t.show_time();
return 0;
}
类外定义有参的构造函数 3.2
#include "iostream"
using namespace std;
class Time{
public:
Time(int,int,int);
void set_time();
void show_time();
private:
int hour;
int minute;
int sec;
};
void Time::set_time(){
cin>>hour>>minute>>sec;
}
void Time::Time(int h,int m,int s){
hour = h;
minute = m;
sec = s;
}
void Time::show_time(){
cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
int main(){
Time t;
t.show_time();
return 0;
}

使用默认参数的构造函数 3.4
#include "iostream"
using namespace std;
class Box{
public:
Box(int h=0,int w=10,int len=10);
int volume();
private:
int height;
int width;
int length;
};
Box::Box(int h,int w,int len){
height = h;
weigth = w;
length = len;
}
int Box::volume(){
return height*weigth*length;
}
int main(){
Box box1;
cout<<box1.volume()<<endl;
Box box2(15);
cout<<box2.volume()<<endl;
Box box3(15,30);
cout<<box3.volume()<<endl;
Box box4(15,30,20);
cout<<box4.volume()<<endl;
return 0;
}
析构函数 3.5
#include "iostream"
#include "string"
using namespace std;
class Student{
public:
Student(int n,string nam,char s){
num = n;
name = nam;
sex = s;
cout<<"Constructor called."<<endl;
}
~Student(){
cout<<"Destructor called."<<endl;
}
void dispaly(){
cout<<"num: "<<num<<endl;
cout<<"name: "<<name<<endl;
cout<<"sex: "<<sex<<endl;
}
private:
int num;
string name;
char sex;
};
int main(){

Student stud1(10010,"Wang_li",'f');
stud1.dispaly();
Student stud2(10011,"Zhang_fan",'m');
stud2.dispaly();
return 0;
}

对象指针:
Time *pt; 定义指针
Time t1; 定义对象
pt = &t1; 将t1的起始地址给指针pt
指针对成员的引用
*pt
(*pt).hour
pt->hour
普通函数可以在类内声明为友元函数
friend void display(Time&);
其他类里的成员函数也可以在类内声明为友元函数
friend void Time::display(Date&)
注意括号里的内容,友元函数可以访问私有成员

最开始的复数相加,重载+运算符 4.2
其他符号的重载都相似,会一个就行
#include "iostream"
using namespace std;
class Complex{
public:
Complex(){real = 0;imag = 0;}
Complex(double r,double i){real = r;imag = i;}
Complex operator +(Complex &c2);
void display();
private:
double real;
double imag;
};
Complex Complex::operator +(Complex &c2){
Complex c;
c.real = real + c2.real;
c.imag = imag + c2.imag;
return c;
}
void Complex::display(){
cout<<real<<" : "<<imag<<endl;
}
int main(){
Complex c1(3,4),c2(5,-10),c3;
c3 = c2 + c1;
c3.display();
return 0;
}

重载流插入运算符和流提取运算符
istream & operator >>(istream &,自定义类 &);
ostream & operator <<(ostream &,自定义类 &);
重载流插入运算符 《
#include "iostream"
using namespace std;
class Complex{
public:
Complex() {real = 0;imag = 0;}
Complex(double r,double i){real = r;imag = i;}
Complex operator +(Complex&c2);
friend ostream& operator <<(ostream,Complex&);
private:
double real;
double imag;
};
Complex Complex::operator +(Complex&c2){
return Complex(real+c2.real,imag+c2.imag);
}
ostream & operator <<(ostream & output,Complex&c){
output<<"("<<c.real<<"+"<<c.imag<<"i)"<<endl;
return output;
}
int main(){
Complex c1(2,4),c2(6,10),c3;
c3 = c2 + c1;
cout<<c3<<endl;
return 0;
}


重载流提取运算符
#include "iostream"
using namespace std;
class Complex{
public:
friend ostream& operator <<(ostream&,Complex&);
friend istream& operator >>(istream&,Complex&);
private:
double real;
double imag;
};
ostream& operator <<(ostream& output,Complex& c){
output<<c.real<<c.imag<<endl;
return output;
}
istream& operator >>(istream& input,Complex& c){
input>>c.real>>c.imag;
return input;
}
int main(){
Complex c1,c2;
cin>>c1>>c2;
cout<<c1<<endl;
cout<<c2<<endl;
return 0;
}
  • Title: C++书上例题分析总结
  • Author: algorain
  • Created at: 2017-02-16 12:39:03
  • Updated at: 2023-05-14 21:39:50
  • Link: http://www.rain1024.com/2017/02/16/c-article60/
  • License: This work is licensed under CC BY-NC-SA 4.0.
 Comments