0

The code: https://pastebin.com/5QQpyWPH

#include <iostream>
#include <cmath>
#include <vector>
#include <iomanip>
long double func(long double x)
{
    return sin(x)-0.5*x;
}
 
long double dfunc(long double x)
{
return cos(x)-0.5;
}
 
class RootFinder{
    private:
    long double (*drf)(long double ,long double ,long double ,long double );
    long double (*f)(long double );
    long double (*df)(long double );
    long double root2(long double min, long double max)
    {
 
        long double x;
        x=(max+min)/2;
        long double y=x-f(x)/df(x);
        if (f(y)<pow(10,-20)&&f(y)>-1*pow(10,-20))
            return y;
        long double w=y-f(y)/df(y);
        return root2(w,w);
    }
    long double root1(long double min, long double max)
    {
        std::vector<long double> d(2);
        d.at(0)=min;
        d.at(1)=max;
        long double df=(max-min)/2;
        if(f(d.at(0)+df)==f(d.at(0)))
            return d.at(0);
        else if(f(d.at(1)-df)==f(d.at(1)))
            return d.at(1);
        if(  (f(d.at(0)+df)>0&&f(d.at(0))<0)  ||  (f(d.at(0)+df)<0&&f(d.at(0))>0) )
        {
            return root1(d.at(0),d.at(0)+df);
        }
        else
        {
            std::vector<long double>::iterator it;
            it=d.begin();
            d.erase(it);
            return root1(d.at(0)-df,d.at(0));
        }
        }
    public:
    RootFinder(){
                std::cout<<"black\n";
    }
    RootFinder( long double (*function)(long double )){
        std::cout<<"white\n";
        f=function;
    }
    RootFinder( long double (*function)(long double ), long double (*dfunction)(long double )){
        f=function, df=dfunction;
    }
    void setDf(long double (*dfunction)(long double )){
        df=dfunction;
    }
    long double callF(long double x){
        return f(x);
    }
    long double GetRoot2(long double min, long double max){
        return root2(min,max);
    }
    long double GetRoot1(long double min, long double max){
        return root1(min,max);
    }
 
 
};
class dRootFinder: public RootFinder{
    private:
    long double a, b, c;
    long double DrtFunc (long double x)
    {
        return a*pow(x,2)+b*x+c;
    }
    public:
    dRootFinder(long double A,long double B,long double C){
        a=A, b=B, c=C;
        RootFinder(DrtFunc);
    }
    std::vector<long double> droot(){
        std::vector<long double> x(2);
        if(pow(b,2)-4*a*c<0){
            std::cout<<"no roots\n";
            x.pop_back();
            x.pop_back();
            return x;
        }
        else if(pow(b,2)-4*a*c==0){
            x.pop_back();
            x.at(0)=-b/2/a;
            return x;
        }
        else{
            x.at(0)=(-b-pow((pow(b,2)-4*a*c),0.5))/2/a;
            x.at(1)=(-b+pow((pow(b,2)-4*a*c),0.5))/2/a;
            return x;
        }
    }
    std::vector<long double>GetDRoot(){
        return droot();
    }
};
 
int main()
{
    long double min=1, max=3;
   // long double x;
  //  std:: cin>>x;
 //   RootFinder func1(func,dfunc);
 
//    std:: cout<<func1.callF(x)<<std::endl;
 //   std:: cout<<std::setprecision(9)<<func1.GetRoot2(min,max)<<std::endl;
   // std:: cout<<std::setprecision(9)<<func1.GetRoot1(0,2)<<std::endl;
 
    dRootFinder drt1(1,0,0);
   // std::vector<long double> a=drt1.GetDRoot();
    //for(int i=0;i<a.size();i++)
   // std::cout<<a.at(i)<<std::endl;
 //   std:: cout<<std::setprecision(9)<<drt1.GetRoot1(-5,-0.5)<<std::endl;
  //  std:: cout<<drt1.callF(3)<<std::endl;
 
}

Problem lies in Line 89. As visible in line 57-58, when passed a function as an argument to a constructor, in the result window I should see "white". In line 89 I pass a function as an argument. The result window shows "black" twice, once after the default constructor is called when creating a subclass object, but the second time it got called since the constructor ignored(as it seems) the argument passed, and took it as calling a default constructor. Is the problem in the code or why did the constructor ignore the argument in line 89?

I tried passing different function, assuming it had to do something with that, such as func (line 5-8), same result.

  • 2
    `RootFinder(DrtFunc);` does not call the superclass's constructor. This constructs a new temporary object, then immediately destroys it and throws it away. You need to use the member initialization section correctly. See the duplicate question, and your C++ textbook, for more information. – Sam Varshavchik Nov 19 '22 at 17:28

0 Answers0