Some simple OOP help please

This is a discussion on Some simple OOP help please within the C++ Programming forums, part of the General Programming Boards category; Hello there, I've been programming for a while now, in various languages. But now I've decided to do it properly ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    10

    Some simple OOP help please

    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:
    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
    My classes are as follows:
    In Linear.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;
    }
    In Quadratic.cpp:
    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();
    }
    Thanks for your time.

    P.S. If you have any tips/criticisms for me, please let me know! I only started today.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    The problem is in your main() function. You accessed the member variables directly:
    Code:
        cout<<"Root1 = "<<qSolve.root1<<"\n";
        cout<<"Root2 = "<<qSolve.root2<<"\n";
    You should call the member functions instead:
    Code:
        cout<<"Root1 = "<<qSolve.getRoot1()<<"\n";
        cout<<"Root2 = "<<qSolve.getRoot2()<<"\n";
    By the way, instead of including a source file, separate the definition of your classes from their implementation by placing the definition in a header file. Then you include headers, not source files.

    After you have done that, read:
    When should my destructor be virtual?
    Const correctness
    Should I use using namespace std in my code?
    Last edited by laserlight; 02-16-2008 at 10:29 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    10
    Ah silly me! I set up the public methods and don't even use them!

    I'm still getting an error though, apparently I have multiple definitions of all the methods in Linear. Do you know why this is? Is it because I'm not importing a header file instead of the whole source?

    Thank you for the links by the way. Looks like a very usefull resource.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    I'm still getting an error though, apparently I have multiple definitions of all the methods in Linear. Do you know why this is? Is it because I'm not importing a header file instead of the whole source?
    Probably. The fix is to place the class definitions and function declarations in the respective headers, use header inclusion guards, and then implement the classes and free functions in the respective source files.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    10
    OK, thank you again. I have no idea how to set up a header file haha. I'll go look it up somewhere.

    Cheers.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    I will be nice and help you write "Quadratic.h":
    Code:
    #ifndef QUADRATIC_H_
    #define QUADRATIC_H_
    
    #include "Linear.h"
    
    class Quadratic : public Linear {
    public:
        float getC() const;
        void setC(float value);
        dComplex getRoot2() const;
        void solve();
    protected:
        dComplex root2;
        float c;
    };
    
    #endif
    Notice that it only includes necessary headers, and does not use the using directive. I also removed the public disc member variable as it looks like it should be a local variable instead. Oh, and I made the member functions const correct, so you will have to reflect that in the source file.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    10
    Ah thank you very much. A contextual example really is the best thing to learn from. My application looks much more neat and elegant now. I've even got another class inheriting from Quadratic.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 05:26 PM
  2. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 10:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 07:52 AM

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