Skip to content

Commit 80812ca

Browse files
committed
upload
1 parent aaff0ad commit 80812ca

30 files changed

+672
-2
lines changed
368 KB
Binary file not shown.
Binary file not shown.
379 KB
Binary file not shown.
399 KB
Binary file not shown.

3.C++程序设计1/week2/005const.pdf

252 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

3.C++程序设计1/week2/README.md

+111-2
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ r = 200; // error
216216

217217

218218

219+
219220
## 内联函数
220221

221222
函数调用是有时间开销的(参数入栈,返回地址)。如果函数本身只有几条语 句,执行非常快,而且函数被反复执行很多次,相比 之下调用函数所产生的这个开销就会显得比较大。
@@ -234,7 +235,7 @@ inline int Max(int a, int b);
234235
235236
## 函数重载
236237
237-
一个或多个函数,名字相同,然而参数个数或参数类型不相同,这叫做函数的重载。 (返回值不能不同!!
238+
一个或多个函数,名字相同,然而参数个数或参数类型不相同,这叫做函数的重载。 (参数相同,返回值不同不算重载
238239
239240
```c++
240241
int Max(double f1, double f2);
@@ -244,4 +245,112 @@ int Max(int n1, int n2, int n3);
244245

245246
函数重载使得函数命名变得简单。
246247

247-
编译器根据调用语句的中的实参的个数和类型判断应该调用哪个函数。如果有歧义则会报错,如`Max(3, 2.4)`
248+
编译器根据调用语句的中的实参的个数和类型判断应该调用哪个函数。如果有歧义则会报错,如`Max(3, 2.4)`
249+
250+
251+
252+
## 函数的缺省参数
253+
254+
缺省参数放最右边
255+
256+
提高程序的可扩充性
257+
258+
259+
260+
## 面向对象程序设计方法
261+
262+
结构化程序设计:复杂问题 -> 模块化 -> 若干子问题
263+
264+
面向对象的一些思想,阅读PPT
265+
266+
267+
268+
## 面向对象语言的发展历程
269+
270+
第一个面向对象语言:Simula 67
271+
272+
看PPT
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+
310+
```c++
311+
对象名.成员名
312+
```
313+
314+
```
315+
指针->成员名
316+
```
317+
318+
```c++
319+
引用名.成员名
320+
```
321+
322+
### 类的成员函数的另一种写法
323+
324+
```c++
325+
type class::function(parameter)
326+
{
327+
...
328+
}
329+
```
330+
331+
332+
333+
## 类成员的可访问范围
334+
335+
### 关键字
336+
337+
- `private`:私有成员,只能在成员函数内访问
338+
- `public`:公有成员,任何地方都可以访问
339+
- `protected`:保护成员
340+
341+
缺省为私有成员
342+
343+
### 对象成员的访问权限
344+
345+
类的成员函数内部,可以访问:
346+
347+
- 当前对象的全部属性和函数
348+
- 同类其他对象的全部属性和函数
349+
350+
类的成员函数以外的地方:
351+
352+
- 只能访问该类对象的公有成员
353+
354+
设置私有成员的目的:
355+
356+
- 强制对成员变量的访问要经过成员函数
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
## 编程作业: 编程作业—C++初探
2+
3+
## 简单的学生信息处理程序实现
4+
5+
[来源: POJ](http://cxsjsxmooc.openjudge.cn/test/C/) (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)
6+
7+
**注意: 总时间限制: 1000ms 内存限制: 65536kB**
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+
Tom,18,7817,80,80,90,70
33+
```
34+
35+
### 样例输出
36+
37+
```
38+
Tom,18,7817,80
39+
```
40+
### 解题思路
41+
42+
没什么难的,学到了一个新的库函数
43+
44+
`istream& getline (istream& is, string& str, char delim);`
45+
46+
可以自己设定输入字符串的分隔符,挺方便的
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
int a, b;
7+
cin >> a >> b;
8+
cout << a + b << endl;
9+
return 0;
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
简单的学生信息处理程序实现
3+
来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)
4+
5+
注意: 总时间限制: 1000ms 内存限制: 65536kB
6+
7+
描述
8+
在一个学生信息处理程序中,要求实现一个代表学生的类,并且所有成员变量都应该是私有的。
9+
10+
(注:评测系统无法自动判断变量是否私有。我们会在结束之后统一对作业进行检查,请同学们严格按照题目要求完成,否则可能会影响作业成绩。)
11+
12+
输入
13+
姓名,年龄,学号,第一学年平均成绩,第二学年平均成绩,第三学年平均成绩,第四学年平均成绩。
14+
15+
其中姓名、学号为字符串,不含空格和逗号;年龄为正整数;成绩为非负整数。
16+
17+
各部分内容之间均用单个英文逗号","隔开,无多余空格。
18+
19+
输出
20+
一行,按顺序输出:姓名,年龄,学号,四年平均成绩(向下取整)。
21+
22+
各部分内容之间均用单个英文逗号","隔开,无多余空格。
23+
24+
样例输入
25+
Tom,18,7817,80,80,90,70
26+
27+
样例输出
28+
Tom,18,7817,80
29+
*/
30+
31+
#include <iostream>
32+
#include <string>
33+
using namespace std;
34+
35+
class Student
36+
{
37+
private:
38+
string name;
39+
string id;
40+
int age;
41+
int score1, score2, score3, score4;
42+
int average;
43+
public:
44+
void init();
45+
void print();
46+
};
47+
48+
void Student::init()
49+
{
50+
char comma;
51+
getline(cin, name, ',');
52+
cin >> age >> comma;
53+
getline(cin, id, ',');
54+
cin >> score1 >> comma >> score2 >> comma >> score3 >> comma >> score4;
55+
average = (score1 + score2 + score3 + score4) / 4;
56+
}
57+
58+
void Student::print()
59+
{
60+
cout << name << ',' << age << ',' << id << ',' << average << endl;
61+
}
62+
63+
int main()
64+
{
65+
Student stu1;
66+
stu1.init();
67+
stu1.print();
68+
return 0;
69+
}
Binary file not shown.
Binary file not shown.
416 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
271 KB
Binary file not shown.
222 KB
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)