please someone help me urgent

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2005
Posts: 13
Reputation: shahid is an unknown quantity at this point 
Solved Threads: 0
shahid shahid is offline Offline
Newbie Poster

how i can write this program

 
0
  #1
Jul 6th, 2005
please someone give me some idea to write this program

Write a C++ program that has class

1) math
Math class has only one data member number and member function display that will display the data member number.
Write the constructor of your math class that will initialize the data member number with the value zero.
Program will overload the following operators.
1. Plus +
2. Minus -
3. Multiplication *
After overloading these three operators program will be able to add, subtract and multiply your math class object with the integer in main() after overloading these operators you should be able to write these statements in main().
math obj1, obj2;
obj1= obj2 + 10;
Above statement will call the member function operator + () and will add the 10 in the data member of obj2 and finally will return the math class object. Similarly you also overload the multiplication operator * and minus operator – so that your math class object will be able to multiply and subtract from integer values.

Also your plus + overloaded operator should be intelligent enough to accommodate the following statement in the main() function.

math obj1, obj2;
obj2= 10 + obj1;

for this you will have to write friend function that will be called automatically and will add the integer value 10 in the data member of obj1 and finally will return the math class object . Similarly write two more friend functions that will overload the multiplication and subtraction operator so that you will be able to write the following statement in the main() function.

math obj1, obj2;
obj2= 10 *obj1;
obj2= 10 - obj1;



Your output should be similar to the following:

Sample output 1:


adding integer 10 in the object using statement: obj= obj + 10 ;
10
adding integer 10 in the object using statement: obj= 10 + obj;
20
Multiplying object with integer 20 using statement: obj= obj * 20 ;
400
Multiplying integer 20 with object using statement: obj= 20 * obj ;
8000
Subtracting 20 from object using statement: obj= obj - 20 ;
7980
Subtracting object from 10 using statement: obj= 10 - obj ;
-7970
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 254
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: how i can write this program

 
0
  #2
Jul 6th, 2005
Follow the instructions as best you can to begin developing some code. When you get stuck, post the code you have (within [code][/code] tags) and any error messages you get; then you can ask a more specific question. [And "I don't even know where to begin" is not the kind of post that will get much attention --please read the Announcement -- especially when the instructions provide many clues .]
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 3
Reputation: jayrajput is an unknown quantity at this point 
Solved Threads: 0
jayrajput jayrajput is offline Offline
Newbie Poster

Re: how i can write this program

 
0
  #3
Jul 6th, 2005
  1. #include<iostream>
  2.  
  3. class math
  4. {
  5.  
  6. public:
  7. math()
  8. {
  9. i= 0;
  10. };
  11. math(int i )
  12. {
  13. i = i;
  14. };
  15. ~math()
  16. {
  17. };
  18.  
  19. int operator+ ( int x)
  20. {
  21. return(x+i);
  22. };
  23.  
  24. int operator-(int x)
  25. {
  26. return(i -x );
  27. };
  28.  
  29. int operator*(int x)
  30. {
  31. return(i*x);
  32. };
  33.  
  34. friend int operator +( int x , math obj)
  35. {
  36. return( x+obj.i);
  37. };
  38.  
  39. friend int operator -( int x , math obj)
  40. {
  41. return(x - (obj.i) );
  42. };
  43.  
  44. friend int operator *( int x , math obj)
  45. {
  46. return(obj.i*x);
  47. };
  48. void Display()
  49. {
  50. cout << "i=" << i << endl;
  51. };
  52.  
  53.  
  54.  
  55. private:
  56.  
  57. int i;
  58.  
  59. };
  60.  
  61.  
  62. int main()
  63. {
  64.  
  65. math obj1, obj2, obj3;
  66. obj2= obj1 + 20 ;
  67. obj3 = 20 + obj1;
  68.  
  69. return 0;
  70.  
  71. }
<< moderator edit: added [code][/code] tags and indenting >>
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 3
Reputation: jayrajput is an unknown quantity at this point 
Solved Threads: 0
jayrajput jayrajput is offline Offline
Newbie Poster

Re: how i can write this program

 
0
  #4
Jul 6th, 2005
But I like the suggestion from Dave. We should first try to write the code and when we get stuck somewhere then we can post any errors which we are getting.

Thanks,
Jay
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 254
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: how i can write this program

 
0
  #5
Jul 6th, 2005
To grab from the standard namespace, you'll need to say so. For now, just do this to get cout and endl.
#include<iostream>
using namespace std;
This doesn't give me a warm fuzzy feeling.
   math(int i )
   {
      i = i;
   };
Perhaps do something like this so it's easier to figure out which is who. [edit]Function definitions don't end with a semicolon after the closing brace.[/edit]
  1. math(int init)
  2. {
  3. i = init;
  4. }
