Hey guys, need some help. I'm new to the concept of abstract classes, and am getting the error 'lvalue require as left operand.." I can't seem to figure out where the compiler is running into the problem. Here's the base abstract class and one of the derived classes:
This is different from c# properties , the method getarea is a getter that means it simply returns a value and does not operate on it like a setter. In your case create setarea(int value ) and setvol(float value ). Also you should add the keyword const at the end of the method like so : int getarea() const , which means your method does not modify an attribute of the class.
How about the second code I posted? I think that solves the problem. Cause, I think for every class that is derived from figure, a copy of area and volume are created, right? I want to minimize unnecessary variable usage, and I guess my method achieves it?
But what you said seems interesting, code you show me an example?
What you did is breaking encapsulation. Yes it works, for educational purposes it might pass but in enterprise I would kill the guy who let any client access any account , if you what I mean. There is keyword call property , which is not really used in c++ but achieve the same purpose. When I examine your code I see that only your derived class access to that method so instead you should put int area and float volume as protected.
Oh okay, yeah I get what you mean, you're saying instead of declaring functions to get the area and volume I should just directly put area and volume variables into protected?
There is keyword call property , which is not really used in c++ but achieve the same purpose.
// declspec_property.cpp
struct S {
int i;
void putprop(int j) {
i = j;
}
int getprop() {
return i;
}
__declspec(property(get = getprop, put = putprop)) int the_prop;
};
int main() {
S s;
s.the_prop = 5;
return s.the_prop;
}
where the_prop is a property , which mean acts like a setter and getter for int i , in this example . In c++ we use instead accessor and mutator -> getter and setter to get or set an attribute.
To answer to your first question , attributes should be protected , since you wont need to call method in your derived class , and you put a setter and getter for each of the attribute as public (if you want).