[C++] Class / Struct
Class
In C 'struct' is used for collecting data which have relation to each other.
struct Car
{
char Driver_name[name_len];
int fuelGauge;
int speed;
};
And using 'class' in C++ is almost same as the way of using 'struct'.
Just switch 'struct' to 'class'.
class Car
{
char Driver_name[name_len];
int fuelGauge;
int speed;
};
I was wondering what's the difference between them.
And there is a clear difference.
It's called Access Modifier.
Access Modifier
There is three modifier in C++.
Public : Can access from everywhere
Private : Can access only by the class(functions declared in class) itself
[ inheritance private : When we derive from a private parent class, then public and protected members of the parent class become private members of the child class ]
Protected : Can access only by the class(functions declared in class) itself
[ inheritance protected :When we derive from a private parent class, then public and protected members of the parent class become protected members of the child class ]
The default access modifier of 'struct' is 'public', and 'class' is 'private'.
Then I thought why? there is two similar keyword in C++.
And it is because the developer of C++ made it for replacing C, so it has to be fittable to every code which is written in C.
So you can choose which one you would use according to the situation.
Control Class / Handler Class
The class that actually controls(handles) functional processing
class Worker
{
private:
char name[100];
int salary;
public:
Worker(char* name, int money)
: salary(money)
{
strcpy(this->name,name);
}
int GetPay() const
{
return salary;
}
void Salary_info() const
{
cout<<"name: "<<name<<endl;
cout<<"salary: "<<GetPay()<<endl;
}
};
class Employee_Handler
{
private:
Worker* employee_list[50];
int emp_num;
public:
Employee_Handler() : emp_num(0)
{}
void Add_Employee(Worker* emp)
{
employee_list[emp_num++]=emp;
}
void All_Sal_info() const
{
for(int i=0; i<emp_num; i++)
employee_list[i]->Salary_info();
}
Total_sal
{
int sum;
for(int i=0; i<employee_list; i++)
sum+=employee_list[i]->GetPay();
cout<<"salary sum: "<<sum<<endl;
}
~Employee_Handler()
{
for(int i=0; i<emp_num; i++)
delete employee_list[i];
}
};
In this case, 'Employee_Handler' is a control class.