Author Topic: What's wrong with this c++ code?  (Read 984 times)

lachu

  • Newbie
  • *
  • Posts: 7
What's wrong with this c++ code?
« on: November 24, 2011, 03:43:45 pm »

What's wrong with this code?

Code:
//Source Code Listing

 
#include<iostream.h>
#include<conio.h>
 
class Ayush 
{
int other;
public:
Ayush operator=(Ayush c)
{
c.other=other;
return(c);
}
 
void getother(void)
{cout<<"Enter a number";
cin>>other;
}
 
void display(void)
{cout<<other;
}
};
 
void main()
{
Ayush a,b;
 
a.getother;
b=a;b.display;
getch();
}  

 

kathir

  • Administrator
  • Sr. Member
  • *****
  • Posts: 283
Re: What's wrong with this c++ code?
« Reply #1 on: November 24, 2011, 03:50:22 pm »

What's wrong with this code?



There 3 mistakes:
1. a.getother; should be changed to a.getother();
2. b.display; should be changed to b.display();
3. c.other = other; should be changed to other = c.other;

For better assignment operator syntax, refer to the link:
http://www.softwareandfinance.com/OOPs/Assignment_Operator.html


Corrected coding is:

Code:
//Source Code Listing

#include<iostream.h>
#include<conio.h>


class Ayush 
{
   int other;
public:
   Ayush operator=(Ayush c)
   {
      other = c.other;
      return(c);
   }
  
   void getother(void)
   {
      cout<<"Enter a number: ";
      cin>>other;
   }
  
   void display(void)
   {
      cout<<other;
   }
};
  
void main()
{
   Ayush a,b;
     
   a.getother();
   b=a;
   b.display();
   getch();
}  
« Last Edit: November 24, 2011, 03:52:44 pm by kathir »

 


Disclaimer: This web site is for educational and informational purposes only. Click here to read the Disclaimer.
Content copyright 2010-2014. Kathiresan. All rights reserved.
This page is using SMF version 2.0.1