C++程序与运行结果
this->v = rx.v;
p = new char[strlen(rx.p)+1];
strcpy(p,rx.p);
cout<<"Copy ctor X::X(const X&)"<<endl;
}
~X()
{
cout<<"dtor X::~X("<<v<<","<<p<<")"<<endl;
delete []p;
}
void Y::GetPri(X& rx)
{
rx.PriFun();
}
void main()
{
X x1;
Y y1;
y1.GetPri(x1);
GlobalFun(x1);
}
PriFun()
PriFun()
FUNTEMP
#include<iostream.h>
#include<string.h>
template<class T>
{
cout<<"B::Fun()"<<endl;
}
};
class C:public A
{
};
class D:public B,public C
{
};
void main()
{
D d;
d.Fun();// error ,B::Fun() ???? C::Fun()
d.B::Fun();// OK
d.C::Fun();// OK
void Swap(T& a,T& b)
{
T t = a;a=b;b=t;
}
template<class T>
T max(T a,T b)
{
return a>b?a:b;
}
char * max(char *a,char *b)
{
if (strcmp(a,b)>0)
return a;
else
return b;
constructor X::X(10)
M::M(0)
default constructor X::X()
destructor X::~X(0)
M::~M(0)
destructor X::~X(10)
M::~M(10)
EXCEPT
#include<iostream.h>
void G()
{
try
{
throw "Test";
constructor X::X(100)
destructor X::~X(0)
destructor X::~X(100)
destructor X::~X(0)
destructor X::~X(1)
CTORMEM
#include<iostream.h>
class M
{
int v;
public:
M(int v = 0)
{
cout<<"Derived::Derived("<<v<<","<<dv<<")"<<endl;
}
};
void main()
{
Base b(2);
Derived d(2,6);
}
Other::Other
Base::Base(2)
Other::Other
Base::Base(2)
Derived::Derived(2,6)
while (p != NULL)
{
q = p;
p = p->pNext;
dБайду номын сангаасlete q;
}
}
int IsEmpty()
{
return pHead==NULL;
}
void Push(const T& v)
{
Node<T> *p = new Node<T>;
p->value = v;
p->pNext = pHead;
{
private:
int v;
Other o;
public:
Base(int v):v(v)
{
cout<<"Base::Base("<<this->v<<")"<<endl;
}
};
class Derived:public Base
{
private:
int dv;
public:
Derived(int v,int dv):dv(dv),Base(v)
{
this->v = v;
cout<<"M::M("<<v<<")"<<endl;
}
~M()
{
cout<<"M::~M("<<v<<")"<<endl;
}
};
class X
{
int Value;
M member; // member object
public:
X()
{
Value = 0;
cout<<"default constructor X::X()"<<endl;
Class
#include<iostream.h>
class X
{
int Value;
public:
X(int v=0)
{
Value = v;
cout<<"X::X("<<v<<")"<<endl;
}
void Print();
};
void X::Print()
{
cout<<"Value="<<Value<<endl;
catch(Exception* p)
{
cout<<p->errcode<<" "<<p->errmsg<<endl;
}
catch(...)
{
cout<<"Catch exception here !"<<endl;
}
return 0;
}
1 Error
Friend
#include<iostream.h>
}
X(int v):member(v),Value(v)
{
cout<<"constructor X::X("<<v<<")"<<endl;
}
~X()
{
cout<<"destructor X::~X("<<Value<<")"<<endl;
}
};
void main()
{
X x1(10),x2;
}
M::M(10)
MULTINHE
#include<iostream.h>
#include<string.h>
class A
{
int a;
public:
void Fun()
{
cout<<"A::Fun()"<<endl;
}
};
class B:public A
{
public:
void Fun() // override A::Fun();
}
void main()
{
int x=1,y=2;
Swap(x,y);
cout<<"x="<<x<<" y="<<y<<endl;
cout<<"max(x,y)="<<max(x,y)<<endl;
cout<<"max(\"Hello\",\"World\")="<<max("Hello","World")<<endl;
pHead = p;
}
T Pop()
{
if (!IsEmpty())
{
Node<T> *p = pHead;
T v = p->value;
pHead = pHead->pNext;
delete p;
return v;
}
else
return NULL;
}
};
void main()
{
Stack<float> stk;
}
X(int v)
{
Value = v;