나는 다형성에 대해 읽었으며 프로그램을 만들기로 결정했다. 기본 클래스에서 파생 클래스가 오류없이 사용할 수 있도록 추상화했습니다. 하지만 파생 클래스에게이C++ pure virtual void
2 인텔리 말한다 물체 제공 할 때 : 라인 : 추상 클래스 타입의 객체 "아들이"허용되지 않습니다 어쨌든 여기에 38
내 코드
입니다#include <iostream>
#include <string>
using namespace std;
class Father{
protected:
string prof;
string name;
public:
virtual void getProf_getName() = 0; //pure virtual function
virtual void showProf_showName() = 0; //pure virtual function
};
class Son: public Father{
public:
Son(){} //creating constructor
void getProf_getName(string hName, string hProf){
name = hName;
prof = hProf;
}
void showProf_showName(){
cout << "The son name is " << name << " and he is a " << prof << endl;
}
~Son(){cout << "Deleting Son" << endl;} // deleting memory
};
int main(){
//local variables
string name;
string profession;
//User interface
cout << "What is the name of the son: ";
getline(cin,name);
cout << "What is his profession: ";
getline(cin,profession);
//implementing data
Son son; // Error error C2259: 'Son' : cannot instantiate abstract class line:38 column:1
son.getProf_getName(name,profession);
son.showProf_showName();
//showing info
son.showProf_showName();
system("pause");
return 0;
}
당신은 하나의 문제가
'getProf_getName (문자열 hName, 문자열 HPROF가)'getProf_getName'가 아닌 다른 함수는()'이다 다르므로의 구체적인 구현이 없다 이후'Son' 클래스는 여전히 추상적이다 'getProf_getName()'. –
측면 설명으로 다형성을 사용할 때는 항상 가상 소멸자를 사용하십시오. – imreal
또 다른 의견으로, 아들은 정말로 아버지인가? :) – codah