第一题:
#include<iostream>
#include<cstring>
using namespace std;
class String
{
friend ostream& operator<<(ostream& output, String &a);
friend int main();
private:
int size;
char *str;
public:
String(char * a);
};
String::String(char * a)
{
str = a;
size = strlen(a);
}
ostream& operator<<(ostream& output, String &a)
{
int i;
int n=a.size;
for(i=0;i<n;i++){
output<<a.str[i];
}
return output;
}
int main()
{
char *a = "Hello world!";
cout << a << endl;
String s = String(a);
cout << s;
cin.get();
return 0;
}
![](https://video.ask-data.xyz/img.php?b=https://iknow-pic.cdn.bcebos.com/cdbf6c81800a19d8bec0170c37fa828ba71e46f3?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_600%2Ch_800%2Climit_1%2Fquality%2Cq_85%2Fformat%2Cf_auto)
第二题:
#include<iostream>
using namespace std;
class point
{
friend point operator+(const point& a, const point& b);
friend ostream& operator<<(ostream& output, point& a);
friend int main();
private:
double x;
double y;
public:
point()
{
x=0;
y=0;
}
point(double xx, double yy){
x=xx;
y=yy;
}
};
point operator+(const point& a, const point& b)
{
point c;
c.x= a.x+b.x;
c.y= a.x+b.y;
return c;
}
ostream& operator<<(ostream& output, point& a)
{
output << a.x << " " << a.y;
return output;
}
int main()
{
point a = point();
cout << a << endl;
point b = point(-1,-1);
cout << b << endl;
point c = a+b;
cout << c << endl;
cin.get();
return 0;
}
![](https://video.ask-data.xyz/img.php?b=https://iknow-pic.cdn.bcebos.com/8718367adab44aed4a4a2cedb71c8701a18bfbb7?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_600%2Ch_800%2Climit_1%2Fquality%2Cq_85%2Fformat%2Cf_auto)