addition in class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<iostream.h>
#include<conio.h>
class ad
{
private:
int n;
public:

void getdata()
{
cout<<"Enter the value of NUm";
cin>>n;
}
ad operator + (ad ob,ad ob1)
{
int v;
v=n+ob.n;
}
void show()
{

cout<<n;
}
};

void main()
{
ad num,num2,num1;
num.getdata();
num1.getdata();
num2=num+num1;
num2.show();
getch();
}


I'm tring to make a class where user can multipul addition (e.g 2+5+3)
without pressing enter in every value.
use only enter 5+8+6+10+15
after process ans show.

h
1)ob.n accessing private member
2)operator function should return a value
3)code:for VC++(convert to turbo for your use)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<iostream>
#include<conio.h>

using namespace std;

class ad
{
private:
int n;
public:

void getdata()
{
cout<<"Enter the value of NUm";
cin>>n;
}
ad operator + (ad ob)
{
ad v;
v.put(n+ob.get());
return v; 
}
void show()
{

cout<<n;
}
int get()
{
	return n;
}
void put(int a)
{
	n=a;
}
};

void main()
{
ad num,num3,num2,num1;
num.getdata();
num1.getdata();
num2.getdata();
num3=num+num1+num2;
num3.show();
_getch();
}

4)change your compiler to VC++.Turbo is old.(This is not related to problem)
(Not if you are in school and they want you to use Turbo)
Topic archived. No new replies allowed.