Thursday, 21 May 2020

class having data members pointer to integer

#include <iostream>
using namespace std;
 class Pair 
 {
    public:
    int *pa,*pb;
    Pair(int, int);
    Pair(const Pair &);
    ~Pair();
};

 Pair::Pair(int x,int y)
 {
   std::cout<<"para const";
    pa=new int(x);
    pb=new int(y);
 }
 Pair::Pair(const Pair &p)
 {
   std::cout<<"copy const";
  pa=new int(*(p.pa));
  pb=new int(*(p.pb));
 }
 Pair::~Pair()
 {
   std::cout<<"destructor";
 }
int main()
{
  
   Pair p(15,16);
   Pair q(p);  
   Pair *hp = new Pair(23,42);
  delete hp;
  hp=nullptr;
  cout << "If this message is printed,"
    << " at least the program hasn't crashed yet!\n"
    << "But you may want to print other diagnostic messages too." << std::endl;
    return 0;
}

No comments:

Post a Comment