944,198 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1887
  • C++ RSS
Jul 6th, 2005
0

how i can write this program

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shahid is offline Offline
13 posts
since May 2005
Jul 6th, 2005
0

Re: how i can write this program

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 .]
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jul 6th, 2005
0

Re: how i can write this program

C++ Syntax (Toggle Plain Text)
  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 >>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jayrajput is offline Offline
3 posts
since Jun 2005
Jul 6th, 2005
0

Re: how i can write this program

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jayrajput is offline Offline
3 posts
since Jun 2005
Jul 6th, 2005
0

Re: how i can write this program

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]
C++ Syntax (Toggle Plain Text)
  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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jul 6th, 2005
0

Re: how i can write this program

Correct Dave
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jayrajput is offline Offline
3 posts
since Jun 2005
Jul 7th, 2005
0

Re: how i can write this program

sorry (i have posted a message in the wrong thread) (i have to learn using tabs in firefox :o )
Reputation Points: 11
Solved Threads: 6
Junior Poster
CrazyDieter is offline Offline
106 posts
since Jul 2005
Jul 8th, 2005
0

please someone help me urgent

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:

C++ Syntax (Toggle Plain Text)
  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 >>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shahid is offline Offline
13 posts
since May 2005
Jul 8th, 2005
0

Re: please someone help me urgent

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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jul 8th, 2005
0

Re: please someone help me urgent

Threads merged.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Double Arrays HELP!!!!!!
Next Thread in C++ Forum Timeline: Linking Error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC