Classes and Objects

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

#include <iostream>

using namespace std;

class Calculator
{
public:
void printResult(double x, double y)
{
cout << "firstNumber + secondNumber = " << x + y << endl;

cout << "firstNumber - secondNumber = " << x - y << endl;

cout << "firstNumber * secondNumber = " << x * y << endl;

cout << "firstNumber/secondNumber = " << x / y << endl;


}
};

int main()
{
double firstNumber, secondNumber;
Calculator c;

cout << "Enter the first number: " << endl;


cin >> firstNumber;

cout << "Enter the second number: " << endl;


cin >> secondNumber;

c.printResult(firstNumber, secondNumber);
}
#include <iostream>
using namespace std;

class Calculator
{
public:
double add(double x, double y)
{
return x + y;
}

double subtract(double x, double y)


{
return x - y;
}

double multiply(double x, double y)


{
return x * y;
}

double divide(double x, double y)


{
return x / y;
}
};

int main()
{
double firstNumber, secondNumber;
Calculator c;

cout << "Enter the first number: " << endl;


cin >> firstNumber;

cout << "Enter the second number: " << endl;


cin >> secondNumber;

cout << "firstNumber + secondNumber = " << c.add(firstNumber, secondNumber) << endl;

cout << "firstNumber - secondNumber = " << c.subtract(firstNumber, secondNumber) << endl;

cout << "firstNumber * secondNumber = " << c.multiply(firstNumber, secondNumber) << endl;

cout << "firstNumber/secondNumber = " << c.divide(firstNumber, secondNumber) << endl;


}
#include <iostream>
using namespace std;

//class definition
class Numbers
{
private:
int a;
int b;
public:
//member function declaration
void readNumbers(void);
void printNumbers(void);
int calAddition(void);
};

//member function definitions


void Numbers::readNumbers(void)
{
cout<<"Enter first number: ";
cin>>a;
cout<<"Enter second number: ";
cin>>b;
}

void Numbers::printNumbers(void)
{
cout<<"a= "<<a<<",b= "<<b<<endl;
}

int Numbers::calAddition(void)
{
return (a+b);
}

//main function
int main()
{
//declaring object
Numbers num;
int add; //variable to store addition
//take input
num.readNumbers();
//find addition
add=num.calAddition();
//print numbers
num.printNumbers();
//print addition
cout<<"Addition/sum= "<<add<<endl;

return 0;
}
With function declared outside the class

#include <iostream>
using namespace std;

//class definition
class Numbers
{
private:
int a;
int b;
int sum;
int sub;
int mul;
int division;
public:
//member function declaration
void readNumbers();
void printNumbers();
void calAddition();
void calSubstraction();
void calMultiplication();
};

//member function definitions


void Numbers::readNumbers()
{
cout<<"Enter first number: ";
cin>>a;
cout<<"Enter second number: ";
cin>>b;
}

void Numbers::printNumbers()
{
cout<<"a= "<<a<<",b= "<<b<<endl;
}

void Numbers::calAddition()
{
sum=a+b;
cout<<"Sum of the number is:=>"<<sum<<endl;
}

void Numbers::calSubstraction()
{
sub=a-b;
cout<<"Substraction of the number is:=>"<<sub<<endl;
}

void Numbers::calMultiplication()
{
mul=a*b;
cout<<"Multiplication of the number is:=>"<<mul<<endl;
}

//main function
int main()
{
//declaring object
Numbers num;

//take input
num.readNumbers();
//print numbers
num.printNumbers();
//find addition
num.calAddition();
num.calSubstraction();
num.calMultiplication();

//print addition
//cout<<"Addition/sum= "<<add<<endl;

return 0;
}
With one function declared inside the class and other functions outside the class

#include <iostream>
using namespace std;

//class definition
class Numbers
{
private:
int a;
int b;
int sum;
int sub;
int mul;
int division;
public:
//member function declaration
void readNumbers()
{

cout<<"Enter first number: ";


cin>>a;
cout<<"Enter second number: ";
cin>>b;

void printNumbers();
void calAddition();
void calSubstraction();
void calMultiplication();
};

//member function definitions


//void Numbers::readNumbers()
//{
// cout<<"Enter first number: ";
// cin>>a;
// cout<<"Enter second number: ";
// cin>>b;
//}

void Numbers::printNumbers()
{
cout<<"a= "<<a<<",b= "<<b<<endl;
}
void Numbers::calAddition()
{
sum=a+b;
cout<<"Sum of the number is:=>"<<sum<<endl;
}

void Numbers::calSubstraction()
{
sub=a-b;
cout<<"Substraction of the number is:=>"<<sub<<endl;
}

void Numbers::calMultiplication()
{
mul=a*b;
cout<<"Multiplication of the number is:=>"<<mul<<endl;
}

//main function
int main()
{
//declaring object
Numbers num;

//take input
num.readNumbers();
//print numbers
num.printNumbers();
//find addition
num.calAddition();
num.calSubstraction();
num.calMultiplication();

//print addition
//cout<<"Addition/sum= "<<add<<endl;

return 0;
}

You might also like