本文主要是介绍第十二周 课后实践:阅读程序(1)(2),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
(1)问题及代码:
#include <iostream>
using namespace std;
class A
{
public:
A()
{
a=0;
}
A (int i)
{
a=i;
}
void print()
{
cout<<a<<" ";
}
private:
int a;
};
class B: public A
{
public:
B()
{
b=0;
}
B(int i, int j, int k): A(i),aa(j)
{
b=k;
}
//思考:这3处出现的print,有何区别
void print() //(1)
{
A::print(); //(2)
aa.print(); //(3)
cout<<b<<endl;
}
private:
int b;
A aa;
};
int main()
{
B test[2];
test[0]=B(1,4,7);
test[1]=B(2,5,8);
for(int i=0; i<2; i++)
test[i].print();
return 0;
}
运行结果:
(2)问题及代码:
#include <iostream>
using namespace std;
class A
{
public:
A(char *s)
{
cout<<s<<endl;
}
};
class B:public A
{
public:
B(char *s1, char *s2):A(s1)
{
cout<<s2<<endl;
}
};
class C:public A
{
public:
C(char *s1,char *s2):A(s1)
{
cout<<s2<<endl;
}
};
class D:public B, C
{
public:
D(char *s1,char *s2,char *s3,char *s4):B(s1,s2),C(s3,s4)
{
cout<<s4<<endl;
}
};
int main()
{
D d("class A","class B","class C","class D");
return 0;
}
运行结果:
这篇关于第十二周 课后实践:阅读程序(1)(2)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!