I have found these examples in the book of c++ written by Herbert Schildth
The first example
#include<iostream>
#include<cstring>
#include<new>
using namespace std;
class Balance
{
char name[80];
double cur_bal;
public:
Balance(){}
void set(double n,char *s)
{
cur_bal=n;
strcpy(name,s);
}
void get(double &n,char *s)
{
n=cur_bal;
strcpy(s,name);
}
};
int main()
{
Balance *p;
char name[80]; double n;
try{ p= new Balance[2]; }
catch(bad_alloc e){ cout<<"alloctaion Failed\n"; return 1;}
p[0].set(121.50,"harsh"); //using DOT to access member function
p[1].set(221.50,"sparsh"); //using DOT to access member function
p[0].get(n,name);
return 0;
}
The second example
#include<iostream>
#include<cstring>
#include<new>
using namespace std;
class balance
{
double cur_bal;
char name[80];
public:
void set(double n,char *s)
{
cur_bal=n;
strcpy(name,s);
}
void get_bal(double &n,char *s)
{
n=cur_bal;
strcpy(s,name);
}
};
int main()
{
balance *p;
char s[80];
double n;
try{p=new balance;}
catch(bad_alloc e){cout<<"Allocation Failed"; return 1; }
p->set(124.24,"harsh"); // using ARROW to access member function
p->get_bal(n,s); // using ARROW to access member function
return 0;
}
Please, I don't need the difference between the two operators(it is already on SO link to that question), but simply explain why they are using arrow and dot respectively, and how do I understand when to use what?