C++程序与运行结果
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
{
cout<< stk2.Pop()<<endl;
}
}
9.2 8.2 7.2 6.2 5.2 4.2 3.2 2.2 1.2 0.2 9 8 7 6 5 4 3 2 1 0
COPYCTOR
#include<iostream.h>
#include<string.h>
class X
{
private:
int v;
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)
#include<string.h>
class X;//forward declaration
class Y
{
public:
void GetPri(X& rx);
};
class X
{
private:
void PriFun();
publLeabharlann Baiduc:
void PubFun();
friend void Y::GetPri(X& rx);// class function
char *p;
public:
X(int v=0,char* s="Noname")
{
this->v = v;
p = new char[strlen(s)+1];
strcpy(p,s);
cout<<"ctor X::X("<<v<<","<<p<<")"<<endl;
}
X(const X& rx)
{
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();
while (p != NULL)
{
q = p;
p = p->pNext;
delete q;
}
}
int IsEmpty()
{
return pHead==NULL;
}
void Push(const T& v)
{
Node<T> *p = new Node<T>;
p->value = v;
p->pNext = pHead;
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;
friend void GlobalFun(X& rx);// Global function
};
void GlobalFun(X& rx)
{
rx.PriFun();
}
void X::PriFun()
{
cout<<"PriFun()"<<endl;
}
void X::PubFun()
{
cout<<"PubFun()"<<endl;
}
x=2 y=1
max(x,y)=2
max("Hello","World")=World
INHERIT
#include<iostream.h>
#include<string.h>
class Other
{
public:
Other()
{
cout<<"Other::Other"<<endl;
}
};
class Base
{
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
{
public:
int errcode;
char errmsg[20];
Exception(int code,char *msg)
{
errcode = code;
strcpy(errmsg,msg);
}
};
int main()
{
try
{
throw new Exception(1,"Error");
}
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;
}
catch(const char* s)
{
cout<<s<<endl;
}
catch(...)
{
cout<<"Catch exception in G()"<<endl;
}
}
void F()
{
int i= 3;
G();
throw i;
}
int main()
{
try
{
F();
}
catch(int k)
{
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)
}
void main()
{
X x1,x2(4);
X *p;
p = &x2;
X& r = x1;
x1.Print();
p->Print();
r.Print();
}
X::X(0)
X::X(4)
Value=0
Value=4
Value=0
CLSTEMP
#include<iostream.h>
template<class T>
{
cout<<"Int "<<k<<endl;
}
catch(...)
{
cout<<"Catch exception here !"<<endl;
}
return 0;
}
Test
Int 3
Except2
#include<iostream.h>
#include<string.h>
class Exception
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;
{
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;
}
};
X Fun(X x2)
{
X x3(x2);
return x3;
}
void main()
{
X x1(1,"Wang");
Fun(x1);
}
ctor X::X(1,Wang)
Copy ctor X::X(const X&)
Copy ctor X::X(const X&)
Copy ctor X::X(const X&)
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;
{
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)
catch(Exception* p)
{
cout<<p->errcode<<" "<<p->errmsg<<endl;
}
catch(...)
{
cout<<"Catch exception here !"<<endl;
}
return 0;
}
1 Error
Friend
#include<iostream.h>
}
OPERATOR
#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)
Stack<int> stk2;
int i;
for (i = 0;i<10;i++)
{
stk.Push(i+0.2);
}
while (!stk.IsEmpty())
{
cout<< stk.Pop()<<endl;
}
for (i = 0;i<10;i++)
{
stk2.Push(i+0.2);
}
while (!stk2.IsEmpty())
Fun();
X * p = new X;
X* p2 = new X(100);
delete p;
delete p2;
}
constructor X::X(1)
default constructor X::X()
constructor X::X(10)
destructor X::~X(10)
default constructor X::X()
}
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;
dtor X::~X(1,Wang)
dtor X::~X(1,Wang)
dtor X::~X(1,Wang)
dtor X::~X(1,Wang)
CTORDTOR
#include<iostream.h>
class X
{
int Value;
public:
X()
{
Value = 0;
cout<<"default constructor X::X()"<<endl;
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";
}
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>
struct Node
{
T value;
Node* pNext;
};
template<class T>
class Stack
{
private:
Node<T> * pHead;
public:
Stack():pHead(NULL)
{
}
~Stack()
{
Node<T> *p=pHead;
Node<T> *q;
}
X(int v)
{
Value = v;
cout<<"constructor X::X("<<v<<")"<<endl;
}
~X()
{
cout<<"destructor X::~X("<<Value<<")"<<endl;
}
};
void Fun()
{
X x(10);
}
void main()
{
X x1(1),x2;
cout<< stk2.Pop()<<endl;
}
}
9.2 8.2 7.2 6.2 5.2 4.2 3.2 2.2 1.2 0.2 9 8 7 6 5 4 3 2 1 0
COPYCTOR
#include<iostream.h>
#include<string.h>
class X
{
private:
int v;
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)
#include<string.h>
class X;//forward declaration
class Y
{
public:
void GetPri(X& rx);
};
class X
{
private:
void PriFun();
publLeabharlann Baiduc:
void PubFun();
friend void Y::GetPri(X& rx);// class function
char *p;
public:
X(int v=0,char* s="Noname")
{
this->v = v;
p = new char[strlen(s)+1];
strcpy(p,s);
cout<<"ctor X::X("<<v<<","<<p<<")"<<endl;
}
X(const X& rx)
{
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();
while (p != NULL)
{
q = p;
p = p->pNext;
delete q;
}
}
int IsEmpty()
{
return pHead==NULL;
}
void Push(const T& v)
{
Node<T> *p = new Node<T>;
p->value = v;
p->pNext = pHead;
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;
friend void GlobalFun(X& rx);// Global function
};
void GlobalFun(X& rx)
{
rx.PriFun();
}
void X::PriFun()
{
cout<<"PriFun()"<<endl;
}
void X::PubFun()
{
cout<<"PubFun()"<<endl;
}
x=2 y=1
max(x,y)=2
max("Hello","World")=World
INHERIT
#include<iostream.h>
#include<string.h>
class Other
{
public:
Other()
{
cout<<"Other::Other"<<endl;
}
};
class Base
{
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
{
public:
int errcode;
char errmsg[20];
Exception(int code,char *msg)
{
errcode = code;
strcpy(errmsg,msg);
}
};
int main()
{
try
{
throw new Exception(1,"Error");
}
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;
}
catch(const char* s)
{
cout<<s<<endl;
}
catch(...)
{
cout<<"Catch exception in G()"<<endl;
}
}
void F()
{
int i= 3;
G();
throw i;
}
int main()
{
try
{
F();
}
catch(int k)
{
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)
}
void main()
{
X x1,x2(4);
X *p;
p = &x2;
X& r = x1;
x1.Print();
p->Print();
r.Print();
}
X::X(0)
X::X(4)
Value=0
Value=4
Value=0
CLSTEMP
#include<iostream.h>
template<class T>
{
cout<<"Int "<<k<<endl;
}
catch(...)
{
cout<<"Catch exception here !"<<endl;
}
return 0;
}
Test
Int 3
Except2
#include<iostream.h>
#include<string.h>
class Exception
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;
{
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;
}
};
X Fun(X x2)
{
X x3(x2);
return x3;
}
void main()
{
X x1(1,"Wang");
Fun(x1);
}
ctor X::X(1,Wang)
Copy ctor X::X(const X&)
Copy ctor X::X(const X&)
Copy ctor X::X(const X&)
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;
{
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)
catch(Exception* p)
{
cout<<p->errcode<<" "<<p->errmsg<<endl;
}
catch(...)
{
cout<<"Catch exception here !"<<endl;
}
return 0;
}
1 Error
Friend
#include<iostream.h>
}
OPERATOR
#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)
Stack<int> stk2;
int i;
for (i = 0;i<10;i++)
{
stk.Push(i+0.2);
}
while (!stk.IsEmpty())
{
cout<< stk.Pop()<<endl;
}
for (i = 0;i<10;i++)
{
stk2.Push(i+0.2);
}
while (!stk2.IsEmpty())
Fun();
X * p = new X;
X* p2 = new X(100);
delete p;
delete p2;
}
constructor X::X(1)
default constructor X::X()
constructor X::X(10)
destructor X::~X(10)
default constructor X::X()
}
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;
dtor X::~X(1,Wang)
dtor X::~X(1,Wang)
dtor X::~X(1,Wang)
dtor X::~X(1,Wang)
CTORDTOR
#include<iostream.h>
class X
{
int Value;
public:
X()
{
Value = 0;
cout<<"default constructor X::X()"<<endl;
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";
}
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>
struct Node
{
T value;
Node* pNext;
};
template<class T>
class Stack
{
private:
Node<T> * pHead;
public:
Stack():pHead(NULL)
{
}
~Stack()
{
Node<T> *p=pHead;
Node<T> *q;
}
X(int v)
{
Value = v;
cout<<"constructor X::X("<<v<<")"<<endl;
}
~X()
{
cout<<"destructor X::~X("<<Value<<")"<<endl;
}
};
void Fun()
{
X x(10);
}
void main()
{
X x1(1),x2;