lecture 12 Flashcards

1
Q

we can define a pointer to a base class object and assign it to the address of a derived class object

but it can only access the member variables and functions of the BASE CLASS ,

it makes sense because the derived class have capabilities that go beyond the base class’s object

A

base class pointers and references only know about members of base class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

typedef int* IntPtr;
IntPtr p;

is equivalent to
int *p;

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

const int*&
is a reference to an int pointer that is constant

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  • instance variable: a member variable in a class. Each object has its
    own copy.
  • static variable: one variable shared among all objects of a class
  • static member function: can be used to access static member
    variable; can be called before any objects are defined
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

static member function
* Declared with static before return type:
static int getObjectCount() const
{ return objectCount; }
* Static member functions can only access static
member data
* Can be called independent of objects:

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

For input containing whitespace, and to control
amount of input, use cin.getline()

getline(cin,address);

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

1.If p1 is an integer pointer variable, with the value of 1000, p1++ changes P1 to point to the memory
location 1001.

A

False. If p1 is an integer pointer variable with the initial value of 1000, p1++ will increment the pointer to point to the next memory location of the integer type, which would be the memory location 1004 (assuming the size of an integer is 4 bytes on the system).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

2.When you return a dynamic array to the freestore, you must include the number of elements in the
array.

A

False. When returning a dynamic array to the freestore (heap), you don’t need to include the number of elements explicitly. However, it is considered good practice to keep track of the size of the array separately if needed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

3.You can assign an array to a pointer variable.
4.The size of dynamic arrays must be declared at compile time.

A

True. In C++, an array name can decay into a pointer to its first element, so you can assign an array to a pointer variable. For example, int arr[5]; int* ptr = arr; is a valid assignment.

True. The size of dynamic arrays in C++ can be determined and allocated at runtime using techniques such as new or malloc. The size is not required to be known at compile time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

5.Dynamically created variables have no name.
6.If p1 and p2 are both pointers that point to integers in memory, the condition p1==p2 will be true if the

A

False. Dynamically created variables do have names. While the memory allocated for the variable may not have a specific name, you can store the pointer to that memory in a variable and refer to it using that variable’s name.

True. If p1 and p2 are both pointers that point to integers in memory, the condition p1 == p2 will be true if they hold the same memory address, indicating that they point to the same memory location. It does not compare the values stored at those locations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

7.Even though pointers point to addresses which are integers, you can not assign an integer to a pointer
variable

A

True. Although pointers point to memory addresses that are integers, you cannot directly assign an integer value to a pointer variable. You need to use type casting or address-of operator (&) to assign a valid memory address to a pointer variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

8.In the following statement, all the variables are pointers.
int* p1, p2;

A

False. In the given statement, only p1 is a pointer variable of type int, while p2 is a regular integer variable. To declare both p1 and p2 as pointers, the statement should be written as int p1, p2; or int p1, *p2;.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

9.A pointer can be stored in an integer variable.

  1. Given that p1 is an integer pointer variable, and a1 is an integer array, the assignment a1=p1 is legal
A

True. In some programming languages, such as C, a pointer can be stored in an integer variable using type casting. However, it is generally not recommended to mix pointer and integer types without a valid reason.

False. The assignment a1 = p1 is not legal. Arrays and pointers are not interchangeable in C++. If you want to copy the values from the memory locations pointed to by p1 into the elements of a1, you need to use a loop or standard library functions such as std::copy

How well did you know this?
1
Not at all
2
3
4
5
Perfectly