C++ 练习代码

本文通过两个C++程序实例,探讨了虚函数和迭代器的使用,尽管程序略有不同,但它们的运行结果相同,揭示了C++中这两个核心概念的巧妙结合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include<vector>
using namespace std;

class A{
	private:
		int x,y;
	public:
		A(int x=0,int y=0){
			this->x=x;
			this->y=y;
		}
		
		//输出x y 
		void fun(){
			cout<<x<<","<<y<<endl;
		}
};
class B{
	private:
		vector<A*> objvec;  // 利用迭代器 
	public:
		B(){}
		void add(A *t){
			objvec.push_back(t);
		}
		void show(){
			vector <A *>::iterator ivec;
		    for(ivec=objvec.begin();ivec<objvec.end();ivec++){
		    	(*ivec)->fun();
		    }
		}
		
		//逐一释放每个 
    	~B(){
			vector<A *>::iterator ivec;
			for(ivec=objvec.begin();ivec<objvec.end();ivec++){
				
				delete  *ivec;
	 		}
			
		}
};

int main(int argc, char** argv) {
	

	
	B *pB=new B();
	
	// 没有new  但是利用了析构 
	pB->add(new A(3,5));    
	pB->add(new A(7,8));
	pB->add(new A(9,11));	
	pB->show();
	

	
	return 0;
}

 

 

第一个和第二个程序有点差异运行结果一样

 

#include <iostream>
#include<vector>
using namespace std;

class A{
	private:
		int x,y;
	public:
		A(int x=0,int y=0){
			this->x=x;
			this->y=y;
		}
		
		//输出x y 
		void fun(){
			cout<<x<<","<<y<<endl;
		}
};
class B{
	private:
		vector<A*> objvec;  // 利用迭代器 
	public:
		B(){}
		void add(A *t){
			objvec.push_back(t);
		}
		void show(){
			vector <A *>::iterator ivec;
		    for(ivec=objvec.begin();ivec<objvec.end();ivec++){
		    	(*ivec)->fun();
		    }
		}

int main(int argc, char** argv) {
	
	A *pA1=new A(3,5);
	A *pA2=new A(7,8);
	A *pA3=new A(9,11);
	
	B *pB=new B();
	pB->add(pA1);
	pB->add(pA2);
	pB->add(pA3);	
	pB->show();
	
	delete pA1;
	delete pA2;
	delete pA3;
	delete pB;
	 	
	
	return 0;
}


 

 

 

#include <iostream>

using namespace std; 
class A{
	private:
		   int x,y;    //数据成员xy 
		   A *next;    // 基类指针 
    public:
    	  A(int x,int y,A *t){
    	  	this->x=x;
    	  	this->y=y;
    	  	next=t;
    	  }
    	  virtual void fun(){ //纯虚函数
		
		  	cout<<x<<","<<y<<endl;
		   if(next)
		  	next->fun();
		  }
		   
}; 
class A1:public A{
	public:
		A1(int i,int j,A *n):A(i,j,n){}
	
}; 
class A2:public A{
	public:
		A2(int i,int j,A *n):A(i,j,n){}
	
};
class A3:public A{
	public:
		A3(int i,int j,A *n):A(i,j,n){}
	
	
};
int main(int argc, char** argv) {
	
    A3 *p3=new A3(7,8,NULL);
	A2 *p2=new A2(5,6,p3);
	A  *p =new A1(3,4,p2);
	p->fun();
	 
	delete p3;
	delete p2;
	delete p;
    
	return 0;
}


 

 

#include <iostream>

using namespace std; 
class A{
	private:
		   int x,y;    //数据成员xy 
		   A *next;    // 基类指针 
    public:
    	  A(int x,int y,A *t){
    	  	this->x=x;
    	  	this->y=y;
    	  	next=t;
    	  }
    	  virtual void fun()=0; //纯虚函数
		  void gun(){
		  	cout<<x<<","<<y<<endl;
		   if(next)
		  	next->fun();
		  }
		   
}; 
class A1:public A{
	public:
		A1(int i,int j,A *n):A(i,j,n){}
		void fun(){
			cout<<"调用A1的函数"<<endl;
			gun();    //回基类 
		}
	
}; 
class A2:public A{
	public:
		A2(int i,int j,A *n):A(i,j,n){}
		void fun(){
			cout<<"调用A2的函数"<<endl;
			gun();  //回基类 
		}
	
};
class A3:public A{
	public:
		A3(int i,int j,A *n):A(i,j,n){}
		void fun(){
			cout<<"调用A3的函数"<<endl;
			gun();   //回基类 
		}
	
};
int main(int argc, char** argv) {
	
    A3 *p3=new A3(7,8,NULL);
	A2 *p2=new A2(5,6,p3);
	A  *p =new A1(3,4,p2);
	p->fun();
	 
	delete p3;
	delete p2;
	delete p;
    
	return 0;
}




 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值