-2

I am a C++ learner... I created a simple program wherein I created a constructor and two functions.. my problem is when I create an object and call a function the constructor is not called (edit:solved!).. also how can i call the add function with read function( so that after taking input from the user the program calls the add function and adds the numbers?)

edit:solved! constructor now works as intended, figured how to make another function call :)

    #include<iostream>
using namespace std;
class class1
{
    int x,y;
    public:
    class1()
    {
        cout<<"object created\n";
    }
    void read()
    {
        cout<<"enter x";
        cin>>x;
        cout<<"enter y";
        cin>>y;
    }
    int add(int a, int b) //int add()
    {
        int sum=a+b; // int sum=x+y
        cout<<"\nsum=" << sum; // return sum
    }
};
int main()
{
    class1 obj1;
    obj1.read();
    onj1.add(); //this was missing and it didnt work prevuiously

}
inc.mak
  • 43
  • 1
  • 7
  • I ran your code and the constructor was called. I saw the message "object created" twice. What do you see? – john Jul 22 '18 at 06:37
  • I don't see object created.. what about the other issue? edit: i can see the object created message now.. don't know what happened earlier – inc.mak Jul 22 '18 at 06:40
  • Cannot reproduce. Do you remember to save file and compile before executing the program? –  Jul 22 '18 at 06:40
  • @NickyC what about the function call.. how to do it? – inc.mak Jul 22 '18 at 06:44
  • Your "issue #2": Ummm... Just like how you call `obj1.read()`? (The return statement in `add` is missing.) –  Jul 22 '18 at 06:45
  • @NickyC after making obj1.read() call the console asks me for input now i want it to add those inputs.. i dont think return statement is needed in read().. pardon me if i am wrong – inc.mak Jul 22 '18 at 06:50
  • I repeat: The return statement in `add` is missing. –  Jul 22 '18 at 06:51

3 Answers3

1

Now that your values are stored in x and y, you can add the values like below:

int add() {
  int sum=x+y;
  cout<<"\nsum=" << sum; 
}

And call this method from main() after read method

obj1.add();
Bharath Kumar
  • 541
  • 3
  • 10
0

Maybe part of the problem here is that your class is not very meaningful. You don't need a class to add two numbers. But given your code you would write something like

obj1.add(10, 20);

and the output wuould be

sum=30

However what I expect you really want to happen is to add two numbers you have previously entered. In that case you need to rewrite your method to use x and y instead of a and b

int add()
{
    int sum=x+y;
    cout<<"\nsum=" << sum;
}

and then

int main()
{
    class1 obj1;
    obj1.read();
    obj1.add();
}

There's no need for obj2 in this code.

john
  • 85,011
  • 4
  • 57
  • 81
  • @inc.mak There's a list of books here https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list Can't make any personal recommendations as I learned C++ a long long time ago. C++ is a complex language and getting a book and doing some proper studying is definitely a good idea though. – john Jul 22 '18 at 07:00
0

A return statement is missing in add function. Just add a line return sum; to the end of your add function

HariUserX
  • 1,341
  • 1
  • 9
  • 17