whats wrong with the above code;\Code:#include<iostream.h> #include<conio.h> class myclass { private: int a,b; public: add() { cin>>a; cin>>b; cout<<a+b; } }; int main() { myclass obj; obj.add(); return 0; }
This is a discussion on help ;p within the C++ Programming forums, part of the General Programming Boards category; Code: #include<iostream.h> #include<conio.h> class myclass { private: int a,b; public: add() { cin>>a; cin>>b; cout<<a+b; } }; int main() { ...
whats wrong with the above code;\Code:#include<iostream.h> #include<conio.h> class myclass { private: int a,b; public: add() { cin>>a; cin>>b; cout<<a+b; } }; int main() { myclass obj; obj.add(); return 0; }
1. Using non-standard headers iostream.h and conio.h -- just #include <iostream>
2. No return type declared for the add() method
baahahha thanks
forgot the type for ADD, that solved itCode:#include<iostream.h> #include<conio.h> class myclass { private: int a,b; public: void add() { cin>>a; cin>>b; cout<<a+b; } }; int main() { myclass obj; obj.add(); return 0; }thanks man ;x
You don't even need conio.h anyway.
Get rid of the .h after iostream ( As Brewbuck suggested ) and use std:: infront of cout and cin.
I'm just trying to be a better person - My Name Is Earl
Your function Add() does not do its thing: it does not only add two integer values, it also sets the private attributes with their values.
Why don't you split this in three functions? Two which set 'a' and 'b', and one who adds them.
This leads to a more understandable main program: we see what we do.
and indent your code