Hi,
I m new to cpp. I have doubt in following code. Doubt is regarding pointer. here is code
from the line
cout << "d[0] area: " << d[0].area() << endl;
cout << "d[1] area: " << d[1].area() << endl;
Here d is array which dynamically allocated. but when we are accessing area() function that time we have to use -> instead of ".". but, we are accessing set_value function using dot operator even though it is pointer.
So, i m confused when to use arrow operator and when to use dot operator.
I m new to cpp. I have doubt in following code. Doubt is regarding pointer. here is code
Code:
// pointer to classes example
#include <iostream>
using namespace std;
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;
}
int main () {
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;
return 0;
}
cout << "d[0] area: " << d[0].area() << endl;
cout << "d[1] area: " << d[1].area() << endl;
Here d is array which dynamically allocated. but when we are accessing area() function that time we have to use -> instead of ".". but, we are accessing set_value function using dot operator even though it is pointer.
So, i m confused when to use arrow operator and when to use dot operator.