468,497 Members | 2,016 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,497 developers. It's quick & easy.

Program to add two numbers by using classes

Expand|Select|Wrap|Line Numbers
  1. class class1
  2. {
  3. public : void sum1(int,int);
  4. };
  5.  
  6. void class1::sum1(c,d)
  7. {
  8. int c,d;
  9. int sum;
  10. sum=c+d;
  11. cout<<"sum="<sum;
  12. }
  13. void main()
  14. {
  15. int a=1,b=2;
  16. class1 p;
  17. p.sum1(a,b);
  18. getch();
  19. }
Check errors in it and reply
Nov 16 '07 #1
3 16989
sicarie
4,677 Expert Mod 4TB
There are a few errors in it that I can see, but I'm not here to do your work for you. I'm here to help you understand it, so you can do it yourself.

With that in mind, what did you get? What do you think are the errors in there? (Hint: running this through a compiler will probably help show errors...)
Nov 16 '07 #2
u can do this by declaring datamembers in class only and then used these data members in the member funtion to get the desired result.
there few other errors in code,see by your ownself.
Nov 17 '07 #3
Dear,
You must type blew code instead of your code:
Expand|Select|Wrap|Line Numbers
  1.       class class1
  2.       {
  3.       public : void sum1(int,int);
  4.       };
  5.  
  6.       void class1::sum1(int C,int D)  //Note here
  7.       {
  8.       int sum;
  9.       sum=C+D;
  10.       cout<<"sum="<sum;
  11.       }
  12.       void main()
  13.       {
  14.       int a=1,b=2;
  15.       class1 p;
  16.       p.sum1(a,b);
  17.       getch();
  18.       }
Nov 17 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Nirjhar Oberoi | last post: by
3 posts views Thread by apedosmil8 | last post: by
2 posts views Thread by =?Utf-8?B?d2lubGlu?= | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.