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:
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.
#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;
}
Comments
Leave a comment