C++PTA填空题

  1.

A pointer allows us to pass   potentially large amounts of data around at low cost: instead of copying the data we simply pass its  address  as a pointer value. The type  of the pointer determines what can be done to the data through the pointer.

2.

Like a pointer, a reference is an alias for an object, is usually implemented to hold a machine address   of an object, and does not impose performance overhead compared to pointers, but it differs from a pointer in that:

• You access a reference with exactly the same  syntax as the name of an object.

• A reference always refers to the object to which it was  initialized  .

• There is no "null reference," and we may assume that a reference refers  to an object.

 

3.

A reference is an alternative name  for an object, an alias. The main use of references is for specifying arguments and return values for functions in general and for overloaded operators in particular.

 

4.

A class is a user-defined  data-type .

A class consists of a set of members. The most common kinds of members are data members

 and member functions   .
A class is a namespace containing its members.

 

5.

C++ classes are a tool for creating new types that can be used as conveniently as the built-in types.
The fundamental idea in defining a new type is to separate the incidental details of the implementation  (e.g., the layout of the data used to store an object of the type) from the properties essential to the correct use of it (e.g., the complete list of functions that can access the data). Such a separation is best expressed by channeling all uses of the data structure and its internal housekeeping routines through a specific  interface  .

 

6.

The public members provide the class’s ​​​interface  and the private members provide implementation details. Members are accessed using . (dot) for ​​objects​​ and −> (arrow) for pointers.

 

7.
A struct is a class where members are by default ​​​public​​.

 

8.基本概念

类是对象的抽象,而一个对象则是其对应的一个 实例

 

9.基本概念

在面向对象程序设计方法中,对象是系统中用来描述客观事物的一个实体,它由 数据 和可执行的一组操作共同组成。

 

10.

除了可以通过对象名来引用静态成员,还可以使用引用 类名 静态成员。

 

11.

静态数据成员初始化必须在  类外  进行。

 

12.

A variable that is part of a class, yet is not part of an object of that class, is called a  static member. There is exactly one    copy of a static member instead of one copy per object, as for ordinary non-static members.

 

13.

By declaring a nonmember function a   friend​, the function can access the private part of the class declaration. That is, a function declared friend is granted ​access  to the implementation of a class just like a member function but is otherwise independent of that class.

 

14.

In a(n) ____is-a______ relationship, an object of a derived class also can be treated as an object of its base class.

 

 

15.

A base class's ____protected______ members can be accessed only in the base-class definition or in derived-class definitions.

 

16.

A binary operator can be defined by either a non-static member function taking one   argument or a nonmember function taking  two   arguments.

 

17.

 A unary operator, whether prefix or postfix, can be defined by either a non-static member function taking   no  arguments or a nonmember function taking  one   argument.

 

18.

The name of an operator function is the keyword  operator   followed by the  operator itself, for example, operator<<. An operator function is declared and can be called like any other function. A use of the operator is only a shorthand for an  explicit  call of the operator function.

 

虚函数:

19.

Virtual functions overcome the problems with the type-field solution by allowing the programmer to declare functions in a base class that can be   redefined  in each derived class. The compiler and linker will guarantee the  correct  correspondence between objects and the functions applied to them.

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在C/C++编程语言中,内存是一个非常重要的概念。内存是计算机用于存储和访问数据的地方,它可以被看作是一个巨大的存储器数组,每个元素都有一个独特的地址。 在C/C++中,我们可以使用指针来访问和操作内存。指针是一个特殊类型的变量,它存储了一个内存地址。通过指针,我们可以间接访问和修改内存中的数据。 当我们在程序中声明一个变量时,系统会为该变量分配一块内存空间,并将其地址存储在变量名中。我们可以通过使用变量名来访问和修改该内存空间中的值。 另外,我们可以使用动态内存分配函数来在运行时动态地分配内存。这在需要在程序中创建变量长度的数组或者临时存储空间时非常有用。动态内存分配函数包括malloc、calloc和realloc。在使用这些函数分配内存后,我们需要记得通过使用free函数来释放这些内存空间。 值得注意的是,C/C++中的内存管理是程序员的责任。这意味着我们在使用指针和动态内存分配函数时需要小心,以避免内存泄漏和悬挂指针等问。我们需要确保我们在使用完内存后及时释放它,以避免浪费内存资源。 总结来说,C/C++中的内存是一个重要的概念,我们可以使用指针来访问和操作内存。通过动态内存分配函数,我们可以在程序运行时动态地分配内存。然而,我们也需要负责管理内存,以避免出现内存泄漏和悬挂指针等问。 ### 回答2: C/C++中的内存填空是指填写一段代码,完成特定的内存操作。以下是一个例子: ```c #include <stdio.h> int main() { int array[5]; // 声明一个包含5个整数的数组 int *p = array; // 声明一个指向数组首元素的指针 // 使用循环将数组中的元素赋值为0到4 for (int i = 0; i < 5; i++) { *(p + i) = i; } // 打印数组中的元素 for (int i = 0; i < 5; i++) { printf("%d ", array[i]); } return 0; } ``` 在这个例子中,我们声明了一个包含5个整数的数组`array`,然后使用指针`p`指向数组的首元素。接下来,通过循环遍历数组,利用指针`p`对数组元素进行赋值操作,赋值的值为数组下标。最后,再通过循环遍历数组,利用数组`array`打印出各个元素的值。这段代码展示了C/C++中的指针和数组的使用,以及对内存空间的操作。 ### 回答3: C/C++ 内存填空一般涉及指针和内存管理的知识。下面给出一个例子以300字来回答: 以下是一道关于C/C++ 内存填空的解答。 ```c #include <stdio.h> #include <stdlib.h> int main() { int* ptr = (int*)malloc(sizeof(int)); int* arr = (int*)calloc(5, sizeof(int)); *ptr = 10; for (int i = 0; i < 5; i++) { arr[i] = i; } printf("Ptr: %d\n", *ptr); printf("Arr: "); for (int i = 0; i < 5; i++) { printf("%d ", arr[i]); } printf("\n"); free(ptr); free(arr); return 0; } ``` 上述代码中包含了两个关于内存的填空处,首先是通过`malloc(sizeof(int))`来分配存储 int 类型数据的内存空间,并将其地址赋值给`ptr`指针;另一个是通过`calloc(5, sizeof(int))`来分配存储 5 个 int 类型数据的连续内存空间,并将其地址赋值给`arr`指针。 接着通过`*ptr = 10`给指针 `ptr` 所指向的内存位置赋值为 10。并用一个 for 循环给数组 `arr` 赋值为 0 到 4。 最后通过`printf`打印结果。Ptr 输出为 10, Arr 输出为 0 1 2 3 4,表示内存填空处正确。 最后需要调用`free`函数手动释放内存,以避免内存泄漏。 在实际编程中,动态内存分配是一个常见的操作,合理地申请内存并及时释放内存对于提高程序的性能和效率十分重要。因此对于这类目要熟悉`malloc`、`calloc`、`realloc`、`free`等函数的使用规则和注意事项,以及指针的正确使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值