Answer to Question #196087 in C++ for mahnoor

Question #196087

do it on the computer, do any rough work required on paper. a) make a class with name as your university registration id e.g. class f20200657890, with the following members: char [] name float number int x_val int y_val constructor without any parameter constructor with all four parameters to set copy constructor with proper deep copy destructor total 8, set and get functions, for each of name, number, x_val, and y_val. operator + overload operator << overload as friend function


1
Expert's answer
2021-05-21T00:02:50-0400
#include <iostream>
#include <string>


using namespace std;


class f20200657890{
private:
	//char [] name float number int x_val int y_val
	char name[20]; 
	float number;
	int x_val;
	int y_val;
public:
	/**
	* constructor without 
	*/
	f20200657890(){


	}
	/**
	* constructor with all four parameters to set
	*/
	f20200657890(char n[],float num,int x,int y){
		strcpy(name,n);
		this->number=num;
		this->x_val=x;
		this->y_val=y;
	}
	//copy constructor 
	f20200657890(const f20200657890 &f1) {
		strcpy(name,f1.name);
		this->number=f1.number;
		this->x_val=f1.x_val;
		this->y_val=f1.y_val; 
	}
	/**
	* @return the name
	*/
	void getName(char n[]) {
		strcpy(n,this->name);
	}
	/**
	* @param name the name to set
	*/
	void setName(char n[]) {
		strcpy(this->name,n);
	}
	/**
	* @return the number
	*/
	float getNumber() {
		return number;
	}
	/**
	* @param number the number to set
	*/
	void setNumber(float num) {
		this->number = num;
	}
	/**
	* @return the x_val
	*/
	int getX_val() {
		return x_val;
	}
	/**
	* @param x_val the x_val to set
	*/
	void setX_val(int x) {
		this->x_val = x;
	}
	/**
	* @return the x_val
	*/
	int getY_val() {
		return y_val;
	}
	/**
	* @param x_val the x_val to set
	*/
	void setY_val(int y) {
		this->y_val = y;
	}
	// overload the operator +
	f20200657890 operator+(f20200657890 otherf20200657890)
	{
		f20200657890 sumf20200657890;
		strcat(this->name, otherf20200657890.name);
		strcpy(sumf20200657890.name,this->name);
		sumf20200657890.number=this->number+ otherf20200657890.number;
		sumf20200657890.x_val=this->x_val+otherf20200657890.x_val;
		sumf20200657890.y_val=this->y_val+otherf20200657890.y_val; 
		return sumf20200657890;
	}
	// overload operator <<
	friend  ostream& operator << (ostream& os, const f20200657890& f_20200657890)
	{
		os<<"Name: "<<f_20200657890.name<<"\n";
		os<<"Number: "<<f_20200657890.number<<"\n";
		os<<"x val: "<<f_20200657890.x_val<<"\n";
		os<<"y val: "<<f_20200657890.y_val<<"\n";
		return os;
	}
};




int main (){
	
	f20200657890 f1("f1",5,4,4);
	f20200657890 f2("f2",10,5,9);
	f20200657890 f3=f1+f2;


	cout<<f1;
	cout<<f2;
	cout<<f3;


	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