In: Computer Science
Instructions
Write the definitions of the member functions of the class integerManipulation not given in Example 10-11. Also, add the following operations to this class:
Also, write a program to test theclass integerManipulation.
The header file for class integerManipulation has been provided for you.
integerManipulation.h:
class integerManipulation
{
public:
void setNum(long long n);
//Function to set num.
//Postcondition: num = n;
long long getNum();
//Function to return num.
//Postcondition: The value of num is returned.
void reverseNum();
//Function to reverse the digits of num.
//Postcondition: revNum is set to num with digits in
// in the reverse order.
void classifyDigits();
//Function to count the even, odd, and zero digits of num.
//Postcondition: evenCount = the number of even digits in
num.
// oddCount = the number of odd digits in num.
int getEvensCount();
//Function to return the number of even digits in num.
//Postcondition: The value of evensCount is returned.
int getOddsCount();
//Function to return the number of odd digits in num.
//Postcondition: The value of oddscount is returned.
int getZerosCount();
//Function to return the number of zeros in num.
//Postcondition: The value of zerosCount is returned.
int sumDigits();
//Function to return the sum of the digits of num.
//Postcondition: The sum of the digits is returned.
integerManipulation(long long n = 0);
//Constructor with a default parameter.
//The instance variable num is set accordingto the parameter,
//and other instance variables are set to zero.
//The default value of num is 0;
//Postcondition: num = n; revNum = 0; evenscount = 0;
// oddsCount = 0; zerosCount = 0;
private:
long long num;
long long revNum;
int evensCount;
int oddsCount;
int zerosCount;
};
integerManipulationlmp.cpp:
main.cpp:
#include <iostream>
using namespace std;
int main() {
// Write your main here
return 0;
}
If you have any doubts, please give me comment...
integerManipulation.h
class integerManipulation
{
public:
void setNum(long long n);
//Function to set num.
//Postcondition: num = n;
long long getNum();
//Function to return num.
//Postcondition: The value of num is returned.
void reverseNum();
//Function to reverse the digits of num.
//Postcondition: revNum is set to num with digits in
// in the reverse order.
void classifyDigits();
//Function to count the even, odd, and zero digits of num.
//Postcondition: evenCount = the number of even digits in num.
// oddCount = the number of odd digits in num.
int getEvensCount();
//Function to return the number of even digits in num.
//Postcondition: The value of evensCount is returned.
int getOddsCount();
//Function to return the number of odd digits in num.
//Postcondition: The value of oddscount is returned.
int getZerosCount();
//Function to return the number of zeros in num.
//Postcondition: The value of zerosCount is returned.
int sumDigits();
//Function to return the sum of the digits of num.
//Postcondition: The sum of the digits is returned.
integerManipulation(long long n = 0);
//Constructor with a default parameter.
//The instance variable num is set accordingto the parameter,
//and other instance variables are set to zero.
//The default value of num is 0;
//Postcondition: num = n; revNum = 0; evenscount = 0;
// oddsCount = 0; zerosCount = 0;
private:
long long num;
long long revNum;
int evensCount;
int oddsCount;
int zerosCount;
};
integerManipulation.cpp
#include "integerManipulation.h"
void integerManipulation::setNum(long long n){
num = n;
}
long long integerManipulation::getNum(){
return num;
}
void integerManipulation::reverseNum(){
long long temp = num;
revNum = 0;
while(temp>0){
revNum = revNum*10 + temp%10;
temp = temp/10;
}
}
void integerManipulation::classifyDigits(){
long long temp = num;
int digit;
while(temp>0){
digit = temp%10;
if(digit%2==0)
evensCount++;
else
oddsCount++;
if(digit==0)
zerosCount++;
temp = temp/10;
}
}
int integerManipulation::getEvensCount(){
return evensCount;
}
int integerManipulation::getOddsCount(){
return oddsCount;
}
int integerManipulation::getZerosCount(){
return zerosCount;
}
int integerManipulation::sumDigits(){
int sum = 0;
long long temp = num;
while(temp>0){
sum += temp%10;
temp = temp/10;
}
return sum;
}
integerManipulation::integerManipulation(long long n){
num = n;
revNum = 0;
evensCount = 0;
oddsCount = 0;
zerosCount = 0;
}
main.cpp
#include <iostream>
#include "integerManipulation.h"
using namespace std;
int main()
{
integerManipulation im1;
im1.setNum(328473676734036);
im1.classifyDigits();
im1.reverseNum();
cout<<"Number: "<<im1.getNum()<<endl;
cout<<"Evens count: "<<im1.getEvensCount()<<endl;
cout<<"Odds count: "<<im1.getOddsCount()<<endl;
cout<<"Zeros count: "<<im1.getZerosCount()<<endl;
cout<<"Sum of digits: "<<im1.sumDigits()<<endl;
return 0;
}