What are “Classes” in programming and how are they distinct from functions? What about their applications?

284 views

What are “Classes” in programming and how are they distinct from functions? What about their applications?

In: 10

21 Answers

Anonymous 0 Comments

Class combine functions (what something can do) with data (what something’s state is) together to help you better organize and create programs.

Classes act as blueprints for creating objects, which are specific instances of a class.

For example let’s create a Person class. We’ll keep it simple and give it two variables, firstName and lastName, and one function introduceSelf().

We know that any instance of the Person class has a first and last name and can introduce itself.

We could create two Person objects, let’s say the first one, which we’ll call personA is given the name Bill Jobs. We’ll create a second one, personB and give it the name Steve Gates.

If we tell personA to introduce itself it’ll say “Hi I’m Bill Jobs.”

If we tell personB to introduce itself it’ll say “Hi I’m Steve Gates.”

Both objects that were created using the Person class blueprints have the same action and they use their individual data to perform that action.

By combining data and actions together we can build logical models that make it easier to write complex programs. It can make it easier for others to understand those programs and it can also make it easier to share that code so other people can use it in their own programs.

You are viewing 1 out of 21 answers, click here to view all answers.