473,320 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Operator overloading in c++

Ajay Bhalala
119 64KB
Hello weaknessforcats,
Thanks for your response.

The discussion thread was closed, so I have Start this new discussion.

As in the following program, I have do the addition of two matrix same as I want to do the addition of two integer number.

Expand|Select|Wrap|Line Numbers
  1. #include<iostream.h>
  2. #include<conio.h>
  3. class matrix
  4. {
  5.  int a[3][3],i,j,k;
  6.  public:
  7.   void getdata();
  8.   void putdata();
  9.   matrix operator +(matrix);
  10. };
  11. void matrix::getdata()
  12. {
  13.  cout<<"\n Enter the matrix :- ";
  14.  for(i=0;i<3;i++)
  15.  {
  16.   for(j=0;j<3;j++)
  17.   {
  18.    cin>>a[i][j];
  19.   }
  20.  }
  21. }
  22. void matrix::putdata()
  23. {
  24.  for(i=0;i<3;i++)
  25.  {
  26.   for(j=0;j<3;j++)
  27.   {
  28.    cout<<"\n Matrix is :- "<<a[i][j];
  29.   }
  30.  }
  31. }
  32. matrix matrix::operator +(matrix m)
  33. {
  34.  matrix m1;
  35.  for(i=0;i<3;i++)
  36.  {
  37.   for(j=0;j<3;j++)
  38.   {
  39.    m1.a[i][j]=a[i][j]+m.a[i][j];
  40.   }
  41.  }
  42.  return m1;
  43. }
  44. void main()
  45. {
  46.  matrix m2,m3,m4;
  47.  clrscr();
  48.  m2.getdata();
  49.  m3.getdata();
  50.  m4=m2+m3;
  51.  cout<<"\n Addition of matrix is ...";
  52.  m4.putdata();
  53.  getch();
  54. }
And after the doing the addition of two integer numbers, I also want to find out the average of two integer numbers of which we find the addition.


I have tried to addtion, and I have written my code as follow:

Expand|Select|Wrap|Line Numbers
  1. #include<iostream.h>
  2. #include<conio.h>
  3. class add
  4. {
  5.  int a,b;
  6.  public:
  7.   void getdata();
  8.   void putdata();
  9.   add operator +(add);
  10. };
  11. void add::getdata()
  12. {
  13.  cout<<"\n Enter the Marks of 1st Subject :- ";
  14.  cin>>a;
  15.  cout<<"\n Enter the Marks of 2nd Subject :- ";
  16.  cin>>b;
  17. }
  18. void add::putdata()
  19. {
  20.  cout<<"\n Marks of Subject1 is :- "<<a;
  21.  cout<<"\n Marks of Subject2 is :- "<<b;
  22. }
  23. add add::operator +(add x)
  24. {
  25.  return a+b;
  26. }
  27. void main()
  28. {
  29.  add x;
  30.  clrscr();
  31.  cout<<"\n Addition is :- ";
  32.  x.putdata();
  33.  getch();
  34. }
But there is error occured.
Error : Error CPP\3.CPP 25: Cannot convert 'int' to 'add'

and 1 warning also occured
Warning :Warning CPP\3.CPP 26: Parameter 'x' is never used

What changes I have to do in my second program?
Dec 31 '14 #1
2 1285
weaknessforcats
9,208 Expert Mod 8TB
This code:

Expand|Select|Wrap|Line Numbers
  1. void main()
  2. {
  3.  add x;    <------------------------
  4.  clrscr();
  5.  cout<<"\n Addition is :- ";
  6.  x.putdata();
  7.  getch();
  8. }
has "add" as a type. There is no such type so the compiler generates an error. Because of that error, the variable x is never created and that causes the warning about the variable x is never used.

If you are going to use arrays, please read: http://bytes.com/topic/c/insights/77...rrays-revealed

and pay attention to the last example in particular.
Dec 31 '14 #2
Ajay Bhalala
119 64KB
I have read your article, It's very nice. I get lots of information from your article. And now I have got what I have to do. Thank you so much for your help. I can complete my program due to you. Thank you so much for your help.
Jan 1 '15 #3

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

Similar topics

16
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class...
51
by: Jojo | last post by:
Is there any way to get to the left-hand side of an operator? Consider the following (this is not meant to be perfect code, just an example of the problem): class Matrix { public: int data;...
7
by: Eckhard Lehmann | last post by:
Hi, I try to recall some C++ currently. Therefore I read the "Standard C++ Bible" by C. Walnum, A. Stevens and - of course there are chapters about operator overloading. Now I have a class...
13
by: Jon Cosby | last post by:
VB .Net does support operator overloading, doesn't it? It seems like this should overload binary addition, but VB doesn't recognize "Operator" Public Shared Operator +(ByVal c1 as cnum, ByVal c2...
2
by: vivekian | last post by:
Have a pointer to an object of class type task task * A ; Now this object takes operator overloading like A<<2 ; which assigns the number 2 to one of the members of the object A.
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
2
by: rn5a | last post by:
I use VB.NET to create ASP.NET apps. If I am not wrong, there is something called method overloading in VB.NET (like in C#) which is one of the features in OOP (polymorphism) but does VB.NET also...
8
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
4
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
Operator overloads are just like any other member function, you can make them do whatever you want. However, of course, we might expect them to behave in a certain way. The ++ operator should...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.