要求:1.分别以public和private的方式派生出两个类B,C;
2.在基类A中添加一个虚函数和一个非虚函数,分别在派生类中对该虚函数进行重载;
3.涉及主程序,要求其能分别用一般对象和指针对象实现各类中虚函数的调用和非虚函数的对应调用
C++编程,求各位专家帮帮忙!
答案:1 悬赏:50 手机版
解决时间 2021-06-04 15:44
- 提问者网友:你给我的爱
- 2021-06-03 17:06
最佳答案
- 五星知识达人网友:街头电车
- 2021-06-03 18:08
#include <iostream>
using namespace std;
class A {
public:
void a() { cout << "non-virtual A\n"; }
virtual void b() { cout << "virtual A\n"; }
virtual ~A(){}
};
class B : public A {
public:
void a() { cout << "non-virtual B\n"; }
virtual void b() { cout << "virtual B\n"; }
};
class C : private A {
public:
void a() { cout << "non-virtual C\n"; }
virtual void b() { cout << "virtual C\n"; }
};
int main()
{
A a;
B b;
C c;
A* d[] = { new A, new B };
a.a(); a.b();
b.a(); b.b();
c.a(); c.b();
for ( int i = 0; i < 2; ++i ) {
d[i]->a(); d[i]->b();
}
}
望采纳
using namespace std;
class A {
public:
void a() { cout << "non-virtual A\n"; }
virtual void b() { cout << "virtual A\n"; }
virtual ~A(){}
};
class B : public A {
public:
void a() { cout << "non-virtual B\n"; }
virtual void b() { cout << "virtual B\n"; }
};
class C : private A {
public:
void a() { cout << "non-virtual C\n"; }
virtual void b() { cout << "virtual C\n"; }
};
int main()
{
A a;
B b;
C c;
A* d[] = { new A, new B };
a.a(); a.b();
b.a(); b.b();
c.a(); c.b();
for ( int i = 0; i < 2; ++i ) {
d[i]->a(); d[i]->b();
}
}
望采纳
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