Object / Class
What is Object? Object can be people, can be animal, can be anything.
- Object only store his own data, attributes and property like house has price, size and animal has age and gender.
- Object also store his own function or method like animal can eat(); and people can walk();.
public class People
{
private:
String name;
int age;
int hungry;
public:
void grow()
{
this->age += 1;
}
void eat()
{
hungry = false;
}
}
People can eat, people will grow up so this is true but you try imagine a people can
fly();
? What other programmer see when want to use ur code?
"WTF, WTF is this? Oh my godness."
so just store whatever the object need, have is enough. When some attribute is too much and enable to split to another object, you can also split it like Customer have many Orders and Orders have many attribute so you can make it become 2 object like the example below:
class Customer
{
private:
String name;
Order orders[10];
}
class Order
{
private:
String product;
double price;
int quantity;
}
Container Object
Take container as another example, how we add object to a container? We can't putadd();
inside people object. Remember everything is an object, container also an object. If a container have a add();
doesn't have any problem, and it will be like "Object has a object" and "Container contains a people". Here an example :
People people = new People();
Container container = new Container();
container.add(people);
As you see, container inside contains people but people cant inherit to container they are relation about storing but not inherit. Container can't access people's data, attribute, method, functions and only can add, remove, get, delete. Container also look like a manager managing list of people object.Always remember everything are object and we treat everything as object. Object can be everything can be container, object, manager or many that structure you can think and make your structure better.