logo codesdope

Q2 Facing problem in below mentioned program

   ah19299

#include<iostream>
using namespace std;

class fruits{
    public:
        int numfruits;
        fruits(){
            numfruits = 0;
        }
        fruits(int numf){
            numfruits = numf;
        }
        void getFruits(int numf){
            numfruits = numf;
        }
        int setFruits(){
            return numfruits;
        }
};

class Apples: public fruits{
    public:    
        int numApp;
        Apples():fruits(){
            numApp = 0;
        }
        Apples(int numA):fruits(int numf){
            numfruits = numA;
        }
        void getApples(int numA){
            numApp = numA;
        }
        int setApples(){
            return numApp;
        }
};

class Mango: public fruits, public Apples{
    public:    
        int numMan;
        Mango():Apples(){
            numMan = 0;
        }
        Mango(int numB):Apples(numA),fruits(numf){
            numMan = numB;
        }
        void getMango(int numB){
            numMan = numB;
        }
        int setMango(){
            return numMan;
        }
};

int main(){
    fruits f;
    Apples a;
    Mango m;
    
    a.Apples::setApples(4);
    m.MangosetMango(6);
    cout<<"\nApples in Basket: "<<a.getApples();
    cout<<"\nMangoes in Basket: "<<m.getMango();
    f.fruits::setFruits(a.getApples()+m.getMango());
    
    cout<<"\nTotal fruits in Basket: "<<f.getFruits();
}
 


Answers

  •    Aakhya Singh

    There are a few changes you need to make in your code.

    • You have interchanged the names – getFruits – setFruits, getApples – setApples and getMango – setMango while defining them in their respective classes.
    • By writing the following
    Apples(int numA):fruits(int numf)
    

    you want to pass the argument numA passed to the constructor of Apples to the of fruits using initialization list. For this, the value is passed without any type. So, remove the type int and pass the value numA to the constructor of fruits as shown below.          

    Apples(int numA):fruits(numA)
    

    Similarly, the initialization list in the class Mango will be as shown below.

    Mango(int numB):Apples(numB),fruits(numB)
    

    Note: In your code, these initialization lists are not required.

    I have written the working code below.

    #include<iostream>
    using namespace std;
    
    class fruits{
        public:
            int numfruits;
            fruits(){
                numfruits = 0;
            }
            fruits(int numf){
                numfruits = numf;
            }
            void setFruits(int numf){
                numfruits = numf;
            }
            int getFruits(){
                return numfruits;
            }
    };
    
    class Apples: public fruits{
        public:    
            int numApp;
            Apples():fruits(){
                numApp = 0;
            }
            Apples(int numA):fruits(numA){
                numApp = numA;
            }
            void setApples(int numA){
                numApp = numA;
            }
            int getApples(){
                return numApp;
            }
    };
    
    class Mango: public fruits, public Apples{
        public:    
            int numMan;
            Mango():Apples(){
                numMan = 0;
            }
            Mango(int numB):Apples(numB),fruits(numB){
                numMan = numB;
            }
            void setMango(int numB){
                numMan = numB;
            }
            int getMango(){
                return numMan;
            }
    };
    
    int main(){
        fruits f;
        Apples a;
        Mango m;
        
        a.Apples::setApples(4);
        m.setMango(6);
        cout<<"\nApples in Basket: "<<a.getApples();
        cout<<"\nMangoes in Basket: "<<m.getMango();
        f.fruits::setFruits(a.getApples()+m.getMango());
        
        cout<<"\nTotal fruits in Basket: "<<f.getFruits();
    }
    

     



Write your answer
Close

Welcome.please sign up.

Close

Welcome.please login.