Write a program in C++ that provides a user-defined class NewFloat with a data member
floatData of type float. The class also comprises the following members:
#include <iostream>
using namespace std;
class NewFloat{
private:
// data member floatData of type float.
float floatData;
public:
//default and parameterized constructors
NewFloat(){
this->floatData=0;
}
NewFloat(float floatData){
this->floatData=floatData;
}
//an inline member function printData() that prints the details of an object of the class
void printData(){
cout<<"floatData: "<<floatData<<"\n";
}
//a member function deciData() that returns the decimal part of floatData
float deciData(){
float whole = floor(floatData);
float decimal = floatData - whole;
return decimal;
}
//operator overloading to perform the following operations:
//post increment (++) increments floatData by 1.
void operator++(){
this->floatData++;
}
//less than (<) on a pair of objects of the class.
//The function returns 1 if the data member of the object on the LHS of the operator
//is less than the data member of the object on the RHS of the operator and 0 otherwise.
int operator<(NewFloat otherFloatData){
return (this->floatData < otherFloatData.floatData);
}
//int cast to convert the float data member of an object into an integer.
int convert(){
return (int)floatData;
}
};
int main() {
float floatData1;
float floatData2;
cout<<"Enter floatData 1: ";
cin>>floatData1;
cout<<"Enter floatData 2: ";
cin>>floatData2;
NewFloat _NewFloat1(floatData1);
NewFloat _NewFloat2(floatData2);
_NewFloat1.printData();
_NewFloat2.printData();
_NewFloat1++;
_NewFloat2++;
cout<<"\nNewFloat1++\n";
cout<<"NewFloat2++\n";
_NewFloat1.printData();
_NewFloat2.printData();
cout<<"\nThe decimal part of floatData1: "<<_NewFloat1.deciData();
cout<<"\nThe decimal part of floatData2: "<<_NewFloat2.deciData();
if(_NewFloat1<_NewFloat2){
cout<<"\n_NewFloat1<_NewFloat2\n";
}else{
cout<<"\n_NewFloat1>_NewFloat2\n";
}
cout<<"\nfloatData1 convert: "<<_NewFloat1.convert();
cout<<"\nfloatData2 convert: "<<_NewFloat2.convert();
system("pause");
return 0;
}
Comments
Leave a comment