Good day
Where we have to create the OOP_constructor page. Below the class we have to create a new object, and pass the required data (manufacturer and colour) to the constructor function. I don't understand why there are set and get Functions? Because I use a construct function for all the data and a Display function, after the class i create the new object and call the display function to display the data of the constructor.So i see it unnecessary for set and get, but each set and get functions counts two points.
Admin
Registered: 11 years ago
Posts: 6,606
Rating: 396
You use the set method to change the data values in an object, eg: In a Customer class, what if an existing customer changes their phone number ? You would use myCusttomer.setPhone(number) to do that.
You use the get method to read a data value from your class. eg: If you only want to display Customers with a particular surname, you would need to use a myCustomer.getName() method.
I get your point of view,but for me its just like alot of unnecessary code that will take extra time, just imagine the hundreds of set,get,set,get's that one must write. Your example of telephone nr, I will just code a new object for the numbers then change that value of that object parameter if a customer wants to change their phone number. Actually if i could have it my way, i was just gonna put it in an array and loop it through, but well its constructors.
Admin
Registered: 11 years ago
Posts: 6,606
Rating: 396
Getters and setters are a normal part of OOP and you may just as well get used to them.
Basically they are just a means of providing public access, in a controlled manner, to data members which are otherwise encapsulated within objects of a class.
It is quite a big jump from structured programming to OOP, but when you get used to OOP you will wonder how / why you ever did it any other way.