c++ Flashcards

1
Q

What are the differences between references and pointers?

A

Both references and pointers can be used to change local variables of one function inside another function. Both of them can also be used to save copying of big objects when passed as arguments to functions or returned from functions, to get efficiency gain.
Despite above similarities, there are following differences between references and pointers.

References are less powerful than pointers

1) Once a reference is created, it cannot be later made to reference another object; it cannot be reseated. This is often done with pointers.
2) References cannot be NULL. Pointers are often made NULL to indicate that they are not pointing to any valid thing.
3) A reference must be initialized when declared. There is no such restriction with pointers

Due to the above limitations, references in C++ cannot be used for implementing data structures like Linked List, Tree, etc. In Java, references don’t have above restrictions, and can be used to implement all data structures. References being more powerful in Java, is the main reason Java doesn’t need pointers.

References are safer and easier to use:

1) Safer: Since references must be initialized, wild references like wild pointers are unlikely to exist. It is still possible to have references that don’t refer to a valid location (See questions 5 and 6 in the below exercise )
2) Easier to use: References don’t need dereferencing operator to access the value. They can be used like normal variables. ‘&’ operator is needed only at the time of declaration. Also, members of an object reference can be accessed with dot operator (‘.’), unlike pointers where arrow operator (->) is needed to access members.

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

What are virtual functions?

A

Virtual functions are used with inheritance, they are called according to the type of object pointed or referred, not according to the type of pointer or reference. In other words, virtual functions are resolved late, at runtime. Virtual keyword is used to make a function virtual.

Following things are necessary to write a C++ program with runtime polymorphism (use of virtual functions)

1) A base class and a derived class.
2) A function with same name in base class and derived class.
3) A pointer or reference of base class type pointing or referring to an object of derived class.

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

What is this pointer?

A

The ‘this’ pointer is passed as a hidden argument to all nonstatic member function calls and is available as a local variable within the body of all nonstatic functions. ‘this’ pointer is a constant pointer that holds the memory address of the current object. ‘this’ pointer is not available in static member functions as static member functions can be called without any object (with class name).

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

Can we do “delete this”?

A

1) delete operator works only for objects allocated using operator new (See this post). If the object is created using new, then we can do delete this, otherwise behavior is undefined.

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

Major Differences between JAVA and C++

A

There are lot of differences, some of the major differences are:

  1. Java has automatic garbage collection whereas C++ has destructors , which are automatically invoked when the object is destroyed.
  2. C++ support multiple inheritance, method overloading and operator overloading but JAVA only has method overloading.
  3. Java is interpreted and hence platform independent whereas C++ isn’t. At compilation time, Java Source code converts into JVM byte code. The interpreter execute this bytecode at run time and gives output. C++ run and compile using compiler which converts source code into machine level language.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

C++의 구조체와 클래스의 차이는?

A
  1. Members of a class are private by default and members of struct are public by default.
  2. When deriving a struct from a class/struct, default access-specifier for a base class/struct is public. And when deriving a class, default access specifier is private.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Malloc() vs new / Delete vs Free

A
  1. new is an operator, while malloc() is a function.
  2. new returns exact data type, while malloc() returns void *.
  3. new calls constructors( class instances are initalized and deinitialized automatically), while malloc() does not( classes won’t get initalized or deinitialized automatically
How well did you know this?
1
Not at all
2
3
4
5
Perfectly