918c++ 容易混淆程序结果输出题

本文通过实例详细阐述C++中继承类析构函数的执行顺序,以及构造函数的调用规则。程序示例展示了派生类与基类构造和析构的顺序,同时探讨了静态成员变量和动态内存分配在对象生命周期中的影响。通过对这些基础知识的掌握,有助于深入理解C++的类和对象机制。
摘要由CSDN通过智能技术生成
#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
在这里插入图片描述
在这里插入图片描述

  • 6
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值