C++ Quiz no.4

Fix the following code so the B is destroyed properly:

struct A {};
struct B: A {};

int main()
{
   A * p = new B;
   delete p;
}

————————————

Answer to C++ Quiz no.3

#include <iostream>
class example
{
 public:
   example& operator+(const example& obj1);
};

example &example::operator+(const example& obj2)
{
 return *this; //Every object in C++ has access to its own address through an important pointer called this pointer.
}

int main(void)
{
  example a, b;
  example* c;
  c = &(a + b);
  //std::cout<< &c <<" "<<"\n";
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s