functions and inheritance

I havent coded this yet so I dont have anything to show, I am trying to figure out how it would work first.

I am trying to work out how my functions will work in different classes that inherit from each other.

First I have a USER class which I think is called the parent class, which the other classes inherit from.

Next I have a GUEST class, and this class as i said inherits from the USER class, this class has a function called CREATEACCOUNT, and this CREATEACCOUNT function makes use of another function called ENCRYPT

The next class is called REG_USER, and this class also inherits from the USER class, this class has a function called LOGIN, and this LOGIN, function also makes use of the ENCRYPT function, the same one that CREATEACCOUNT uses

So im wondering how do I set this up. At first i was just going to code 2 encrypt functions in each of the classes, but that seems like it would be bad coding.

Do i put the encrypt function into the USER class and let it work through the inheritance?

Or is there another way I am unfamiliar with?
a class should represent something and you should be able to make a mini conversation about it that is coherent.
Yes, that is my car, it has a radio and a v6 engine and can go from point at to point b and cooks waffles. Wait, what? Maybe void cookwaffles() should not be in the car class just because it was handy to inherit it into the waffleiron class. Hmm.

It sounds like encryption should either be simple / stand alone method (no class at all, inputs of something like a string and a public key or whatever) or its own class (could be for various reasons like construct with the public key and just pass in a string, or a running encryption of distinct streams where you need multiple streams at different places that need to be maintained rather than just a straight gigo function).

Back to the car analogy, inherited classes also tell a story, you may inherit that radio and engine and so on.
So tell the story. A guest is a user who has a password that is encrypted by a difficult to crack 1024 bit algorithm.
Or
A guest is a user. Users encrypt their own password as part of their everyday job description, using a pocket calculator. :P
I am trying to be a little funny here. But you get the point? Is encryption something the user DOES (method) or HAS (variable)? No. They HAVE an encrypted password, but know nothing about how it was scrambled (at least you hope not, as that is the first step to cracking it).
Last edited on
ok so that has helped me decided with what not to do, I am interested in the stand alone method you mentioned. I am wondering how that works. Is the function just in a cpp file by itself?
where you put it is another topic (how to organize code into many files) but yes, essentially.

eg
string encrypt(string input, uber_extra_longlong key); //note the lack of a classname:: here

then any or all of your classes can just call this function, long as they have the inputs.

have you never written a stand alone function, just went straight to objects?
I dont think I have come across standalone functions, my teaching has been very patchy. So My classes are over different cpp files, if I have this standalone function in its own cpp file, the functions in the classes will be able to access the standalone? I have started to code it up, so trying out this standalone version
Topic archived. No new replies allowed.