C++ Questions

Good morning everyone, I'm doing a test and I have some doubts and can not resolve these issues. Someone could help me?

thanks.

1) What is wrong with the code below and how to fix?

const std::size_t COUNT = 100;
|
int* ids = new int[COUNT];
|
retrieveIds( ids, COUNT );
|
for( std::size_t i = 0; i< COUNT; i++ )
|
std::cout << ids;
|
delete ids;


2) There are a number of improvements that can made ​​in the above code using relevant features of C + +. Rewrite the text above using these resources as much as possible while maintaining the same final functionality.


3)Given the code below.

class A
{
public:
A() { std::cout << "#A"; }
~A() { std::cout << "~A"; }
};

class B : public A
{
public:
B() { std::cout << "#B"; }
~B() { std::cout << "~B"; }
};

void test()
{
B b; // 1
A* a = new B; // 2
delete a;
}

The result would be printed on the screen: #A #B ~B ~A for // 1 and #A#B~A to 2
What can be changed so that //1 and //2 has the same result?

4) What is wrong with the code below and how to fix?

#define LENGTH 100
|
unsigned char buffer[LENGTH];
|
size_t length = 0;
|
length = receive( buffer );
|
printf( "Received %d bytes with %s\n", length, buffer );

5) How do you keep the same file is included multiple times? Is there any other way (even if non-standard)?
For 1 and 4, do you at least see what the problems are? Remember, you can compile it and see for yourself.
For 2, I honestly have no idea.
For 3, see polymorphism.
For 5, google "header guards"
1. You should use delete[] ids;
2. I don't understand this point
3. Make Class A virtual. virtual ~A() { std::cout << "~A"; }
4. how does "receive()" function know the size of "buffer". This may cause memory leaks.
5. Two ways, 1-Use Gaurd MACROS, 2-Use #pragma once (call compilers may not support this)
1 That's not all. What does std::cout << ids; do?
4 Also, I have doubts about whether buffer contains a null terminates string. Though that might be just me.
Thanks for the replies!
Topic archived. No new replies allowed.