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?
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.