#Objects :0

207 messages · Page 1 of 1 (latest)

coral panther
#

Homework 8 - Objects and Data Encapsulation -

  1. Create a class for STUDENT objects that uses only private member data.
  2. Data includes: first name, last name, id#, array of 3 subjects, array of 3 grades).
  3. Create both accessor and modifier member functions to manage all data operations.
  4. Instantiate a STUDENT object.
  5. Initialize the data, change the data, and print the data using public member functions.
  6. Label the data that is printed.
lunar kernelBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

coral panther
#

Here's my code so far 🙂 ```#include <iostream>
using namespace std;

class student
{
private:
string first_name;
string last_name;
int id_number;
string subjects[3] = { Physics, Chemistry, Calculus };
int grades[3] = { 74, 63, 92 };

};```

#

Is it correct so far, and what should I do next??

simple shell
#

well, based on your list up there, the next steps are

  1. Create both accessor and modifier member functions to manage all data operations.
  2. Instantiate a STUDENT object.
  3. Initialize the data, change the data, and print the data using public member functions.
  4. Label the data that is printed.
coral panther
#

Yes! I’m not entirely sure how to create an accessor and modifier.

  • would I need to create a ‘public’ list at all??
#

Could you provide me an example of accessor and modifier please

simple shell
#

i would assume that you discussed how to make getters/setters in class?

#

and yes, they will probably need to be public, otherwise they'd be quite useless

#

(tbf, getters/setters are generally bad practice, but well, looks like that's what your school is teaching you ^^)

coral panther
#

My professor provided his GitHub for objects but honestly, I’m still familiarizing myself with objects overall. I only really know the definition of ‘accessor and modifiers’ but beyond that, I don’t really know much.

#

I only started the topic of objects yesterday and it is quite confusing

#

Would you be willing to look at the GitHub?

simple shell
#

i'm not sure what i would see on that github

#

an accessor aka getter for a member is a function that just returns you the value of the member

#

and a modifier aka setter for a member is a function that lets you set the value of the member

coral panther
#

Oh on his GitHub, it’s his in class examples!

#

And I see. Do you have an example of an accessor and modifier. It doesn’t have to relate to my code, I just want to see an example and I’ll try to do the same and integrate it into my code.

coral panther
lunar kernelBOT
#

@coral panther Has your question been resolved? If so, type !solved :)

coral panther
#

No!

coral panther
#

Isn’t that an example of only an accessor??

#

Or am I missing something

simple shell
#

it's an example of a modifier and an accessor

#

do you not have any book or at least course notes or anything that actually teaches you this stuff?

#

or how are you expected to learn this?

coral panther
#

The way that I’m expected to really learn this stuff is learn his code given on GitHub

#

And do the assignment given to us

simple shell
#

wtf

coral panther
#

I mean, he provided definitions of it but frankly it’s a little tough to understand it with not much coding experience

#

Anyways, can you tell me which line shoes the accessor and modifier

simple shell
#

what do you think?

#

but seriously man, how on earth do they expect you to learn anything without proper instruction

#

like wtf

coral panther
#

😭 idk

#

That’s why I’m in this server LOL

simple shell
#

this is seriously fucked up

#

they didn't provide you with at least a book you can read that will actually explain you the language? they just give you this github and tell you to learn it? with no further instruction?

coral panther
#

Well, he provides in class slides but again, it’s merely definitions.

#

And the physical examples are the GitHub

#

That’s why it’s tough.

simple shell
coral panther
#

But I assume line 46,53, etc. is an accessor?

simple shell
#

the get function gets the value, it's an accessor

#

the set function sets the value, it's a modifier

#

but like, do you even know what an int& is? or what member functions are? was any of that explained at all?

coral panther
#

Yeah! I learned it while doing my prior assignments

#

Int is integer

#

Member function is the . Thing

simple shell
#

note the &

#

that's a reference

coral panther
#

Integer and?

#

I’ve never seen that. Just int.

simple shell
#

man, i don't know how they expect you do this. this is bullshit. you should complain.

#

this teacher needs to be fired

#

seriously

#

they have no business teaching people

coral panther
#

LOL

#

It’s fine with me, I’m done with his class in 3 weeks sooo

simple shell
#

yeah but you'll have learned nothing

#

except for how to pass this stupid class

coral panther
#

I know a bit. I’m not great at it but I know a bit.

simple shell
#

man, the decline of the education system really is fucking sad

#

your school is failing you

#

i'm sorry about this

coral panther
#

I mean, it’s not my major sooo

coral panther
simple shell
#

other way round

#

acessor gets you the value

#

modifier sets the value

#

note that in your case, you can just have it return the value

#

instead of using out parameters like this does

coral panther
#

Accessor is line 53, Modifer is line 46

simple shell
#

yes

coral panther
#

Ig my question is, what does it mean to ‘get’ or ‘set’ a value. Is setting a value initializing a value?

#

And getting a value is returning the valued

#

Value?*

simple shell
#

it means to get the value from the object and to set the value in the object

#

you use those functions if you need the value to do stuff

coral panther
#

Oh, I see.

simple shell
#

i'd also like to reiterate that this is complete bullshit malpractice and not how you actually want to program

#

but some people like doing this

#

you'll have to do it like this because your teacher tells you do

#

but just be aware that that's not how you actually write code

#

just in case you're planning to ever actually learn programming properly

coral panther
#

Hm okay!

#

My question now is what’s the difference between public and private. Like I know he told me to put all of this info in private but what exactly am I supposed to put in public

#

Bc you said I need a public thing to make the code work??

simple shell
#

private means it's only accessible by the class itself

#

public means it's accessible to anyone using the class

#

if you write code that does stuff with a student object, that code will be limited to using the public parts of the class

#

only code inside the class itself can access the private parts

coral panther
#

So ig kind of like a web browser. The public part will be seen by everyone? And the private part will only be seen by a certain private group?

#

Or am I misunderstanding

#

Lol no it’s one of my required course for my physics degree.

chilly minnow
#

Ah. You should contact your university and let them know how horrible this class is and that there’s no actual teaching

#

Providing a GitHub and assignments is not a way to teach students

coral panther
#

😭 maybe you’re right

chilly minnow
#

I mean, sounds like you’ll still have to go through the class. But please do that either now or when you’re finished with the course

coral panther
#

I’ll do it at the end bc tbh I don’t want him to mess with my grade, yk?

#

But yeah it’s only 3 weeks left and then I’m done. I have some knowledge but I’ll be honest, most of it comes from discord servers like this

simple shell
#

but if there's some anonymous course evaluation or smth, you should say something there

#

obviously be diplomatic and don't risk making yourself a target for retaliation

#

but if you can say smth, say smth

#

because this is bullshit

coral panther
#

Ofc I’ll do that

#

They do these anonymous course evaluations at the last week of classes, I’m going to poorly rate him

#

Anyways, I’m trying to continue my code but tbh I don’t know what to add in my public. I know I use set and get but idk set what or get what

warm urchin
#

Instructions on step 3 says, "Create both accessor and modifier member functions to manage all data operations."

#

Once you create a setter and getter for one, it's easy to do the rest

coral panther
#

Don’t I need to create a public first before adding the setter and getter?

warm urchin
#

yea, it's just
public:
//functions
private:
//data members

coral panther
#

Can you please give me examples of functions

warm urchin
#

look up setters and getters in c++

coral panther
#

I’m looking at this thing rn? Is it not the right thing

warm urchin
#

Don't use YouTube for C++

#

look it up in Google

coral panther
#

oh

simple shell
#

well, for that they're doing, it's probably fine

warm urchin
#

oh alright

simple shell
#

you need to consider that they're just trying to pass this stupid course

#

they're not trying to actually learn good C++

#

and they're on a reasonably tight time limit

#

so dunno

#

if this video helps you understand some of the stuff going on in that code, that's fair enough imo

coral panther
#

oh, this isnt due until next wednesday.

simple shell
#

yeah but you're not gonna start C++ from scratch properly until next wednesday ^^

#

i certainly wouldn't recommend attempting that at least ^^

coral panther
#

i mean, tbh, when i have the time, maybe i'll learn it from scratch bc comp sci is interesting BUT rn i just dont have time

#

Im a double major soo

simple shell
#

yeah

#

if you ever do

lunar kernelBOT
#
How to Learn C++ Programming

We generally recommend a good book to learn the necessary fundamentals:

To actually write and run C++ code, you will need a compiler, editor, and debugger. We strongly recommend to start out using an IDE, which will provide all these tools for you:

<:microsoft:1165512917047853127> Windows
  • [Visual Studio](#1165492293810257920 message)
  • CLion
<:tux:1165505626894520361> Linux
<:apple:1165508607798943754> Mac
Words of Advice

The wise programmer is told about the debugger and uses it.
The average programmer is told about the debugger and avoids it.
The foolish programmer is told about the debugger and laughs at it.

simple shell
#

^

coral panther
#

OH LOL i didnt know xcode or clion was a thing

#

I TRIED DOWNLOADING visual studios on my ma

#

mac

#

LMAO

simple shell
#

lol

coral panther
#

that's crazy to me, ty

#

anyways, i know its void set_function

#

but my problem is, what is the function

#

bc on the example, it was all based on time

#

void set time, get time, seconds, hr, minutes

#

but mine is based on the attributions of students

#

so should it be void_student???

simple shell
#

well, you have first name, last name, id, grade, etc.

#

you wanna get/set all of those

#

i suppose

coral panther
#

but isnt that in my private

#

i have to do it twice?

simple shell
#

that's why you need the accessors/modifiers: so that you can get at those from outside the class

coral panther
#

in both i mean

#
using namespace std;

class student
{
private:
    string first_name;
    string last_name;
    int id_number;
    string subjects[3] = { Physics, Chemistry, Calculus };
    int grades[3] = { 74, 63, 92 };

public:
    void set_first_name;
    void get_first_name;
    void set_last_name;
    void get_last_name;
    void set_id_number;
    void get_id_number;
    void get_grades;
    void set_grades;
    void get_subjects;
    void set_subjects;```
