Hello there,
I've been programming for a while now, in various languages. But now I've decided to do it properly and learn C++.
Here's my problem, I have a class called Linear which has public and protected attributes. And I have another class - Quadratic - which inherits from Linear.
It seems to me that Quadratic is not able to access the protected attributes from Linear. (So I think anyway). The errors I get are as follows:
My classes are as follows:C:\Users\******\C++\EquationSolver\Linear.cpp In function `int main()':
22) C:\Users\******\C++\EquationSolver\Linear.cpp `dComplex Linear::root1' is protected
58) C:\Users\******\C++\EquationSolver\Quadratic.cpp within this context
17 C:\Users\******\C++\EquationSolver\Quadratic.cpp `dComplex Quadratic::root2' is protected
59) C:\Users\******\C++\EquationSolver\Quadratic.cpp within this context
C:\Users\******\C++\EquationSolver\Makefile.win [Build Error] [Quadratic.o] Error 1
In Linear.cpp:
In Quadratic.cpp:Code:#include <iostream> #include <complex> using namespace std; typedef complex<double> dComplex; class Linear { public: Linear(); // Constructor ~Linear(); // Destructor void setA ( float value ); float getA(); void setB ( float value ); float getB(); dComplex getRoot1(); virtual void solve(); protected: float a; float b; dComplex root1; }; // Linear::Linear(){} // Linear::~Linear(){} void Linear::setA(float value) { a = value; } void Linear::setB(float value) { b = value; } float Linear::getA() { return a; } float Linear::getB() { return b; } void Linear::solve() { root1 = -b / a; } dComplex Linear::getRoot1() { return root1; }
Thanks for your time.Code:#include <complex> #include <iostream> #include <cmath> #include "Linear.cpp" using namespace std; typedef complex<double> dComplex; class Quadratic : public Linear{ public: float getC(); void setC(float value); dComplex getRoot2(); void solve(); float disc; protected: dComplex root2; float c; }; float Quadratic::getC() { return c; } void Quadratic::setC(float value) { c = value; } dComplex Quadratic::getRoot2() { return root2; } void Quadratic::solve() { disc = b*b - 4*a*c; if(disc >= 0) { root1 = (-b + sqrt(disc)) / (a*2); root2 = (-b - sqrt(disc)) / (a*2); } else { float rePart, imPart; rePart = -b / (2 * a); imPart = sqrt((4 * a * c) - (b * b)) / (2 * a); root1 = (rePart, imPart); root2 = (rePart, -imPart); } } int main(){ Quadratic qSolve; float aVal, bVal, cVal; cout<<"Enter value for A: "; cin>>aVal; cin.ignore(); cout<<"Enter value for B: "; cin>>bVal; cin.ignore(); cout<<"Enter value for C: "; cin>>cVal; cin.ignore(); qSolve.setA(aVal); qSolve.setB(bVal); qSolve.setC(cVal); qSolve.solve(); cout<<"Root1 = "<<qSolve.root1<<"\n"; cout<<"Root2 = "<<qSolve.root2<<"\n"; cout<<"\nPress Enter to exit..."; cin.get(); }
P.S. If you have any tips/criticisms for me, please let me know! I only started today.



LinkBack URL
About LinkBacks


