Tuesday, December 9, 2014

Pointer with array pro and Complex pro in C++



Object Reference, Pointer and Array with Dynamic Allocation

 
#include 
#include
#include 
using namespace std;
int main() 
 {
             time t1(1, 2, 3);
   t1.print();  
   time* ptrt1 = &t1;
   (*ptrt1).print();
   ptrt1->print();   
   time& reft1 = t1; 
             reft1.print();      
             time* ptrt2 = new time(4, 5, 6); 
   ptrt2->print(); 
   delete ptrt2;   
   time tarray1[2];   
   tarray1[0].print();  
   tarray1[1].print();  
   time tarray2[2] = {time(7, 8, 9), time(10)}; 
   tarray2[0].print();
   tarray2[1].print();  
   time* ptrtarray3 = new time[2];   
   ptrtarray3[0].print();  
   ptrtarray3[1].print();  
   delete[] ptrtarray3;    
   time* ptrtarray4 = new time[2] {time(11, 12, 13), time(14)};
    ptrtarray4- >print();                  
   (ptrtarray4 + 1)->print();  
   delete[] ptrtarray4;
}
Complex
 
#include
#include
#ifndef complex_h
#define complex_h
class complex 
{
private:
   double real;
   double imag;
 public:
   complex(double real = 0.0, double imag = 0.0);
   double getreal() const;
   void setreal(double real);
   double getimag() const;
   void setimag(double imag);
   void setvalue(double real, double imag);
   void print() const;
   bool isreal() const;
   bool isimaginary() const;
   complex & addinto(const complex & another);
   complex & addinto(double real, double imag);
   complex addreturnnew(const complex & another) const;
   complex addreturnnew(double real, double imag) const;
};

No comments:

Post a Comment