标签:析构函数 静态 target 结果 ret virtual ppa div include
1.
class CParent { public : virtual void Intro() { printf( "I‘m a Parent, " ); Hobby(); } virtual void Hobby() { printf( "I like football!" ); } }; class CChild : public CParent { public : virtual void Intro() { printf( "I‘m a Child, " ); Hobby(); } virtual void Hobby() { printf( "I like basketball!\n" ); } }; int main( void ) { CChild *pChild = new CChild(); CParent *pParent = (CParent *) pChild; pParent->Intro(); return ( 0 ); }
|
解析:
上行转换 但是指针指向没变 然后调用子类重载 所以答案是 Im a Child I like footbal!
2.
解析: 类静态成员 在外面初始化 要指定是哪个类 及 Fred:: 同时省略static关键字
3.
class
Test{
public
:
int
a;
int
b;
virtual
void
fun() {}
Test(
int
temp1 = 0,
int
temp2 = 0)
{
a=temp1 ;
b=temp2 ;
}
int
getA()
{
return
a;
}
int
getB()
{
return
b;
}
};
int
main()
{
Test obj(5, 10);
// Changing a and b
int
* pInt = (
int
*)&obj;
*(pInt+0) = 100;
*(pInt+1) = 200;
cout <<
"a = "
<< obj.getA() << endl;
cout <<
"b = "
<< obj.getB() << endl;
return
0;
}
4.
链接:https://www.nowcoder.com/questionTerminal/ac7f182a41dd4c52b8895fcaf151e92d
来源:牛客网
class A { public : A() { } ~A() { cout<< "~A" <<endl; } }; class B: public A { public : B(A &a):_a(a) { } ~B() { cout<< "~B" <<endl; } private : A _a; }; int main( void ) { A a; //很简单,定义a的时候调用了一次构造函数 B b(a); } |
# include <iostream> using namespace std; class A { public : A() { cout<< "create A" <<endl; } A( const A& other){ cout<< "copy A" <<endl;} //复制构造函数 ~A() { cout<< "~A" <<endl; } }; class C { public : C() { cout<< "create C" <<endl; } C( const A& other){ cout<< "copy C" <<endl;} //复制构造函数 ~C() { cout<< "~C" <<endl; } }; class B: public A { public : B() { cout<< "create B" <<endl; } ~B() { cout<< "~B" <<endl; } private : C _a; }; int main( void ) { B b; cout<< "------------------------" <<endl; } |
create A
create C
create B
------------------------
~B
~C
~A
标签:析构函数 静态 target 结果 ret virtual ppa div include
原文地址:https://www.cnblogs.com/MengX/p/12316077.html