When I've taken classes (small class size only) I generally find the instructor goes around the room and gives the students a chance to say hi, introduce themselves and what they're called.
For example, I had a friend whose name was William Alexander but all of the men in his family are named "William _____" so they all go by their middle names. So when we were introducing ourselves, he said
I'm on the roster as "William" but I go by my middle name, "Alex".
This format works for most occasions, whether they know your name or not. Even if you're not able to introduce yourself to the class, if you're speaking with the teacher/professor after class and you want to be certain they know who you are on the roster, you can do the same thing.
Because you asked, if you're joining an existing class, how they introduce new students (if they even do) will vary widely based on the class. Some classes may not specifically welcome new students but you should still be able to greet the teacher by arriving early to the first class and introducing yourself with the above phrase or something similar. If they do have a "meet the new student" introduction, you can let them know at that time. If you really want them to know your preferred name before you get there, you could also let them know in advance either over the phone or by email.
If you're introducing yourself to someone who doesn't know your name already, you can just introduce yourself by what you prefer to be called. It's possible that this may cause some small amount of confusion at some point, but even if you introduce yourself with your actual name and your nickname, there's no guarantee that in a couple of months they'll remember the name you've never used.
I think the exception to this may be if your preferred name is not a "normal" name. It's one thing to go by "Alex" instead of "Alexander" but if your name's "Alexander" and you go by "Gooshie" you might want to go with the full explanation.
I didn't find out about Alex using his middle name until long after I'd met him. I'd assumed that was is first name, so when he explained it, I found it interesting (I also know his dad, "Mike") that they have this family tradition.
"Gooshie" is short for Dr. Gushman... or so says a couple of sources I've seen.