#

is this correct so far

simple shell
#

well, functions are gonna need parameter lists

coral panther
#

oh like the insides of it

#

{ }

#

like inside this thing

simple shell
#

no, like the parameters of it

#

maybe you should take a step back and look at some resources that explain the basics of functions and classes

#

it's gonna be hard to write this when you're not exactly familiar with the tools you're supposed to use

coral panther
#

i see

#

isnt a function something you define first

#

and then initialize later on?

simple shell
#

um

#

yeah no, this just tells me that you have no idea what a function is. like at all ^^

#

which is fine

#

but you really need to learn that first

coral panther
#

this is what im looking at

#

like im creating a function called addition and then i initialize it

#

or is that wrong

simple shell
#

you're defining it.

#

not initializing it.

#

initialization is providing the initial value of something.

#

functions don't even have values.

#

they're not objects.

coral panther
#

oh i used the wrong terminology

#

my bad

#

so in the public funciton, is this where i define it?

simple shell
#

you can define it inside the class or outside the class

#

up to you

#

your teacher's code did it outside

#

but, again, i would highly recommend to check out some resources that explain classes and member functions.

#

just looking at example code is a terrible way to try to learn anything, let alone C++

#

the problem is that example code doesn't explain the actual principles behind how the language works, or the intention behind why the code looks like it does as opposed to some other way

coral panther
#

hm ok. how about this, ill try to learn more about it and then come back tomorrow with a better code.

#

or at least, watch videos and google it more

#

ig i need a background on function, and getters and settes

#

?

simple shell
#

on functions, classes, and member functions

#

and yes, then you can also look up getters and setters

coral panther
#

ok ill do that bc tbh im quite lost with what i have to do now

#

anyways, gn!! but ill try to learn it all by tmrw

#

Do you have any recommended links to learn??

simple shell
#

well, i'd recommend a good book

coral panther
#

Tysm

simple shell
#

it's not great but it might do