c++mooc第六章小测

2021-05-11 11:30

阅读:351

标签:turn   mes   int   use   using   ios   fun   print   call   

(红色为补充代码)

001:看上去像多态

#include using namespace std;
class B { 
	private: 
		int nBVal; 
	public: 
		void Print() 
		{ cout "nBVal="; } 
		void Fun() 
		{cout "B::Fun" ; } 
		B ( int n ) { nBVal = n;} 
};
//补充代码
class D:public B
{
private:
	int nDVal;
public:
	D(int nDval_) :nDVal(nDval_),B(3*nDval_){};
	
	void Fun()
	{
		cout "D::Fun" ;
	}
 
	void Print()
	{
		B::Print();
		cout "nDVal=" ;
	}
 
};

int main() { 
	B * pb; D * pd; 
	D d(4); d.Fun(); 
	pb = new B(2); pd = new D(8); 
	pb -> Fun(); pd->Fun(); 
	pb->Print (); pd->Print (); 
	pb = & d; pb->Fun(); 
	pb->Print(); 
	return 0;
}

002:Fun和Do:

#include using namespace std;
class A { 
	private: 
	int nVal; 
	public: 
	void Fun() 
	{ cout "A::Fun" ; }; 
	void Do() 
	{ cout "A::Do" ; } 
}; 
class B:public A { 
	public: 
	virtual void Do() 
	{ cout "B::Do" ;} 
}; 
class C:public B { 
	public: 
	void Do( ) 
	{ cout "C::Do"; } 
	void Fun() 
	{ cout "C::Fun" ; } 
}; 
void Call(
//补充代码
B &p ) { p.Fun(); p.Do(); } int main() { C c; Call( c); return 0; }

003:这是什么鬼delete

#include using namespace std;
class A 
{ 
public:
	A() { }
//补充代码 virtual ~A(){ cout"destructor A"; } }; class B:public A { public: ~B() { cout "destructor B" ; } }; int main() { A * pa; pa = new B; delete pa; return 0; }

004:怎么又是Fun和Do

#include using namespace std;
class A {
	private:
	int nVal;
	public:
	void Fun()
	{ cout "A::Fun" ; };
	virtual void Do()
	{ cout "A::Do" ; }
};
class B:public A {
	public:
	virtual void Do()
	{ cout "B::Do" ;}
};
class C:public B {
	public:
	void Do( )
	{ cout "C::Do"; }
	void Fun()
	{ cout "C::Fun" ; }
};
void Call(
//补充代码 A*p ) { p->Fun(); p->Do(); } int main() { Call( new A()); Call( new C()); return 0; }
 
 

c++mooc第六章小测

标签:turn   mes   int   use   using   ios   fun   print   call   

原文地址:https://www.cnblogs.com/gongsuiqing/p/13152628.html


评论


亲,登录后才可以留言!