Here it would be a good idea to initialize obj1. [edit]Oops. C-mode. Forgot about default constructor.[/edit]
int main()
{

   math obj1, obj2, obj3;
   obj2= obj1 + 20 ;
   obj3 = 20 + obj1;

   return 0;

}
And don't forget to ask questions when you post your code.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 3
Reputation: jayrajput is an unknown quantity at this point 
Solved Threads: 0
jayrajput jayrajput is offline Offline
Newbie Poster

Re: how i can write this program

 
0
  #6
Jul 6th, 2005
Correct Dave
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 76
Reputation: CrazyDieter is an unknown quantity at this point 
Solved Threads: 3
CrazyDieter's Avatar
CrazyDieter CrazyDieter is offline Offline
Junior Poster in Training

Re: how i can write this program

 
0
  #7
Jul 7th, 2005
sorry (i have posted a message in the wrong thread) (i have to learn using tabs in firefox :o )
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 13
Reputation: shahid is an unknown quantity at this point 
Solved Threads: 0
shahid shahid is offline Offline
Newbie Poster

please someone help me urgent

 
0
  #8
Jul 8th, 2005
i write following program but my program does not run and not given required output. please someone check this and correct this or write this in better form.thanks. program and coding is given below:
program:
Write a C++ program that has class

1) math
Math class has only one data member number and member function display that will display the data member number.
Write the constructor of your math class that will initialize the data member number with the value zero.
Program will overload the following operators.
1. Plus +
2. Minus -
3. Multiplication *
After overloading these three operators program will be able to add, subtract and multiply your math class object with the integer in main() after overloading these operators you should be able to write these statements in main().
math obj1, obj2;
obj1= obj2 + 10;
Above statement will call the member function operator + () and will add the 10 in the data member of obj2 and finally will return the math class object. Similarly you also overload the multiplication operator * and minus operator – so that your math class object will be able to multiply and subtract from integer values.

Also your plus + overloaded operator should be intelligent enough to accommodate the following statement in the main() function.

math obj1, obj2;
obj2= 10 + obj1;

for this you will have to write friend function that will be called automatically and will add the integer value 10 in the data member of obj1 and finally will return the math class object . Similarly write two more friend functions that will overload the multiplication and subtraction operator so that you will be able to write the following statement in the main() function.

math obj1, obj2;
obj2= 10 *obj1;
obj2= 10 - obj1;



Your output should be similar to the following:

Sample output 1:


adding integer 10 in the object using statement: obj= obj + 10 ;
10
adding integer 10 in the object using statement: obj= 10 + obj;
20
Multiplying object with integer 20 using statement: obj= obj * 20 ;
400
Multiplying integer 20 with object using statement: obj= 20 * obj ;
8000
Subtracting 20 from object using statement: obj= obj - 20 ;
7980
Subtracting object from 10 using statement: obj= 10 - obj ;
-7970

Code:

  1. #include<iostream.h>
  2. class mth
  3. {
  4. friend class number;
  5. Public:
  6. class math():
  7. int (0){}
  8. void print member (){
  9. cout<<number<<endl;
  10. }
  11. operator float();
  12. object add (+);
  13. object sub(-);
  14. object multi (*);
  15. {
  16. //The implementation is object:add,sub,multi,}
  17. Private:
  18. i=10;
  19. };
  20. //friend functions
  21. friend float add both(class math,number class);
  22. friend float sub both(class math,number class);
  23. friend float multi both(class math,number class);
  24. };
  25. class number
  26. {Private:
  27. float value;
  28. public:
  29. number class()
  30. {
  31. value=10;
  32. }
  33. //friend functions
  34. friend float add both(class math,number class);
  35. friend float sub both(class math,number class);
  36. friend float multi both(class math,number class);
  37. };
  38.  
  39.  
  40. void int main()
  41. {
  42. mth obj1,obj2;
  43. obj1=obj2+10;
  44. obj2=10+obj1;
  45. obj2=10*obj1;
  46. char choice;
  47. cout<<"pleae enter one of the operator +,-,*:";
  48. if (choice=='+')
  49. {cout<<"adding integer 10 in the object using statement:";
  50. if (choice=='+')
  51. {cout<<"adding object 10 in the object using statement:";
  52.  
  53. if (choice=='*')
  54. {cout<<"multi object 20 in the object using statement:";
  55. if (choice=='*')
  56. {cout<<"multi integer 20 in the object using statement:";
  57. if (choice=='-')
  58. {cout<<"sub 20 from the object using statement:";
  59. if (choice=='-')
  60. {cout<<"sub obj from 10 using statement:";
  61. }
  62. system("PAUSE");
  63. getch();
  64. }
<< moderator edit: added [code][/code] tags >>
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,867
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: please someone help me urgent

 
0
  #9
Jul 8th, 2005
I suggest you actually read your C++ book before trying to write a program of any complexity. You have mistakes that should not be made by someone who has paid attention in class.
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 254
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: please someone help me urgent

 
0
  #10
Jul 8th, 2005
Threads merged.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 1696 | Replies: 9
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC