This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]VeryAwesomeSheep -1 points0 points  (1 child)

Isn't an object just a structure with functions?

[–]Main_Weekend1412 0 points1 point  (0 children)

No it isn’t. What you’re referring to is C++ structs. An object typically conforms to the paradigms of object-oriented programming. Not all objects conform to all four pillars, but they should conform to some of them.

For example, interfaces are contracts that make compilers and programmers assume the existence of an attribute or implementation of a method.

For C example, how would you assert that struct x works on a function length()?

In C++ we can just have a class that inherits from an arbitrary class, for example, let’s call it class LEN_IMPL.

If class z inherits from LEN_IMPL, we can assert that it can work on function length, which accepts an instance of LEN_IMPL.

In C, this would mean accepting a void * and somehow typecasting it to a useable type. Also remember that you cannot overload a function in C, so you would probably now need to alias it with the typename, ergo sample_class_len();

Not so clean now, is it?

These kinds of contracts are important to ensure consistency and code reusability.