how to write addition program with c++ using class

#include<iostream.h>
using namespace std;
class adi
{
public:
int a,b;
};
int main()
{


adi.add1;
adi.add2;

int sum;


adi1.a=10;
adi1.b=20;


adi2.a=56;
adi2.b=4;


sum=adi1.a+adi1.b;
cout<<"Sum of two numbers in adi1:"<<sum<<endl;

sum=adi2.a+adi2.b;
cout<<"Volume of box2:"<<sum<<endl;
}


wany to know what is wrong with this code pls help me
closed account (D80DSL3A)
I think you're confusing the name of the class with the name of an instance of the class.
In this code int sum; you declare an integer variable named sum.
This is correct. The data type is 'int' and 'sum' names the variable.

The class usage is similar. The data type is 'adi' and the variable names are 'add1' and 'add2'. Here is the correct usage:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class adi
{
public:
int a,b;
};

int main()
{
adi add1;// declare an instance of an adi variable named add1
adi add2;

int sum;// declare an instance of an integer variable named sum

add1.a=10;
add1.b=20;

adi2.a=56;
adi2.b=4;

sum=add1.a+add1.b;

Likewise for the remaining code.
Last edited on
tnks yar.....
closed account (D80DSL3A)
Are you having a seizure? I could call 911 for you.
I give you en example. Both do the same.. First without class
#include <iostream>
#include <cmath>
using namespace std;

void factor(int n);
void nextPrime(int n);
bool primeNumber(int n);
int minprime = 2;

int main()

{
int userInput;

cout << "Please enter a number: ";
cin >> userInput;

if (primeNumber(userInput))

cout << "The number is prime \n";

else

cout << "\nThe number is not prime\n";
cout << "Factors: \n";
factor(userInput);
cout << "\nThe next prime is: ";
nextPrime(userInput);

cout << endl;

system ("Pause");


return 0;
}


bool primeNumber(int n)
{
for (int i = minprime; i <= sqrt(double(n)); i++)
if (n % i == 0)
return false;
return true;
}

void factor(int n)
{
int limit = sqrt(double(n));
for (int i = minprime; i <= limit; i++)
if (n % i == 0)
{
cout << i << ", ";
factor(n / i);
return;
}
cout << n;

}

void nextPrime (int n)
{
int nextnumber = ++n;

while(!primeNumber(nextnumber))
{
++nextnumber;
}

cout << nextnumber;
}


//with class

#include <iostream>
#include "PrimeNUMBER.h"
using namespace std;

int main()
{

PrimeNUMBER eNumber;
int n;
cout << "Enter a number: ";
cin >> n;
eNumber.setNumber(n);
eNumber.checkNumber(n);

system("Pause");

return 0;
}

//class

class PrimeNUMBER
{
public:
PrimeNUMBER(void);
~PrimeNUMBER(void);
void setNumber(int n) { Liczba = n; }
bool ifPrimeNumber(int n);
void factor(int n);
void nextPrime(int n);
void userPrime(int n);
void checkNumber (int n);

private:
int Liczba;
};

// cpp

#include "StdAfx.h"
#include "PrimeNUMBER.h"
#include <iostream>
#include <cmath>
using namespace std;
int minNumber = 2;


PrimeNUMBER::PrimeNUMBER(void)
{
}


PrimeNUMBER::~PrimeNUMBER(void)
{
}

bool PrimeNUMBER::ifPrimeNumber(int n)
{
int limit = sqrt(double(n));
for (int i = minNumber; i <= limit ;i++ )
{
if (n % i == 0)
return false;
}
return true;

}

void PrimeNUMBER::factor(int n)
{

int limit = sqrt(double(n));

for (int i = minNumber; i <= limit; i++)

if (n % i == 0)
{
cout << i << ", ";
factor(n / i);
return;
}
cout << n;

}


void PrimeNUMBER::nextPrime (int n)
{
int nextNumber = ++n;
while (!ifPrimeNumber(nextNumber))
{
++nextNumber;
}
cout << "\nNext prime number: " << nextNumber;
}

void PrimeNUMBER::userPrime(int n)
{
if (ifPrimeNumber(n))
cout << "The number is prime.\n";

else
cout << "The number is not prime.\n";
}

void PrimeNUMBER::checkNumber(int n)
{
userPrime (n);
cout << "\nFactors: ";
factor(n);
cout << endl;
nextPrime(n);
cout << endl;
}




Last edited on
closed account (Dy7SLyTq)
also, your using the class as an object
Static or not, that is the question :-)
Topic archived. No new replies allowed.