C++学习day4
思维导图:
练习:
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>
using namespace std;
class mydouble{
private:
int a;
int b;
public:
mydouble(int a,int b):a(a),b(b){};
void show()
{
cout << a << "." << abs(b) << endl;
}
mydouble operator+(const mydouble& r)
{
mydouble AB(0,0);
AB.a= a + r.a;
AB.b= b + r.b;
return AB;
}
mydouble operator-(const mydouble& r)
{
mydouble AB2(0,0);
AB2.a = a - r.a;
AB2.b = b - r.b;
return AB2;
}
mydouble operator*(const mydouble& r)
{
mydouble AB3(0,0);
AB3.a = a * r.a;
AB3.b = b * r.b;
return AB3;
}
};
int main(int argc,const char** argv){
mydouble A(3,14);
A.show();
mydouble B(1,23);
B.show();
mydouble AB=A+B;
printf("A+B的值为:");
AB.show();
mydouble AB2 = A-B;
printf("A-B的值为:");
AB2.show();
mydouble AB3 = A*B;
printf("A*B的值为:");
AB3.show();
}
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>
using namespace std;
class myOut{
private:
public:
void operator<<(int a)
{
printf("%d\n",a);
cout<<a<<endl;
}
void operator<<(double a )
{
printf("%f\n",a);
cout<<a<<endl;
}
void operator<<(const char* a)
{
printf("%s\n",a);
cout<<a<<endl;
}
void operator<<(ostream&(*endl)(ostream&))
{
printf("\n");
}
};
/*ostream& operator<<(ostream& out,const myOut& r)
{
out<<r;
*/
int main(int argc,const char** argv){
myOut out;
out<<1;
out<<3.14;
out<<"hello";
out<<endl;
return 0;
}