Abstract Class Problems

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

// Base Class 

class figure
{
    int area;
    float volume;
public:
    void display () { cout<<"\n Area : "<<area<<"\n Volume : "<<volume; }
    virtual void calc()=0;
protected:
   virtual void getv()=0;
    int getarea() { return area;   }
    float getvol()  { return volume; }
};


//derived class


class circle : public figure
{
    int radius;
    void getv() { cout<<"\n Enter the radius : "; cin>>radius; }
   void calc() { getv(); getarea()=4*3.14*radius*radius; getvol()=(4/3)*3/14*radius*radius*radius; display();  }
};


Any help/information/insight would be appreciated. Thanks!
Silly mistake, the getarea and getvol functions have to be passed by reference

Edited code :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

class figure
{
    int area;
    float volume;
public:
    void display () { cout<<"\n Area : "<<area<<"\n Volume : "<<volume; }
    virtual void calc()=0;
protected:
   virtual void getv()=0;
    int    &getarea() { return area;   }
    float  &getvol()  { return volume; }
};

class circle : public figure
{
    int radius;
    void getv() { cout<<"\n Enter the radius : "; cin>>radius; }
    void calc() { getv(); getarea()=4*3.14*radius*radius; getvol()=(4/3)*3.14*radius*radius*radius; figure::display();  }
};
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.

Did this help?
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.


Could you elaborate further?
This is an example found at this link

https://msdn.microsoft.com/en-us/library/yhfk0thd.aspx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 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).

Was that elaborated enough ?
Last edited on
Please note that attribute specifier "property" is not a part of C++ and only supported by Visual Studio.
I should have mentionned that ..my bad
Oh okay, though, according to what MiiNiPaa said, I wouldn't be able to use this in say Code::Blocks, ie, C++ in general? :l Is there any alternative?

(ps- sorry I took a long time to reply, had exams going on..)
Just make the member variables protected and use them directly. So why not? Dont't produce overhead.

This property stuff is rather nonsense. Why would you want declare a pseudo variable?
Topic archived. No new replies allowed.