#include<iostream>
using namespace std;
class Base{
public:
~Base(){
cout<<"BASE ";
}
};
class Derived:public Base{
public:
~Derived(){
cout<<"DERIVED ";
}
};
int main(){
Derived d;
return 0;
}
这里考察的是继承的析构函数执行顺序是先到自己然后到同类(如果有的话)接着到父类然后到父父类这样子!这里是先执行自己后执行父类
所以是:
DERIVED BASE
程序阅读题:
#include <iostream> // 预处理命令
using namespace std; // 使用标准命名空间std
class MyClass
{
public:
MyClass(){ count++; }
~MyClass(){ count--; }
static int GetCount(){ return count; }
private:
static int count;
};
int MyClass::count = 0; // 初始化静态数据成员
int main(void)
{
MyClass obj1;
cout << MyClass::GetCount() << endl;
MyClass obj2;
cout << MyClass::GetCount() << endl;
MyClass obj3;
cout << obj1.GetCount() << endl;
MyClass *p = new MyClass;
cout << MyClass::GetCount() << endl;
delete p;
cout << MyClass::GetCount() << endl;
return 0; // 返回值0, 返回操作系统
}
上面程序的输出结果为:
1
2
3
4
3
//需要注意MyClass::GetCount()这样调用方法只触发构造函数不会触发析构函数
程序阅读题:
#include <iostream> // 预处理命令
using namespace std; // 使用标准命名空间std
class A
{
public:
A() { cout << "A()" << endl; }
~A() { cout << "~A()" << endl; }
virtual void f() { cout << "A::f()" << endl; }
};
class B: public A
{
public:
B() { cout << "B()" << endl; }
~B() { cout << "~B()" << endl; }
void f() { cout << "B::f()" << endl; }
};
int main(void)
{
B obj;
A *p = &obj;
p->f();
return 0; // 返回值0, 返回操作系统
}
上面程序的输出结果为:
A()
B()
B::f()
~B()
~A()
需要注意:
1.析构函数先调用派生类,后调用基类
2.构造函数调用优先级:基类构造函数总是被优先调用,这说明创建派生类对象时,会先调用基类构造函数,再调用派生类构造函数。
程序输出题:
void fun(int &x, int y)
{ int t=x; x=y; y=t; }
int main( )
{ int a[2] = {23, 42};
fun(a[1],a[0]);
std::cout<<a[0]<<","<<a[1]<<std::endl;
return 0; }
23,23
程序输出题:
#include <iostream>
using namespace std;
class Stock
{
public:
void print()
{cout<<"Stock class.\n";}
};
class Der1_Stock:public Stock
{
public:
void print()
{cout<<"Deri1_Stock class.\n";}
};
class Der2_Stock:public Stock
{
public:
void print()
{cout<<"Deri2_Stock class.\n";}
};
int main(){
Stock s1;
Stock *ptr;
Der1_Stock d1;
Der2_Stock d2;
ptr=&s1;
ptr->print();
ptr=&d1;
ptr->print();
ptr=&d2;
ptr->print();
return 0;
}
Stock class.
Stock class.
Stock class.
stock的print函数不是virtaul所以这样要是有virtaul就是这样
程序输出题:
#include <iostream.h>
class A
{
public:
A(){cout<<"A::A() called.\n";}
virtual ~A(){cout<<"A::~A() called.\n";}
};
class B:public A
{
public:
B(int i){
cout<<"B::B() called.\n";
buf=new char[i];
}
virtual ~B(){
delete []buf;
cout<<"B::~B() called.\n";
}
private:
char *buf;
};
void fun(A *a)
{
delete a;
}
void main()
{
A *a=new B(15);
fun(a);
}
程序输出题:
#include <iostream.h>
#define N 100
class CStack
{
public:
CStack() {top=0;cout<<"Hello ";}
~CStack() {cout<<"Bye";}
void push(int i);
int pop();
private:
int stack[N];
int top;
};
void CStack::push(int i)
{
if (top==N){
cout<<"Overflow";
return;
}
else{
top++;
stack[top]=i;
}
}
int CStack::pop( )
{
int temp;
if (top==0){
cout<<"Underflow";
return 0;
}
else{
temp=stack[top];
top--;
return temp;
}
}
void main()
{
CStack *ptr=new CStack;
ptr->push (10);
ptr->push(50);
cout <<ptr->pop( )<<" ";
cout << "OK!"<<endl;
}
答案:Hello 50 OK!
如果加了delete ptr 则是
程序输出题:
#include <iostream>
using namespace std;
int i = 0;
int fun(int n)
{
static int a = 2;
a++;
return a+n;
}
void main()
{
int k = 5;
{
int i = 2;
k += fun(i);
}
k += fun(i);
cout << k;
}
答案:14
程序输出题:
#include <iostream>
using namespace std;
class Base
{
public:
Base(int i) { cout << i; }
~Base () { }
};
class Base1: virtual public Base
{
public:
Base1(int i, int j=0) : Base(j) { cout << i; }
~Base1() {}
};
class Base2: virtual public Base
{
public:
Base2(int i, int j=0) : Base(j) { cout << i; }
~Base2() {}
};
class Derived : public Base2, public Base1
{
public:
Derived(int a, int b, int c, int d) : mem1(a), mem2(b), Base1(c),
Base2(d), Base(a)
{ cout << b; }
private:
Base2 mem2;
Base1 mem1;
};
void main() { Derived objD (1, 2, 3, 4); }
答案:14302012