Question #185047

Create a class called ADDITION has one integer and one float data member. Use pointer to 

data member to find their sum and print it from main method and also use the concept of 

dynamic constructor.


Expert's answer

#include <iostream>
using namespace std;
class Addition{
    int *x;
    float *y;
    public:
    Addition(int x, float y){
        this->x = &x;
        this->y = &y;
    }
    float sum(){
        return *x + *y;
    }
};
int main(){
    int x = 3;
    float y = 4.5;
    Addition* add = new Addition(x, y);
    cout<<add->sum();
    return 0;
}
LATEST TUTORIALS
APPROVED BY CLIENTS