*001
#include "iostream"
using namespace std;
double max(double,double);
int max(int,int);
char max(char,char);
int max(int,int,int);
void main()
{
cout<<max(2.5,17.54)<<"/t"<<max(56,8)<<"/t"<<max('w','p')<<endl;
cout<<"max(5,9,4)="<<max(5,9,4)<<"/t"<<"max(5,4,9)="<<max(5,4,9)<<endl;
}
double max(double m1,double m2)
{
return(m1>m2?m1:m2);
}
int max(int m1,int m2)
{return(m1>m2?m1:m2);}
char max(char m1,char m2)
{return(m1>m2?m1:m2);}
int max(int m1,int m2,int m3)
{
int t=max(m1,m2);
return max(t,m3);
}
*002
#include "iostream"
using namespace std;
int add(int m1=0,int m2=0,int m3=0,int m4=0)
{return m1+m2+m3+m4;}
void main()
{
cout<<add(1,3)<<"/t"<<add(1,3,5)<<"/t"<<add(1,3,5,7)<<endl;
}
*003
#include "iostream"
using namespace std;
class Point
{
private:
int x,y;
public:
Point(int a,int b){x=a;y=b;}
void Show(){cout<<"x="<<x<<"/t"<<",y="<<y<<endl;};
};
class Rectangle:public Point
{
private:
int H,W;
public:
Rectangle(int,int,int,int);
void Show()
{
cout<<"H="<<H<<" W="<<W<<endl;
}
void Display()
{
Point::Show();
Rectangle::Show();
}
};
Rectangle::Rectangle(int a,int b,int h,int w):Point(a,b){H=h;W=w;};
void main()
{
Point a(3,4);
Rectangle r1(3,4,5,6);
a.Show();
r1.Show();
r1.Point::Show();
r1.Display();
}
*004
#include "iostream.h"
class test{
private:
int i;
float f;
char ch;
public:
test(int a=0,float b=0,char c='/0'){i=a;f=b;ch=c;}
friend ostream&operator<<(ostream&,test);
friend istream&operator>>(istream&,test&);
};
ostream&operator<<(ostream&chen,test obj)
{
chen<<obj.i<<",";
chen<<obj.f<<",";
chen<<obj.ch<<endl;
return chen;
}
istream&operator>>(istream&yong,test&obj)
{
yong>>obj.i;
yong>>obj.f;
yong>>obj.ch;
return yong;
}
void main()
{
test A(45,8.5,'w');
operator<<(cout,A);//cout<<A;
test B,C;
cout<<"Input as i f ch:";
operator>>(cin,B);//cin>>B>>C;
operator>>(cin,C);
operator<<(cout,B);//cout<<B<<C;
operator<<(cout,C);
}
*005
#include "iostream"
using namespace std;
class number{
int num;
public:
number(int i){num=i;}
int operator++();
int operator++(int);
void Print(){cout<<"num="<<num<<endl;}
};
int number::operator++()//++n
{
num++;
return num;
}
int number::operator++(int)//n++
{
int i=num;
num+=9;
return i;
}
void main()
{
number n(10);
int i=++n;//int i=n.operator++();
cout<<"i="<<i<<endl;
n.Print();
//i=n.operator++(0);
i=n++;
cout<<"i="<<i<<endl;
n.Print();
}
*006
#include "iostream.h"
class complex{
private:
double real,imag;
public:
complex(double r=0,double i=0){real=r;imag=i;}
friend complex operator+(complex,complex);
void Show(){cout<<real<<"+"<<imag<<"i"<<endl;}
};
complex operator+(complex a,complex b)
{
double r=a.real+b.real;
double i=a.imag+b.imag;
return complex(r,i);
}
void main()
{
complex x(5,3),y;
//隐式类型转换
y=x+7;//y=operator+(x,7);
y.Show();
y=7+y;//y=operator+(7,y);
y.Show();
}
*007
#include "iostream"
using namespace std;
template<typename T>
T max(T m1,T m2)
{return(m1>m2?m1:m2);}
template<typename T>
T max(T a,T b,T c)
{return max(max(a,b),c);}
template<class T>//template<typename T>
T max(T a[],int n)
{
T maxnum=a[0];
for(int i=0;i<n;i++)
if(maxnum<a[i]) maxnum=a[i];
return maxnum;
}
template<>
char*max(char*a,char*b)
{return(strcmp(a,b)>=0?a:b);}
int max(int m1,double m2)
{int m3=(int)m2;return(m1>m3?m1:m3);}
void main()
{
cout<<max("ABC","ABD")<<"/t";
cout<<max("ABC","ABD","ABE")<<"/t";
cout<<max('W','T','K')<<"/t";
cout<<max(2.0,5.0,8.9)<<"/t";
cout<<max(2,6.7)<<"/t";
double d[]={8.2,2.2,3.2,2.7,5.2,7.2,7.4,5.1,1.5,2.7};
int a[]={1,2,3,4,5,7,8,11,12,13};
char c[]="abcdefg";
cout<<"intMax="<<max(a,10)<<" doubleMax="<<max(d,10)<<" charMax="<<max(c,10)<<endl;
}
*008
#include "iostream"//类模板
using namespace std;
template <class T,int size=4>
class Sum
{
T m[size];
public:
Sum(T a,T b,T c,T d)
{
m[0]=a;m[1]=b;m[2]=c;m[3]=d;}
T S()
{return m[0]+m[1]+m[2]+m[3];}
};
void main()
{
Sum<int,4>num1(-23,5,8,-2);
Sum<float,4>f1(3.5f,-8.5f,8.8f,9.7f);
Sum<double,4>d1(355.4,253.8,456.7,-67.8);
Sum<char,4>c1('w',-2,-1,-1);
cout<<num1.S()<<","<<f1.S()<<","<<d1.S()<<","<<c1.S()<<endl;
}
*009
#include "iostream"//模板类
using namespace std;
class A1{
int x;
public:
A1(int c=8):x(c){cout<<"Call A1 "<<x<<endl;}
};
class A11:public A1{
int x1;
public:
A11(int a,int b):A1(a),x1(b){cout<<"Call A11 "<<x1<<endl;}
};
template <typename T>
class B1{
T x;
public:
B1(T a=0):x(a){cout<<"Call B1 "<<x<<endl;}
};
template <typename T>
class B11:public B1<T>{
T x1;
public:
B11(T a,T b):B1<T>(a),x1(b){cout<<"Call A11 "<<x1<<endl;}
};
template<typename T>//共有继承未知基类
class C:public T{
public:
C(int a):T(a){cout<<"Call C "<<endl;}
C(int a,int b):T(a,b){cout<<"Call C "<<endl;}
};
void main()
{
C<A1>x(90);
C<A11>x1(25,95);
C<B1<int> >y(189);//右边两个>号之间必须至少有一个空格
C<B11<int> >y1(125,188);//右边两个>号之间必须至少有一个空格
}
*010
#include "iostream"//类模板的专门化
using namespace std;//
template<class T>
class Stack{
int counter;
int max;
T *num;
public:
Stack(int a):counter(0),max(a),num(new T[a]){}
bool isEmpty() const{return counter==0;}
bool isFull() const{return counter==max;}
int count()const{return counter;}
bool push(const T&data){
if(isFull()) return false;
num[counter++]=data;
return true;
}
bool pop(T&data){
if(isEmpty()) return false;
data=num[--counter];
return true;
}
const T&top()const{return num[counter-1];}
~Stack(){delete[]num;}
};
template<>//专门化前缀
class Stack<char*>{
int counter;
int max;
char**num;
public:
Stack(int a):counter(0),max(a),num(new char*[a]){}
bool isEmpty()const{return counter==0;}
bool isFull()const{return counter==max;}
int count()const{return counter;}
bool push(const char*data){
if(isFull()) return false;
num[counter]=new char[strlen(data)+1];
strcpy(num[counter++],data);
return true;
}
bool pop(char data[]){
if(isEmpty())return false;
strcpy(data,num[--counter]);
delete[]num[counter];
return false;
}
const char*&top()const{return num[counter-1];}
~Stack(){
while(counter)delete[]num[--counter];
delete[]num;
}
};
void main()
{
Stack<int>st(8);
int i=0;
while(!st.isFull()){
st.push(10+i++);
cout<<st.top()<<" ";
}
cout<<endl;
int data;
while(!st.isEmpty()){
st.pop(data);
cout<<data<<" ";
}
cout<<endl;
Stack<double>stl(8);
i=0;
while(!stl.isFull()){
stl.push(0.5+i++);
cout<<stl.top()<<" ";
}
cout<<endl;
double data1;
while(!stl.isEmpty()){
stl.pop(data1);
cout<<data1<<" ";
}
cout<<endl;
char*str[]={"1st","2nd","3rd","4th","5th","6th","7th","8th"};
Stack<char*>st2(8);
i=0;
while(st2.push(str[i++]))cout<<st2.top()<<" ";
cout<<endl;
char strdata[8];
while(st2.pop(strdata)) cout<<strdata<<" ";
cout<<endl;
}
*011
#include "iostream"
using namespace std;
const double PI=3.14159;
class Point{
private:
double x,y;
public:
Point(double i,double j){x=i;y=j;}
double area(){return 0;}
};
class Circle:public Point{
private:
double radius;
public:
Circle(double a,double b,double r):Point(a,b){radius=r;}
double area(){return PI*radius*radius;}
};
void main()
{
Point a(1.5,6.7);
Circle c(1.5,6.7,2.5);
cout<<"area of Point is "<<a.area()<<endl;
cout<<"area of Circle is "<<c.area()<<endl;
Point*p=&c;//Circle*p=&c;
cout<<"area of Circle is "<<p->area()<<endl;
Point&rc=c;
cout<<"area of Circle is "<<rc.area()<<endl;
}
*012
#include "iostream"
using namespace std;
const double PI=3.14159;
class Point{
private:
double x,y;
public:
Point(double i,double j){x=i;y=j;}
virtual double area(){return 0;}
};
class Circle:public Point{
private:
double radius;
public:
Circle(double a,double b,double r):Point(a,b){radius=r;}
double area(){return PI*radius*radius;}
};
void display(Point*p)
{cout<<p->area()<<endl;}
void display(Point&a)
{cout<<a.area()<<endl;}
void main()
{
Point a(1.5,6.7);
Circle c(1.5,6.7,2.5);
Point*p=&c;
Point&rc=c;
display(a);
display(p);
display(rc);
}
*013
#include "iostream"
using namespace std;
class A{
public:
int m_A;
A(int a){m_A=a;}
void func1(){cout<<"A::func1()"<<endl;}
void func2(){cout<<"A::func2()"<<endl;}
virtual void vfunc1(){cout<<"A::vfunc1()"<<endl;}
virtual void vfunc2(){cout<<"A::vfunc2()"<<endl;}
};
class B:public A{
public:
int m_B;
B(int a,int b):A(a),m_B(b){}
void func1(){cout<<"B::func1()"<<endl;}
void vfunc1(){cout<<"B::vfunc1()"<<endl;}
};
class C:public B{
public:
int m_C;
C(int a,int b,int c):B(a,b),m_C(c){}
void func1(){cout<<"C::func1()"<<endl;}
void vfunc1(){cout<<"C:vfunc1()"<<endl;}
};
void main()
{
cout<<sizeof(A)<<","<<sizeof(B)<<","<<sizeof(C)<<endl;
A a(11);
B b(21,22);
C c(31,32,33);
//输出类的首地址及数据成员地址,验证首地址是vptr地址
cout<<&a<<","<<&(a.m_A)<<endl;
cout<<&b<<","<<&b.m_A<<","<<&b.m_B<<endl;
cout<<&c<<","<<&c.m_A<<","<<&c.m_B<<","<<&c.m_C<<endl;
A*pa=&a;
pa->vfunc1();
pa->vfunc2();
pa->func1();
pa->func2();
cout<<endl;
pa=&b;
pa->vfunc1();
pa->vfunc2();
pa->func1();
pa->func2();
cout<<endl;
pa=&c;
pa->vfunc1();
pa->vfunc2();
pa->func1();
pa->func2();
cout<<endl;
B*pb=&b;
pb->vfunc1();
pb->vfunc2();
pb->func1();
pb->func2();
cout<<endl;
pb=&c;
pb->vfunc1();
pb->vfunc2();
pb->func1();
pb->func2();
cout<<endl;
C*pc=&c;
pc->vfunc1();
pc->vfunc2();
pc->func1();
pc->func2();
}
*014
class shape{
public:
virtual double area()=0;
};
class square:public shape{
protected:
double H;
public:
square(double i){H=i;}
double area(){return H*H;}
};
class circle:public square{
public:
circle(double r):square(r){}
double area(){return H*H*3.14159;}
};
class triangle:public square{
protected:
double W;
public:
triangle(double h,double w):square(h){W=w;}
double area(){return H*W*0.5;}
};
class rectangle:public triangle{
public:
rectangle(double h,double w):triangle(h,w){}
double area(){return H*W;}
};
double total(shape*s[],int n)
{
double sum=0.0;
for(int i=0;i<n;i++)
sum+=s[i]->area();
return sum;
}
#include "iostream"
using namespace std;
void main()
{
shape*s[5];
s[0]=new square(4);
s[1]=new triangle(3,6);
s[2]=new rectangle(3,6);
s[3]=new square(6);
s[4]=new circle(10);
for(int i=0;i<5;i++)
cout<<"s["<<i<<"]="<<s[i]->area()<<endl;
double sum=total(s,5);
cout<<"The total area is:"<<sum<<endl;
}
*015
#include "iostream"//函数指针
using namespace std;
double const STEP=0.1;
double f1(double);
double f2(double);
void main()
{
double x,(*p)(double);//声明函数指针对象
for(int i=0;i<2;i++)
{
if(0==i) p=f1;
else p=f2;
for(x=-1;x<=1;x+=STEP)
cout<<x<<"/t"<<(*p)(x)<<endl;
}
}
double f1(double x)
{return(x*x+5*x+8);}
double f2(double x)
{return(x*x*x-6*x);}
*016
#include "iostream"
using namespace std;
int all(int,int,int(*)(int,int));
int max(int,int),min(int,int),mean(int,int);
void main(){
int a,b;
cin>>a>>b;
cout<<"max="<<all(a,b,max)<<endl;
cout<<"min="<<all(a,b,min)<<endl;
cout<<"mean="<<all(a,b,mean)<<endl;
}
int all(int x,int y,int(*func)(int,int))
{return(*func)(x,y);}
int max(int x,int y)
{return(x>y?x:y);}
int min(int x,int y)
{return(x<y?x:y);}
int mean(int x,int y)
{return(x+y)/2;}
*017
#include "iostream"
using namespace std;
double const s1=0.0;
double const s2=1.0;
double const step=0.01;
double func(double);
double value(double(*)(double));
void main()
{double(*p)(double);
p=func;
cout<<"最小值是:"<<value(p)<<endl;
}
double func(double x)
{return(10*x*x-9*x+2);}
double value(double(*f)(double))
{double x=s1,y=(*f)(x);
while(x<=s2)
{if(y>(*f)(x)) y=(*f)(x);
x+=step;
}
return y;
}
*018
#include "iostream"
using namespace std;
class A{
private:
int val;
public:
A(int i){val=i;}
int value(int a){return val+a;}
};
void main(){
int(A::*pfun)(int);
pfun=A::value;
A obj(10);
cout<<(obj.*pfun)(15)<<endl;
A*pc=&obj;
cout<<(pc->*pfun)(10)<<endl;
}
*019
#include "iostream"
using namespace std;
class base{
public:
virtual void print(){cout<<"In Base"<<endl;}
};
class derived:public base{
public:
void print(){cout<<"In Derived"<<endl;}
};
void display(base*pb,void(base::*pf)())
{(pb->*pf)();}
void main(){
base b;
derived d;
display(&b,base::print);
display(&d,base::print);
}
*020
class Test{
static int x;
int n;
public:
Test(){}
Test(int a,int b){x=a;x=b;}
static int func(){return x;};
static void sfunc(Test&r,int a){r.n=a;}
int Getn(){return n;}
};
int Test::x=25;
#include "iostream"
using namespace std;
void main()
{
cout<<Test::func();
Test b,c;
b.sfunc(b,58);
cout<<" "<<b.Getn();
cout<<" "<<b.func();
cout<<" "<<c.func();
Test a(24,56);
cout<<" "<<a.func()<<" "<<b.func()<<endl;
}
*021
#include "iostream"
using namespace std;
class test{
private:
int n;
public:
test(int i):n(i){cout<<"constructor:"<<endl;}
~test(){cout<<"desructor:"<<n<<endl;}
int getn(){return n;}
void inc(){++n;}
};
void main()
{cout<<"loop start:"<<endl;
for(int i=0;i<3;i++){
static test a(3);
test b(3);
a.inc();
b.inc();
cout<<"a.n="<<a.getn()<<endl;
cout<<"b.n="<<b.getn()<<endl;
}
cout<<"loop end."<<endl;
cout<<"Exit main()"<<endl;
}
*022