I don't know why I get this error. It's like a*a
treats the a*
as a pointer (float*
to float*
is it the same as float
to float
). Apparently the problem is kind of resolved when I declare a
in the sub classes, but the point is that I want my sub classes automatically to have that from my parent class.
One more thing when i add "float a;" to every subclass the program can run. So i did that just to test if it would work but no. The float a doesn't get a value in setA. I just added cout<<a; after a=b;.
#include"Oblik.h"
#include<cmath>
using namespace std;
class Jednakostranicni : public GeometrijskaFigura{
float P(){
return (a*a*sqrt(3))/4; //12 13 C:\Users\Name\Desktop\prvi.cpp [Error] invalid operands of types 'float*' and 'float*' to binary 'operator*'
}
float O(){
return a+a+a; //15 12 C:\Users\Name\Desktop\prvi.cpp [Error] invalid operands of types 'float*' and 'float*' to binary 'operator+'
}
};
class Kvadrat : public GeometrijskaFigura{
float P(){
return a*a;//23 12 C:\Users\Name\Desktop\prvi.cpp [Error] invalid operands of types 'float*' and 'float*' to binary 'operator*'
}
float O(){
return a+a+a+a;//26 12 C:\Users\Name\Desktop\prvi.cpp [Error] invalid operands of types 'float*' and 'float*' to binary 'operator+'
}
};
class Krug : public GeometrijskaFigura{
float P(){
return a*a*3.14;//34 12 C:\Users\Name\Desktop\prvi.cpp [Error] invalid operands of types 'float*' and 'float*' to binary 'operator*'
}
float O(){
return 2*a*3.14;//37 12 C:\Users\Name\Desktop\prvi.cpp [Error] invalid operands of types 'int' and 'float*' to binary 'operator*'
}
};
int main(){
GeometrijskaFigura *f;
int x;
cin>>x;
f->setA(x);
f=new Jednakostranicni;
cout<<"Jednakostranicni-P: "<<f->P()<<" O: "<<f->O()<<endl;
f=new Kvadrat;
cout<<"Kvadrat-P: "<<f->P()<<" O: "<<f->O()<<endl;
f=new Krug;
cout<<"Krug-P: "<<f->P()<<" O: "<<f->O()<<endl;
return 1;
}
// this is Oblik.h file code
#include<iostream>
using namespace std;
class GeometrijskaFigura{
protected: float a;
public:
void setA(float b){
a=b;
}
virtual float P()=0;
virtual float O()=0;
};