OOP consists of different types of classes%u2014public, private, and protected.
ID: 3536943 • Letter: O
Question
OOP consists of different types of classes%u2014public, private, and protected. What are the differences among these classes? If you were asked to create a class to solve a business problem, which of these classes would you use? Why?
Using pointer syntax, however, you can declare arrays at runtime through dynamic allocation and size the arrays to fit the requirements of the program. What is the best way to dynamically allocate an array of integers? Explain, specifically, how this method differs from static allocation and the benefits of using dynamic allocation of arrays.
Explanation / Answer
The basic differences between these class types are (they ae called access modifiers):
(here "can accessed" means to create an object of that class using new operator)
Public Class: Can be accessed anywhere, by external classes/ programs.
Protected Class: It can only be accessed by classes which inherit the protected class. No other class can use it.
Private Class: Cannot be accessed anywhere else, except within the class itself.
Best way to dynamically assign array is by new operator:
Array_type array_name = new Array_type[Size of array]
Example: int cars=new int[10]; //dynamically initialize array of 10 integers
In dynamic allocation, memory is assigned to heap (runtime memory), and not the stack (which is used in static allocation)
Benefits of dynamic allocation is that it is much more efficient as memory is allocated only when required, and can be freed if unused.
In static allocation, memory remains allocated throughout the program execution, irrespective of whether in use or not.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.