Programming Professor shinanigains

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
corey__cakes
ES Beta Backer
ES Beta Backer
Posts: 23
Joined: Mon Dec 23, 2013 7:56 pm
Current Project: Canadian Simulator 2017
Favorite Gaming Platforms: GBA, DC, iOS, SNES, WS, 360, N64
Programming Language of Choice: C++, Lua
Location: Your VCR.

Programming Professor shinanigains

Post by corey__cakes »

Hello,

I couldn't think of a better topic to post this question in other than programming discussion because it has to do with programming. To be more specific, learning programming from a tenured college professor who may or may not know how to teach or what they're doing.

I am a sophomore undergraduate studying Computer Science. I already have a background in programming with C++ and I've been interested in programming and studying it since I was about 12. In other words, I'm good enough off to have my own opinions on how something should be coded.

But moving along, today, my professor gave a lecture about structs and a classes in C++. Her description of a class was correct to my understanding. When she talked about structs, she actually talked C-style structs (no member functions, no default constructor, all that jazz) and this class is about C++, may I remind you. And I feel as though structs in C and C++ are different enough that it is incorrect to say you can't have member functions declared inside a struct is incorrect in the context of a college course about C++.

To my understanding (and please tell me if I'm an idiot), the biggest most important difference between a struct and a class is that a struct is public by default and a class is private by default. Any other differences mostly have to do with C compatibility with C++. So to me, when I walked out of that class, I felt like she was wrong to teach it like that.

I recall a few spin-off videos Falco made (old ones) where he says a lot of "I don't give a shit if your professor says..." where the following is some belief about how things should be implemented when Falco makes the point that it's not necessarily the best/quickest/most efficient way of implementing that thing.

This is sort along the same line of thinking. Your college professors, however smart they may be, could very well be a crazy jackass that teaches a way of programming that will be eventually be unlearned (at least by me) so that a more efficient, optimal way may be used.

What does a lowly novice/intermediate/adept programming do in order to get by with professor's being incorrect or scolding you for not programming the way THEY want you to program?

Thank you for any answers. And if I am way wrong or out-of-line, or crazy, please tell me otherwise. I really want to learn any and all I can.
What do you call a cow with no legs?

Ground beef.
mattheweston
Chaos Rift Junior
Chaos Rift Junior
Posts: 200
Joined: Mon Feb 22, 2010 12:32 am
Current Project: Breakout clone, Unnamed 2D RPG
Favorite Gaming Platforms: PC, XBOX360
Programming Language of Choice: C#
Location: San Antonio,Texas
Contact:

Re: Programming Professor shinanigains

Post by mattheweston »

The gist of what I was taught in school was learn the fundamentals because no matter where you go they will want you to adapt to THEIR way of doing things. I would suggest to follow along and do things her way in that class to get the grade while researching the information yourself and deciding the optimum way of doing it outside of class. UNLESS, you believe you can change her mind, then I'd prepare a strong argument against what she said and present it to her. I would make sure you have ample resources to backup your claim and don't come across as it's just your opinion. Some profs like students who can raise a strong argument while others will do their best to flunk you for going against their way of thinking. Since I'm not in your class, I can't tell you which way to go, but you should be able to figure that one out fairly easily.
Image
MCU
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 3
Joined: Sat Jan 14, 2017 6:11 am
Favorite Gaming Platforms: SNES, N64, Genesis, Dreamcast, Wii
Programming Language of Choice: C, Assembly

Re: Programming Professor shinanigains

Post by MCU »

corey__cakes wrote: To my understanding (and please tell me if I'm an idiot), the biggest most important difference between a struct and a class is that a struct is public by default and a class is private by default. Any other differences mostly have to do with C compatibility with C++. So to me, when I walked out of that class, I felt like she was wrong to teach it like that.
I'm primarily a C programmer, so this is my take on it, but I wouldn't view structs as some public by default class.

Structures are just a way of creating a raw data type where the data is sequentially placed together.

Classes are usually used to encapsulate something and are used in OOP. So you'll have the get/set methods in a class because you're supposed to encapsulate the data in most cases, and the class methods are an interface to using the data.

A struct is just a raw data type, so it kinda defeats the purpose of having a method inside them I believe, even if you can in C++. Like any other data type, you'll just access it directly in your program's logic.

Structs also have different use cases. In a struct you can use bit fields, for example, in modeling a hardware register. Or something like this

Code: Select all

typedef struct
{
	int sign	: 1;
	int exp	 : 5;
	int mant	: 10;
} half;
for a 16 bit floating point type.

Edit: Apparently classes have bit fields too :lol: , but I'd still say if you're using bit fields for something like a register, you wouldn't use a class anyway. :roll:

You'll also typically use them in unions to represent a register or something which can have its bytes accessed individually, like x86.

Code: Select all

typedef union
{
	uint16_t word;
	struct
	{
		uint8_t high;
		uint8_t low;
	} byte;
} Register;
I can't think of a valid scenario you'd want to use classes for those.

So I would argue the use cases are different, and the difference is much more than a struct's members being public by default.
Last edited by MCU on Wed Jan 25, 2017 11:19 pm, edited 1 time in total.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Programming Professor shinanigains

Post by dandymcgee »

While technically the struct keyword in C++ is exactly the same as the class keyword aside from the default member access being public vs. private, most code I've seen still treats structs semantically as though they are C structs. Remember, you can specify "public:" accessor at the top of your class if you want a fully public class with member methods, constructors, and all that jazz. While you can also do this with the struct keyword, generally it is semantically reserved for use as a complex type that wraps a bunch of public members. You might use give a struct a constructor because it's convenient, but generally even in C++ you don't go crazy with adding member functions in stuff that you declare as a struct. I would argue that your professor is teaching you best practices of how to use the "struct" keyword in a semantically correct way in C++, even though it *can* be used in a lot of other ways.

That said, professors are often incorrect about thing. Sometimes they are seriously and adamantly incorrect. If you think they made a mistake, you could politely correct by asking a question (e.g. "I saw some example code that showed a struct with members in it, and when I tried it it compiled. Why is that?), or by talking to them in private after class. Some professors are honestly just horrible programmers who force students to abide by bad habits, and you just have to deal with it. The same way your employer might force you to do some certain thing in your code that you hate but which isn't really hurting anyone and you'll have to deal with it then. At the end of the day, unless what they're telling you to do is going to seriously harm someone, there's no point debating with someone who isn't willing to discuss things.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Programming Professor shinanigains

Post by Falco Girgis »

I totally agree with you, Corey. What your professor said is incorrect. If it's a C++ class, she should not be pretending structs work the way that they do in C, because that's absolutely incorrect within context of C++, where they are LITERALLY C++ classes with a different default access modifier.

There are a few convoluting factors going on here...
1) C vs C++
2) Actual Implementation/Behavior
3) Expected/Accepted Usages

