Question

C++ - describe the concept of "ownership", as it pertains to memory management in C++. -...

C++
- describe the concept of "ownership", as it pertains to memory management in C++.

- What are the three ways a class can contain object's in C++?

-What is a forward declaration and why would you use one?
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer 1:

A resource is something that has to be acquired and later released. Memory acquired by new and released by delete and files opened by fopen() and closed by fclose() are examples of resources where the most direct handle to the resource is a pointer. Whenever we allocate memory in C++, we have to decide the ownership of that memory. An owner of a resource is responsible for freeing the resource when it is no longer needed. It is usually a good idea to immediately place a pointer that represents ownership in a resource handle class, such as vector,string, and unique_ptr. That way, we can assume that every pointer that is not within a resource handle is not an owner and must not be deleted.

The standard library provides two ‘‘smart pointers’’ to help manage objects on the free store:

1) unique_ptr: to represent unique ownership

2) shared_ptr: to represent shared ownership

Answer 2:

A class can contain object in following three ways: private, protected, or public.

• If it is private, its name can be used only by member functions and friends of the class in which it is declared.

• If it is protected, its name can be used only by member functions and friends of the class in which it is declared and by member functions and friends of classes derived from this.

• If it is public, its name can be used by any function.

Answer 3:

A forward declaration is used to declare something in advance of its use. It's a way to tell the compiler about existence of a function or class before actually defining it.

In case of functions it is used to tell the compiler about existence of a function before we actually define it.

eg:

// function prototype

void print_number(int n); // forward declaration

int main() {

int num;

cin >> num;

print_number(num);

}

// function definition

void print_number(int n) {

cout << "Number: " << n << "\n";

}

We use forward declaration to tell the compiler that something is defined in some other file or later in the code, so the compiler can compile the program without any errors.

P.s. Please ask any doubts in comments and don't forget to rate the answer.

Add a comment
Know the answer?
Add Answer to:
C++ - describe the concept of "ownership", as it pertains to memory management in C++. -...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT