Answer to Question #309063 in C++ for Rajat Vats

Question #309063

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:

  • default and parameterized constructors
  • an inline member function printData() that prints the details of an object of the class
  • a member function deciData() that returns the decimal part of floatData
  • operator overloading to perform the following operations:
  1. post increment (++) increments floatData by 1.
  2. 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.
  3. int cast to convert the float data member of an object into an integer.
1
Expert's answer
2022-03-10T18:08:06-0500


#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog