4-1 复数类的运算符重载
#include <iostream>
#include <algorithm>
using namespace std;
class Complex
{
private:
double real;
double imag;
public:
Complex()
{
real = 0;
imag = 0;
}
Complex(double r, double i)
{
real = r;
imag = i;
}
Complex complex_add(Complex &c2);
Complex complex_cut(Complex &c3);
void display1();
void display2();
};
Complex Complex::complex_add(Complex &c2)
{
Complex c;
c.real = real+c2.real;
c.imag = imag+c2.imag;
return c;
}
Complex Complex::complex_cut(Complex &c3)
{
Complex d;
d.real = real-c3.real;
d.imag = imag-c3.imag;
return d;
}
void Complex::display1()
{
cout<<real<<"+"<<imag<<"i"<<endl;
}
void Complex::display2()
{
cout<<real<<imag<<"i"<<endl;
}
int main()
{
Complex c1(3.2, 4.5),c2(8.9, 5.6), c3, c4;
c3 = c1.complex_add(c2);
c4 = c1.complex_cut(c2);
cout<<"a=";c1.display1();
cout<<"b=";c2.display1();
cout<<"a+b=";c3.display1();
cout<<"a-b=";c4.display2();
return 0;
}
4-2 电子时钟中的运算符重载
#include <iostream>
#include <cstdio>
using namespace std;
class Time
{
private:
int hour;
int minute;
int second;
public:
Time(int h,int m,int s)
{
hour=h;
minute=m;
second=s;
}
Time operator ++ ();
void display();
int cmp(Time &t);
};
Time Time::operator ++ ( )
{
if(++second>= 60)
{
second-= 60;
if(++minute>= 60)
{
minute-=60;
++hour;
}
}
}
void Time::display()
{
printf ( "%.2d:%.2d:%.2d\n", hour, minute, second );
}
int Time::cmp(Time & t)
{
if(hour>t.hour)
return 0;
else if(hour== t.hour&& minute > t.minute)
return 0;
else if(hour == t.hour && minute == t.minute && second > t.second)
return 0;
else
return 1;
}
int main()
{
int h1,h2,m1,m2,s1,s2;
cin>>h1>>m1>>s1;
cin>>h2>>m2>>s2;
Time t1(h1,m1,s1);
Time t2(h2,m2,s2);
if(!t1.cmp(t2) )
{
cout<<"The begin time is not earlier than the end time!"<<endl;
}
else
{
while(t1.cmp(t2))
{
t1.display();
++t1;
}
}
return 0;
}
面向对象程序设计上机练习十(运算符重载)
#include <iostream>
using namespace std;
class Complex
{
public:
Complex(double a=0,double b=0)
{real=a,imag=b;}
Complex operator +(Complex &a)
{
return Complex(a.real+real,a.imag+imag);
}
Complex operator +(int a)
{
return Complex(a+real,imag);
}
friend ostream &operator<<(ostream &o,Complex a)
{
o<<a.real;
if(a.imag>0) o<<"+";
if(a.imag) o<<a.imag<<"i";
o<<endl;
return o;
}
friend Complex operator + (int a,Complex &b);
private:
double real,imag;
};
Complex operator + (int a,Complex &b)
{
return Complex(a+b.real,b.imag);
}
int main()
{
int a,b;
cin>>a>>b;
Complex c1(a,b);
cin>>a>>b;
Complex c2(a,b);
cin>>a;
cout<<c1+c2<<c1+a<<a+c1<<endl;
return 0;
}
面向对象程序设计上机练习十一(运算符重载)
#include <iostream>
using namespace std;
class position
{
private:
int x1,x2,x3;
int y1,y2,y3;
public:
position(int a=0,int b=0,int c=0,int d=0,int e=0,int f=0)
{
x1=a;
x2=b;
x3=c;
y1=d;
y2=e;
y3=f;
}
position operator +(position & r);
friend ostream&operator <<(ostream&, position&);
};
position position :: operator +(position & r)
{
position p;
p.x1=x1+r.x1;
p.x2=x2+r.x2;
p.x3=x3+r.x3;
p.y1=y1+r.y1;
p.y2=y2+r.y2;
p.y3=y3+r.y3;
return p;
}
ostream & operator << (ostream & output, position &r)
{
output<<r.x1<<" "<<r.x2<<" "<<r.x3<<endl;
output<<r.y1<<" "<<r.y2<<" "<<r.y3;
return output;
}
int main()
{
int a,b,c,d,e,f;
cin>>a>>b>>c>>d>>e>>f;
position r1(a,b,c,d,e,f);
cin>>a>>b>>c>>d>>e>>f;
position r2(a,b,c,d,e,f);
position r3;
r3=r1+r2;
cout<<r3<<endl;
return 0;
}
面向对象程序设计上机练习十二(运算符重载)
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
class Complex
{
double real,imag;
public:
operator double()
{
return real;
}
Complex (double a=0,double b=0)
{
real=a;
imag=b;
}
};
int main()
{
double a,b,c;
cin>>a>>b>>c;
Complex c1(a,b);
double d=c1+c;
cout<<fixed<<setprecision(1)<<d<<endl;
return 0;
}
3-1 Point类的构造函数
#include <algorithm>
#include <iostream>
using namespace std;
class point
{
private:
int x, y;
public:
point()
{
x = 0;
y = 0;
}
void showpoint()
{
cout<<"("<<x<<","<<y<<")"<<endl;
}
void show(int a,int b)
{
x = a;
y = b;
}
};
int main()
{
point p;
int a, b;
cin>>a>>b;
p.showpoint();
point p1;
p1.show(a, b);
p1.showpoint();
return 0;
}
3-2 构造函数的调用
#include <algorithm>
#include <iostream>
using namespace std;
class point
{
public:
void show()
{
cout<<"Constructing an object of A"<<endl;
}
~point()
{
cout<<"Destructing an object of A"<<endl;
}
};
int main()
{
point A, B;
A.show();
B.show();
return 0;
}
3-3 构造函数的调用(高级)
#include <algorithm>
#include <iostream>
using namespace std;
class point
{
public:
void show()
{
cout<<"constructing an object of A"<<endl;
}
~point()
{
cout<<"Destructing an object of A"<<endl;
}
};
void display()
{
point C;
C.show();
}
int main()
{
cout << "----begin main---" << endl;
point A, B, D;
A.show();
display();
cout << "*******"<<endl;
B.show();
D.show();
cout << "----end main---" << endl;
return 0;
}
3-4 计算长方形的周长和面积
#include <iostream>
using namespace std;
class Rect
{
public:
Rect(double L=0,double W=0)
{
Length=L;
Width=W;
}
void f1()
{
cout<<"the length and width of r1 is:"<<Length<<","<<Width<<endl;
cout<<"the perimeter of r1 is:"<<2*(Length+Width)<<endl;
cout<<"the area of r1 is:"<<Length*Width<<endl;
}
void f2()
{
cout<<"the length and width of r2 is:"<<Length<<","<<Width<<endl;
cout<<"the perimeter of r2 is:"<<2*(Length+Width)<<endl;
cout<<"the area of r2 is:"<<Length*Width<<endl;
}
Rect(const Rect &b)
{
Length=b.Length;
Width=b.Width;
}
private:
double Length;
double Width;
};
int main()
{
double a,b;
cin>>a>>b;
if(a<0||b<0)
{
a=0;
b=0;
}
Rect r1(a,b),r2(r1);
r1.f1();
r2.f2();
return 0;
}
3-5 学生成绩统计
#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
class student
{
private:
char name[50];
int num;
int math;
int eng;
int com;
public:
void set_stu(student p[], int n)
{
int i;
for ( i = 0;i < n; i++ )
cin>>p[i].num>>p[i].name>>p[i].math>>p[i].eng>>p[i].com;
}
int sum(student p)
{
int Sum = 0;
Sum = p.math+p.com+p.eng;
return Sum;
}
double ave(int Sum)
{
double Ave;
Ave = (double)Sum/(double)3;
return Ave;
}
void Printf(student p[])
{
cout<<"StuID"<<"\t"<<"Name"<<"\t"<<"Math"<<"\t"<<"Eng"<<"\t"<<"Com"<<"\t"<<"Total"<<"\t"<<"Average"<<endl;
int i;
for ( i = 0;i < 5; i++ )
{
int Sum;
double Ave;
Sum = sum(p[i]);
Ave = ave(Sum);
cout<<p[i].num<<"\t"<<p[i].name<<"\t"<<p[i].math<<"\t"<<p[i].eng<<"\t"<<p[i].com<<"\t"<<Sum<<"\t";
printf ( "%.1lf\n", Ave );
}
}
};
int main()
{
cout<<"Input the messages of five students(StudentID Name Math English Computer )"<<endl<<endl;
student stu[5];
stu[5].set_stu(stu, 5);
stu[5].Printf(stu);
return 0;
}
3-6 静态数据成员与静态成员函数
#include <algorithm>
#include <iostream>
using namespace std;
class point
{
private:
double x, y;
static int count;
public:
static void showpoint();
void display()
{
cout<<"Deconstructor point x=" << x << endl;
}
point(double a = 0, double b = 0)
{
count++;
x=a;
y=b;
}
};
int point::count = 0;
void point::showpoint()
{
cout << "x=0,Y=0" << endl;
cout << "the number of points is " << count << endl;
}
int main()
{
point a(5), b(3), c(0);
point::showpoint();
a.display();
b.display();
c.display();
return 0;
}
3-7 类的友元函数的应用
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
class point
{
private:
double x;
double y;
public:
void display2(point &q);
void display1()
{
cout<<" point is the coordinate:X="<<x<<","<<"Y="<<y<<endl;
}
point(double a, double b)
{
x = a;
y = b;
}
};
void point::display2(point &q)
{
cout<<"The distance between the two points is:"<<sqrt((x-q.x)*(x-q.x)+(y-q.y)*(y-q.y))<<endl;
}
int main()
{
double a, b, c, d;
cin>>a>>b>>c>>d;
point x(a, b);
point y(c, d);
cout<<"The first";
x.display1();
cout<<"The second";
y.display1();
x.display2(y);
return 0;
}
面向对象程序设计上机练习八(对象数组)
#include <iostream>
#include <algorithm>
using namespace std;
class student
{
private:
int i;
int num[1000], n;
char name[1000][20];
public:
void setint();
void setput();
};
void student::setint()
{
cin>>n;
for ( i = 0; i < n; i++ )
{
cin>>name[i]>>num[i];
}
}
void student::setput()
{
for ( i = 0; i < n; i++ )
{
cout << name[i]<<" "<<num[i]<<endl;
}
}
int main()
{
student t;
t.setint();
t.setput();
}
面向对象程序设计上机练习九(对象指针)
#include <iostream>
#include <algorithm>
using namespace std;
class student
{
private:
int i;
int k;
int num[1000], n;
char name[1000][20];
public:
void setint();
void setput();
};
void student::setint()
{
for ( i = 0; i < 5; i++ )
{
cin>>name[i]>>num[i];
}
}
void student::setput()
{
int max = 0;
for ( i = 0; i < 5; i++ )
{
if ( max < num[i] )
{
max = num[i];
k = i;
}
}
cout<<name[k]<<" "<<num[k]<<endl;
}
int main()
{
student t, *p;
p = &t;
(*p).setint();
(*p).setput();
return 0;
}