Answer to Question #195936 in C++ for zain ul abdeen

Question #195936

Question 2 - Do it on 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:


  1. char [] name
  2. Float number
  3. int x_val
  4. int y_val
  5. Constructor without any parameter
  6. Constructor with all four parameters to set
  7. Copy constructor with proper deep copy
  8. Destructor
  9. Total 8, Set and Get functions, for each of Name, Number, x_val, and y_val.
  10. Operator + overload
  11. Operator << overload as friend function


b) Write main for the class, to demonstrate constructing two objects and using each of the member functions at least once. If you miss using any single function you lose some marks.




1
Expert's answer
2021-05-21T10:52:36-0400
#include <iostream>
#include <string>


using namespace std;


class F20200657890{
private:
	char name[50]; 
	float number;
	int x_val;
	int y_val;
public:


	F20200657890(){


	}


	F20200657890(char n[],float num,int x,int y){
		strcpy(name,n);
		this->number=num;
		this->x_val=x;
		this->y_val=y;
	}


	F20200657890(const F20200657890 &f1) {
		strcpy(name,f1.name);
		this->number=f1.number;
		this->x_val=f1.x_val;
		this->y_val=f1.y_val; 
	}


	void getName(char n[]) {
		strcpy(n,this->name);
	}


	void setName(char n[]) {
		strcpy(this->name,n);
	}


	float getNumber() {
		return number;
	}


	void setNumber(float num) {
		this->number = num;
	}


	int getX_val() {
		return x_val;
	}


	void setX_val(int x) {
		this->x_val = x;
	}
	
	int getY_val() {
		return y_val;
	}


	void setY_val(int y) {
		this->y_val = y;
	}


	F20200657890 operator+(F20200657890 otherF20200657890)
	{
		F20200657890 newF20200657890;
		strcat(this->name, otherF20200657890.name);
		strcpy(newF20200657890.name,this->name);
		newF20200657890.number=this->number+ otherF20200657890.number;
		newF20200657890.x_val=this->x_val+otherF20200657890.x_val;
		newF20200657890.y_val=this->y_val+otherF20200657890.y_val; 
		return newF20200657890;
	}


	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 F202006578901("F202006578901",10,7,9);
	cout<<F202006578901;
	F20200657890 F202006578902;
	F202006578902.setName("F202006578902");
	F202006578902.setNumber(23);
	F202006578902.setX_val(5);
	F202006578902.setY_val(10);


	F20200657890 F202006578903=F202006578901+F202006578902;


	
	cout<<F202006578902;
	cout<<F202006578903;


	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