C++에서 상속을 배우려고합니다. 가상 기능을 배우기위한 코드를 작성했습니다.C++ 상속 가상 함수
#include <iostream>
using namespace std;
class A {
int a;
public:
A() {}
virtual int get_count() const = 0;
int get_A() { return a; }
};
class B : public A{
public:
int b;
B() {}
B(A& base)
: b(base.get_count()) {}
virtual int get_count() const { return 10; }
};
void func(A& base) {
B derived(base);
cout << derived.b;
}
int main() {
A base;
B derived;
func(derived);
}
내가이 오류를 얻을 컴파일하려고 :
test_inheritance_vir.cpp: In function ‘int main()’:
test_inheritance_vir.cpp:32: error: cannot declare variable ‘base’ to be of abstract type ‘A’
test_inheritance_vir.cpp:5: note: because the following virtual functions are pure within ‘A’:
test_inheritance_vir.cpp:10: note: virtual int A::get_count() const
가 내가 뭘 잘못 말해 주실 수 있습니까?
이것은 문제가되지 않습니다. 그는 메인의 첫 번째 변수로 인스턴스를 만들려고하는데 이것은 논리적 오류입니다. A * 또는 A & and 및 파생 클래스의 인스턴스 여야합니다. – SOReader
** 그가 ** 원하는 ** 때문에 인스턴스를 만드는 경우? :) 그리고 그가 원한다면 - 그는 그것을 할 수 있습니다. –
이면 순전히 가상이 아니어야합니다. 디자인 문제입니다 – SOReader