C++函数与类的深入解析
立即解锁
发布时间: 2025-08-19 01:36:29 阅读量: 24 订阅数: 26 AIGC 


C++面向对象编程入门与实践
# C++ 函数与类的深入解析
## 一、函数相关知识
### 1.1 函数的基本概念与作用
函数在编程中扮演着至关重要的角色。它的一个重要作用是帮助组织程序成为概念单元,使得程序结构更加清晰,易于维护和理解。
下面是一些关于函数的常见问题及解答:
|问题|答案|
| ---- | ---- |
|函数最重要的单一角色是什么?|帮助组织程序成为概念单元(选项 d)|
|函数本身被称为什么?|函数定义(function definition)|
|编写一个名为 foo() 的函数,用于显示单词 foo。|
```cpp
#include <iostream>
void foo() {
std::cout << "foo" << std::endl;
}
```
|对函数的单语句描述被称为什么?|函数定义(function definition)或原型(prototype)|
|执行函数工作的语句构成了什么?|函数体(function body)|
|调用函数的程序语句是什么?|函数调用(function call)|
|函数定义的第一行被称为什么?|函数头(function header)|
|函数参数是什么?|调用程序发送给函数的值(选项 c)|
|当参数按值传递时,函数是否处理调用程序中的原始参数?|错误,函数处理的是原始参数的副本|
|在函数声明中使用参数名的目的是什么?|提高代码的可读性,表明参数的用途|
|以下哪些可以合法地传递给函数?|常量、变量、结构(选项 a、b、c)|
|函数声明中空括号的意义是什么?|表示函数不接受任何参数|
|一个函数可以返回多少个值?|通常只能返回一个值,但可以通过返回结构体或引用等方式返回多个值|
|当函数返回一个值时,整个函数调用是否可以出现在等号右侧并赋值给另一个变量?|正确|
|函数的返回类型在哪里指定?|在函数定义的函数头中指定|
|不返回任何值的函数的返回类型是什么?|void|
### 1.2 函数示例代码
#### 1.2.1 times2 函数及调用示例
```cpp
#include <iostream>
int times2(int a) {
return (a * 2);
}
int main() {
int num = 5;
int result = times2(num);
std::cout << "The result of times2 is: " << result << std::endl;
return 0;
}
```
#### 1.2.2 按引用传递参数
当参数按引用传递时,函数访问调用程序中参数的原始值。例如,下面是一个按引用传递参数的示例:
```cpp
#include <iostream>
void swap(int& a, int& b) {
int temp = a;
a = b;
b = temp;
}
int main() {
int x = 5, y = 10;
std::cout << "Before swap: x = " << x << ", y = " << y << std::endl;
swap(x, y);
std::cout << "After swap: x = " << x << ", y = " << y << std::endl;
return 0;
}
```
### 1.3 函数的其他特性
#### 1.3.1 函数重载
重载函数是一组具有相同名称但参数不同的函数。例如,下面是两个重载的 bar() 函数声明:
```cpp
int bar(char c);
int bar(char c1, char c2);
```
#### 1.3.2 内联函数
一般来说,内联函数比普通函数执行速度快,但需要更多的内存。下面是一个内联函数 foobar() 的声明:
```cpp
inline float foobar(float x) {
return x * 2;
}
```
#### 1.3.3 默认参数
默认参数具有一个值,该值可以由调用程序提供,也可以使用函数中指定的默认值。例如,下面是一个带有默认参数的函数声明:
```cpp
char blyth(int a, float b = 3.14159);
```
### 1.4 变量的作用域和存储类
变量的作用域和存储类涉及变量的可见性和生命周期。下面是一些相关问题及解答:
|问题|答案|
| ---- | ---- |
|哪些函数可以访问与它们在同一文件中的全局变量?|同一文件中的所有函数都可以访问全局变量|
|哪些函数可以访问局部变量?|定义该局部变量的函数内部可以访问|
|静态局部变量的用途是什么?|在函数不执行时保留值(选项 d)|
|当函数通过引用返回值时,可以在什么不寻常的地方使用函数调用?|可以在赋值语句的左侧使用|
## 二、函数练习
### 2.1 计算圆面积的函数
```cpp
#include <iostream>
const float PI = 3.14159;
float circarea(float radius) {
return PI * radius * radius;
}
int main() {
float r;
std::cout << "Enter the radius of the circle: ";
std::cin >> r;
float area = circarea(r);
std::cout << "The area of the circle is: " << area << std::endl;
return 0;
}
```
### 2.2 幂函数
```cpp
#include <iostream>
double power(double n, int p = 2) {
double result = 1;
for (int i = 0; i < p; ++i) {
result *= n;
}
return result;
}
int main() {
double num;
int powerValue;
std::cout << "Enter a number: ";
std::cin >> num;
std::cout << "Enter the power (leave blank for default of 2): ";
if (!(std::cin >> powerValue)) {
std::cin.clear();
std::cin.ignore(10000, '\n');
powerValue = 2;
}
double result = power(num, powerValue);
std::cout << num << " raised to the power of " << powerValue << " is: " << result << std::endl;
return 0;
}
```
### 2.3 将较小数置为 0 的函数
```cpp
#include <iostream>
void zeroSmaller(int& a, int& b) {
if (a < b) {
a = 0;
} else {
b = 0;
}
}
int main() {
int num1, num2;
std::cout << "Enter two integers: ";
std::cin >> num1 >> num2;
zeroSmaller(num1, num2);
std::cout << "After zeroing the smaller number: " << num1 << " " << num2 << std::endl;
return 0;
}
```
### 2.4 比较两个距离值并返回较大值的函数
```cpp
#include <iostream>
struct Distance {
int feet;
float inches;
};
Distance larger(Distance d1, Distance d2) {
float total1 = d1.feet * 12 + d1.inches;
float total2 = d2.feet * 12 + d2.inches;
return (total1 > total2) ? d1 : d2;
}
int main() {
Distance dist1, dist2, result;
std::cout << "Enter the first distance (feet inches): ";
std::cin >> dist1.feet >> dist1.inches;
std::cout << "Enter the second distance (feet inches): ";
std::cin >> dist2.feet >> dist2.inches;
result = larger(dist1, dist2);
std::cout << "The larger distance is: " << result.feet << " feet " << result.inches << " inches" << std::endl;
return 0;
}
```
### 2.5 将时分秒转换为秒的函数
```cpp
#include <iostream>
long hms_to_secs(int hours, int minutes, int seconds) {
return hours * 3600 + minutes * 60 + seconds;
}
int main() {
int h, m, s;
char colon;
while (true) {
std::cout << "Enter time in format hh:mm:ss (or any non - numeric to exit): ";
if (!(std::cin >> h >> colon >> m >> colon >> s)) {
break;
}
long totalSeconds = hms_to_secs(h, m, s);
std::cout << "Total seconds: " << totalSeconds << std::endl;
}
return 0;
}
```
### 2.6 时间结构的转换函数
```cpp
#include <iostream>
struct time {
int hours;
int minutes;
int seconds;
};
long time_to_secs(time t) {
return t.hours * 3600 + t.minutes * 60 + t.seconds;
}
time secs_to_time(long secs) {
time t;
t.hours = secs / 3600;
secs %= 3600;
t.minutes = secs / 60;
t.seconds = secs % 60;
return t;
}
```
### 2.7 重载的 power 函数
```cpp
#include <iostream>
double power(double n, int p = 2) {
double result = 1;
for (int i = 0; i < p; ++i) {
result *= n;
}
return result;
}
char power(char n, int p =
```
0
0
复制全文
相关推荐









