1. Define complex class, and overload +, - Operator with UFIDA function. And write the main function to test. (Experiment 7 requires overloading with member functions)
Reference answer 0:
UFIDA function overload operator #include<iostream.h> class complex { double real,imag; public: complex(double r=0,double i=0) { real=r; imag=i; } void print(); friend complex operator+(complex a,complex b); friend complex operator-(complex a,complex b); }; void complex::print() { cout<<real; if(imag>0) cout<<"+"; if(imag!=0) cout<<imag<<"i"<<endl; } complex operator+(complex a,complex b) { complex temp; temp.real=a.real+b.real; temp.imag=a.imag+b.imag; return temp; } complex operator-(complex a,complex b) { complex temp; temp.real=a.real-b.real; temp.imag=a.imag-b.imag; return temp; } void main() { complex A1(2,3),A2(1,1),A3,A4; A3=A1+A2; A4=A1-A2; A1.print(); A2.print(); A3.print(); A4.print(); }
2. Define the point class and overload the pre – and post – operators with member functions and friend functions respectively.
Reference answer:
(1) UFIDA function overload operator
Front-end
#include<iostream.h> class point { int x,y; public: point(int,int); friend void operator--(point&); void print(); }; point::point(int a,int b) { x=a; y=b; } void operator--(point &p) { --p.x; --p.y; } void point::print() { cout<<"("<<x<<","<<y<<")"<<endl; } void main() { point p(2,2); p.print(); (--p); p.print(); }
Postposition -
#include<iostream.h> class Point { int x,y; public: Point(int x,int y); friend Point operator--(Point &point,int a); void showprint(); }; Point::Point(int x,int y) { this->x=x; this->y=y; } Point operator--(Point &point,int a) { Point p(1,1); p=point; --point.x; --point.y; return p; } void Point::showprint() { cout<<"("<<x<<","<<y<<")"<<endl; } void main() { Point point(2,2); point.showprint(); (operator--(point,0)).showprint(); point.showprint(); }
(2) overloading operators with member functions
Front-end
#include<iostream.h> class Point { int x,y; public: Point(int,int); Point operator--(); void print(); }; Point::Point(int a,int b) { x=a; y=b; } Point Point::operator--() { --x; --y; return *this; } void Point::print() { cout<<"("<<x<<","<<y<<")"<<endl; } void main() { Point p(2,2); p.print(); (--p); p.print(); }
Postposition -
#include<iostream.h> class Point { int x,y; public: Point(int,int); Point operator--(int); void print(); }; Point::Point(int a,int b) { x=a; y=b; } Point Point::operator--(int a) { Point p(1,1); p=*this; --x; --y; return p; } void Point::print() { cout<<"("<<x<<","<<y<<")"<<endl; } void main() { Point p(2,2); p.print(); (p.operator--(0)).print(); p.print(); }
3. The combination of UFIDA metaclasses and classes solves the following problems
(1) design a point class
Data members:
Coordinates of point x,y
Member function:
Constructor with parameters (without default)
(2) define a line class
Data members:
Two points on the line point1 and point2 (with defined points)
Member function:
Define a line (constructor)
Calculate the length of the segment
Refer to answer 1:
#include <iostream> #include <cmath> using namespace std; class Point{ int x,y; public: Point(int xx,int yy):x(xx),y(yy){} friend class Line; }; class Line{ Point p1,p2; public: Line(int x1,int y1,int x2,int y2):p1(x1,y1),p2(x2,y2) {} double length(); }; double Line::length(){return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));} int main(){ Line line(1,1,5,5); cout<<line.length()<<endl; return 0; }
Refer to answer 2:
#include<iostream.h> #include<math.h> class line; class point {private: float x,y; public: point(float a,float b){x=a;y=b;} friend class line; }; class line { point point1,point2; public: line(point a,point b):point1(a),point2(b){} float length() {return sqrt((point1.x-point2.x)*(point1.x-point2.x)+(point1.y-point2.y)*(point1.y-point2.y)); } }; void main() {point poin1(1,2),poin2(3,4); line lin(poin1,poin2); cout<<lin.length(); }