标题: 大家帮忙,读一下程序,谢谢 [打印本页] 作者: 蓳隯Д麞帥 时间: 2009-11-3 04:11 标题: 大家帮忙,读一下程序,谢谢 本人初学C++,不太懂,有两个小程序就读懂,请高手帮忙读一下,最好能给出输出结果和讲解一下!谢谢!
1、
#include<iostream.h>
class A
{public: A(){cout<<"A";}
~A(){cout<<"~A";}};
class B: public A
{public: B(){cout<<"B";}
~B(){cout<<"~B";}
};
class c:public B
{public: c(){cout<<"c";}
~C(){cout<<"~C";}
private: Am; Bn;
};
A obj;
int main()
{ C*P=new C; cout<<endl;
B obj; cout<<endl;
delete P; cout<<endl;
return 0;
}
2、
#include<iostream.h>
class A {public: virtual void output() {cout<<"class A\n";}};
class B:public A {viod output() {cout<<"class B\n";}};
class C:public A {void output() {cout<<"class C\n";}};
class D:public C { Cm;};
class E:public B{void output() {cout<<"class E\n";} Bm;};
int main()
{ Aa;Bb;Cc;Dd;Ee;
A*P[]={&a,&b,&c,&d,&e};
A v[]={a,b,c,d,e};
for (int i=0;i<5;i++) {P[ i ] ->output();v[ i ].output();}
return 0;
}作者: 炫夜 时间: 2009-11-3 04:11
#include<iostream.h>
class A
{public: A(){cout<<"A";}
~A(){cout<<"~A";}};
class B: public A
{public: B(){cout<<"B";}
~B(){cout<<"~B";}
};
class c:public B
{public:
c(){cout<<"C";}
~c(){cout<<"~C";}
private: int Am;
int Bn;
};
// A obj;
int main()
{ c *P=new c; cout<<endl;
B obj; cout<<endl;
delete P; cout<<endl;
return 0;
}
注意大小写.还有就是实例化对象的时候顺序问题,先基类,再子类,释放时候,顺序相反作者: ★真的爱你★ 时间: 2009-11-3 04:11
后一个是动态编连里面的概念.虚函数,用指针做的,还有用对象应用也可以实现.你暂时可以把基础先看好...