0
#include<iostream.h>
#include<conio.h>
class twonum
{
private :
    int x,y;

public:
void getvalues()
{
    cout<<"enter any two numbers";
    cin>>x>>y;
}
void putvalues()
{
    cout<<"\n\n"<<x<<","<<y;
}
int add(int x,int y)
{
    return(x+y);
}
int multiply(int x,int y)
{
    return(x*y);
}
void swap(int x,int y)
{
    int c;
    c=x;
    x=y;
    y=c;
    cout<<x<<","<<y;
};
void main()
{
    clrscr();
    int k,j,l;
    twonum s;
    s.getvalues();
    s.putvalues();
    k=s.add(x,y);
    cout<<"addition:"<<k;
    j=s.multiply(x,y);
    cout<<"multiplication:"<<j;
    s.swap(x,y);
    getch();
}
4
Contributors
4
Replies
20
Views
7 Months
Discussion Span
Last Post by tinstaafl
1

Regarding conio.h, getch, clrscr, not everyone has conio.h. You might try changing iostream.h to iostream. "void main" should probably be "int main" and your main function should return 0.

Those "problems" might allow it to compile on your machine and not on others, but the actual errors are below.

  1. You are using cout without either the "using namespace std;" line at the top or putting std:: in front of cout.
  2. x and y are data members of your class and they are private too. If x and y were public, your program would compile if you replaced x with s.x and y with s.y However, since they are private, even that won't work.

Edited by AssertNull

1

Whoops, sorry RP, didn't see your post before posting. Good points.

Votes + Comments
You made good points as well. The code, well, it's just a code dump and run with many dangling unspoken questions.
0

Another thought, it looks to me like you're attempting to use the variables inside the class for your functions. In that case you can leave out the parameters and make the calculations based on the variables inside the class directly. This way they can stay private.

int add()
{
    return(x + y);
}

Edited by tinstaafl

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.