See below code, I am confused why do we need getter and setter?
#include<iostream>
#include<cstring>
using namespace std;
class car
{
char name[30];
int price;
public:
void get_data(char* n,int p)
{
strcpy(name,n);
price=p;
}
void set_data()
{
cout<<"Name: "<<name<<endl;
cout<<"Price:"<<price<<endl;
}
///Lets add the idea of constructor
car()
{
cout<<"constructor has been called"<<endl;
}
car(char *n, int p)
{
cout<<"2nd constructor has been called"<<endl;
strcpy(name,n);
price=p;
}
~car()
{
cout<<"Name: "<<name<<endl;
cout<<"Price:"<<price<<endl;
}
};
int main()
{
car A("BMW",1000);
car B("Audi",2000);
}
I am asking why do we need getter and setter if Constructor can set the value and print the value? Why the idea of getter and setter?