by Dinesh Thakur Category: Array Pointer and Union

 

#include <iostream.h>
#include<conio.h>
class CRectangle
{
                 int width, height;
        public:
                     void set_values (int, int);
                     int area (void)
                 {
                     return (width * height);
                 }
};
                 void CRectangle::set_values (int a, int b)
             {
                     width = a;
                     height = b;
                 }
                     void main ()
                {
                     clrscr();
                     CRectangle a, *b, *c;
                     CRectangle * d = new CRectangle[2];
                     b= new CRectangle;
                     c=&a;
                     a.set_values (1,2);
                     b-> set_values (3,4);
                     d-> set_values (5,6);
                     d[1]. set_values (7,8);
                     cout << "a area: " << a.area() << endl;
                     cout << "*b area: " << b->area() << endl;
                     cout << "*c area: " << c->area() << endl;
                     cout << "d[0] area: " << d[0].area() << endl;
                     cout << "d[1] area: " << d[1].area() << endl;
                     delete[] d;
                     delete b;
                      getch();
                 }

Pointer To Classes Example



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles