this post was submitted on 05 Aug 2024
20 points (100.0% liked)

Learn Programming

1615 readers
2 users here now

Posting Etiquette

  1. Ask the main part of your question in the title. This should be concise but informative.

  2. Provide everything up front. Don't make people fish for more details in the comments. Provide background information and examples.

  3. Be present for follow up questions. Don't ask for help and run away. Stick around to answer questions and provide more details.

  4. Ask about the problem you're trying to solve. Don't focus too much on debugging your exact solution, as you may be going down the wrong path. Include as much information as you can about what you ultimately are trying to achieve. See more on this here: https://xyproblem.info/

Icon base by Delapouite under CC BY 3.0 with modifications to add a gradient

founded 1 year ago
MODERATORS
 

So I have struggled with classes and objects but think I'm starting to get it...? As part of a short online class I made a program that asked a few multiple choice questions and returns a score. To do this there are a few parts.

  1. Define some inputs as lists of strings ((q, a), (q2, a2),...). The lists contain the questions and answers. This will be used as input and allows an easy way to change questions, add them, whatever.

  2. Create a class that takes in the list and creates objects - the objects are a question and it's answer.

  3. Create a new list that uses that class to store the objects.

  4. Define a function that iterates over the list full of question/answer objects, and then asks the user the questions and tallies the score.

Number 2 is really what I am wondering about, is that generally what a class and object are? I would use an analogy of a factory being a class. It takes in raw materials (or pre-made parts) and builds them into standard objects. Is this a reasonable analogy of what a class is?

top 4 comments
sorted by: hot top controversial new old
[–] Windex007@lemmy.world 13 points 3 months ago

Class is the blueprint of whatever, a house.

Object is a house that's actually been built using that blueprint. You might have 50 houses (objects) defined by a single blueprint (class).

[–] DScratch@sh.itjust.works 4 points 3 months ago

“Is this a reasonable analogy”

Kind of. That concept is sometimes referred to as the Factory Pattern. You give it some raw materials and it spits out some products.

While in this question it does use a class, classes are more basic than that.

I think of a Class like a blueprint for a car. And each car that is actually built from that blueprint as an Object. The blueprint can have settings, like paint colour, seat fabric and the like.

For the question in the OP, you can imagine you design a form in Word that you print out. The form has 2 blank fields; Question and Answer. This is a Class. You can call it QuestionClass (terrible name, used for clarity)

When you print those forms you are creating an Object. Also known as creating an Instance of the Class. Also known as Instantiating.
Then you can fill in the questions and answers on each sheet/Object.

You can, however, when designing your Class make it so you MUST supply the question and the answer for this instance when you create it, rather than add them later. You give the class some initial values. You call this an Initialiser.

Now take a step back. Make a new function that takes in a list of Question-Answer pairs and spits out a bunch of the QuestionClass Objects.

That’s a big chunk of the work done.

[–] adespoton@lemmy.ca 2 points 3 months ago

You are taking a short online class. The class has objectives.

The class itself is defined by what it is supposed to handle (students mastering X skills). But the class by itself is only a structure, a definition that can be used to standardize ehat this sourt of course will look like. Within that class are actual students producing work that is more or less in line with the class objectives. These are objects that belong to the class that contain work.

A class is the structured definition. So you could have a class “factory” that defines a factory as a large building that takes in raw materials and outputs a specific type of finished goods.

The automotive factory on Fifth would be an object in the Factory class. That factory itself would be populated with contents defined by how the factory was operating.

Likewise in Python, you can define a class Greeting that contains a single string. You can then define an object friendlyGreeting of type Greeting, and that object will automatically contain a single string, because that’s what objects of class Greeting have. You can then populate the string in friendlyGreeting with the characters “Hello World” and you have an instantiated object. You could then have another object inheriting Greeting named unfriendlyGreeting. It will also inherit the single string element. But if you never instantiate unfriendlyGreeting, it won’t be usable in your program, and will have no actual contents.

So to sum up, classes define the structure of objects you use in your program. Objects define specific “things” that you can make copies of and interact with in your program. When an object is actually used and is containing data to be used in a specific way in your program, it is instantiated (there is an actual instance of that object).

[–] JackbyDev@programming.dev 1 points 3 months ago

Classes are like definitions for what objects look like. A lot of times people will use the terms interchangably in non formal settings.

It so happens that the class they want you to make is like a factory, but not all classes are like that. They can do anything. It's just a different way to organize your code. Languages like C don't have classes and you can write Python code without classes. There is a concept called the "factory pattern" (not going to bore you with the details) but suffice to say, it's a very common thing so your analogy is correct -- but as long as you remember not all classes behave like that, just this specific one.