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

259 views

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

In: 10

21 Answers

Anonymous 0 Comments

They’re not too hard to grasp. Think of a class like a container for all the relevant bits of an object, like the things it can do and any data.

The class ‘Car’, for example, might have;
`int numberOfWheels, float fuelInTankLitres, string registrationNo;`
It might also contain some functions that are required for the car’s function;
`StartEngine(); EnableAC(); LockDoors();`
If we create an object from this called firstCar, we can use `firstCar.registrationNo = 7JML581;` to register that car.

We could say;
`Car firstCar = new Car;`
`firstCar.fuelInTank = 10f;`
`if (firstCar.fuelInTank > 0) then firstCar.StartEngine();`
This uses the Car class to create a new object, the firstCar, and lets you access that object, specifically, without affecting the template. Using the same code, after that part, we could write;
`Car secondCar = new Car;`
`secondCar.LockDoors();`
`secondCar.EnableAC();`
The first car starts its engine, the second car locks its doors and its heating turns on, but the second car doesn’t start. While the template is the same, the two cars are different and separate entities of their own.

Anonymous 0 Comments

They’re not too hard to grasp. Think of a class like a container for all the relevant bits of an object, like the things it can do and any data.

The class ‘Car’, for example, might have;
`int numberOfWheels, float fuelInTankLitres, string registrationNo;`
It might also contain some functions that are required for the car’s function;
`StartEngine(); EnableAC(); LockDoors();`
If we create an object from this called firstCar, we can use `firstCar.registrationNo = 7JML581;` to register that car.

We could say;
`Car firstCar = new Car;`
`firstCar.fuelInTank = 10f;`
`if (firstCar.fuelInTank > 0) then firstCar.StartEngine();`
This uses the Car class to create a new object, the firstCar, and lets you access that object, specifically, without affecting the template. Using the same code, after that part, we could write;
`Car secondCar = new Car;`
`secondCar.LockDoors();`
`secondCar.EnableAC();`
The first car starts its engine, the second car locks its doors and its heating turns on, but the second car doesn’t start. While the template is the same, the two cars are different and separate entities of their own.

Anonymous 0 Comments

They’re not too hard to grasp. Think of a class like a container for all the relevant bits of an object, like the things it can do and any data.

The class ‘Car’, for example, might have;
`int numberOfWheels, float fuelInTankLitres, string registrationNo;`
It might also contain some functions that are required for the car’s function;
`StartEngine(); EnableAC(); LockDoors();`
If we create an object from this called firstCar, we can use `firstCar.registrationNo = 7JML581;` to register that car.

We could say;
`Car firstCar = new Car;`
`firstCar.fuelInTank = 10f;`
`if (firstCar.fuelInTank > 0) then firstCar.StartEngine();`
This uses the Car class to create a new object, the firstCar, and lets you access that object, specifically, without affecting the template. Using the same code, after that part, we could write;
`Car secondCar = new Car;`
`secondCar.LockDoors();`
`secondCar.EnableAC();`
The first car starts its engine, the second car locks its doors and its heating turns on, but the second car doesn’t start. While the template is the same, the two cars are different and separate entities of their own.

Anonymous 0 Comments

I think you can say a class groups functions together and allows you to partially apply them all. Partially applying the functions is called instantiating or constructing, and the result is an object.

This allows you to separate out the code that invokes the partial application (i.e. constructing the object) from the code that uses the partially applied functions.

Anonymous 0 Comments

I think you can say a class groups functions together and allows you to partially apply them all. Partially applying the functions is called instantiating or constructing, and the result is an object.

This allows you to separate out the code that invokes the partial application (i.e. constructing the object) from the code that uses the partially applied functions.

Anonymous 0 Comments

I think you can say a class groups functions together and allows you to partially apply them all. Partially applying the functions is called instantiating or constructing, and the result is an object.

This allows you to separate out the code that invokes the partial application (i.e. constructing the object) from the code that uses the partially applied functions.

Anonymous 0 Comments

A function is a basic abstraction. A reusable bit of code you use to accomplish a specific thing. Rather than having to retype that code every time you want to use it, you name it, so when you want to use it, you call it by that name, pass it the information it needs to do its job, and it gives you back a result. And that’s that.

A class is a more complicated abstraction. Instead of just a reusable bit of code, it has a degree of “intelligence”. When you create an instance of a class (an object), it sticks around. You can interact with it. It keeps track of scope and state. It can do things for you, and prevent you from doing others. It can decide who, when and how the data it contains can be viewed or changed. It can even instantiate other objects, an need perform other tasks.

That’s kind of vague, and really surface level. You very quickly move into things like interfaces, abstract classes, inheritance, and other concepts that are confusing at first – but the tl;dr; is that they are different kinds of abstractions. Functions are simple and direct. Basic building blocks, or simple machine…a lever, or incline plane. Classes are a more advanced implementation… building, or a watch, or a logical engine.

Anonymous 0 Comments

A function is a basic abstraction. A reusable bit of code you use to accomplish a specific thing. Rather than having to retype that code every time you want to use it, you name it, so when you want to use it, you call it by that name, pass it the information it needs to do its job, and it gives you back a result. And that’s that.

A class is a more complicated abstraction. Instead of just a reusable bit of code, it has a degree of “intelligence”. When you create an instance of a class (an object), it sticks around. You can interact with it. It keeps track of scope and state. It can do things for you, and prevent you from doing others. It can decide who, when and how the data it contains can be viewed or changed. It can even instantiate other objects, an need perform other tasks.

That’s kind of vague, and really surface level. You very quickly move into things like interfaces, abstract classes, inheritance, and other concepts that are confusing at first – but the tl;dr; is that they are different kinds of abstractions. Functions are simple and direct. Basic building blocks, or simple machine…a lever, or incline plane. Classes are a more advanced implementation… building, or a watch, or a logical engine.

Anonymous 0 Comments

A function is a basic abstraction. A reusable bit of code you use to accomplish a specific thing. Rather than having to retype that code every time you want to use it, you name it, so when you want to use it, you call it by that name, pass it the information it needs to do its job, and it gives you back a result. And that’s that.

A class is a more complicated abstraction. Instead of just a reusable bit of code, it has a degree of “intelligence”. When you create an instance of a class (an object), it sticks around. You can interact with it. It keeps track of scope and state. It can do things for you, and prevent you from doing others. It can decide who, when and how the data it contains can be viewed or changed. It can even instantiate other objects, an need perform other tasks.

That’s kind of vague, and really surface level. You very quickly move into things like interfaces, abstract classes, inheritance, and other concepts that are confusing at first – but the tl;dr; is that they are different kinds of abstractions. Functions are simple and direct. Basic building blocks, or simple machine…a lever, or incline plane. Classes are a more advanced implementation… building, or a watch, or a logical engine.

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.