第一题:
#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;
}
第二题:
#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;
}