C vs C++
As you said, this is a fundamental difference. A C struct is not just a "public" class, it literally has no concept of an access modifier, doesn't support inheritance, doesn't allow member functions, and a few more subtleties. These are a massive difference, but yeah, it's more about a difference between the languages.

ACTUAL Implementation/Behavior
In the realm of C++, a struct is truly nothing but a default-public class. There is no special logic or considerations a compiler takes into account other than that.

Expected/Accepted Use-Cases
While a struct may actually just be a default-public class in C++, I think that kind of sketchy, sloppy usage of when to use a struct vs class is really shitty. I draw a very clear-cut distinction between when to use one versus the other in C++, and this is a distinction that's actually (awesomely) enforced within C#, which reinforces my opinion on it being proper: A class is to be used for modeling an "object" with member functions, encapsulation, and all of the OO goodness. A struct is to be a used as a simple aggregate data-type (as in C) that does not need encapsulation or any of that.

There are plenty of scenarios where fully encapsulating a POD object is retarded overkill. Maybe a 2D Vector struct. Maybe a structure simply used to proxy variables to a function without needing to take a million arguments. Maybe a private struct defined within a class for some internal data storage... These are all scenarios where I prefer to use a struct to make this distinction quite clear. It's not arbitrary at all.

I highly discourage using structs the same way you would use classes. I think the determining factor in whether to choose a class or a struct for storage should be whether you wish to encapsulate the data or not.
Post Reply