64,680
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
class IStudent
{
public:
virtual ~IStudent() = 0;
virtual const char* GetName() = 0;
virtual int GetAge() = 0;
};
class CStudent : public IStudent
{
public:
CStudent(char *strName, int nAge)
: m_strName(strName)
, m_nAge(nAge)
{
}
virtual ~CStudent()
{
}
virtual const char* GetName() const
{
return m_strName.c_str();
}
virtual int GetAge() const
{
return m_nAge;
}
private:
string m_strName;
int m_nAge;
};
int main()
{
const char *strName = "LiLei";
IStudent *pStudent = new CStudent(strName, 24);
cout << "Name: " << pStudent->GetName() << ", Age: " << pStudent->GetAge() << endl;
return 0;
}
CStudent(char *strName, int nAge)
: m_strName(strName)
, m_nAge(nAge)
IStudent *pStudent = new CStudent(strName, 24);
class IStudent
{
public:
virtual ~IStudent(){};
virtual const char* GetName() = 0;
virtual int GetAge() = 0;
};
class CStudent : public IStudent
{
public:
CStudent(const char *strName, int nAge)
: m_strName(strName)
, m_nAge(nAge)
{
}
virtual ~CStudent()
{
}
virtual const char* GetName()
{
return m_strName.c_str();
}
virtual int GetAge()
{
return m_nAge;
}
private:
string m_strName;
int m_nAge;
};
int main()
{
const char *strName = "LiLei";
IStudent *pStudent = new CStudent(strName, 24);
cout << "Name: " << pStudent->GetName() << ", Age: " << pStudent->GetAge() << endl;
return 0;
}
virtual ~IStudent() = 0;//不能声明为纯虚。因为派生类的析构函数会自动调用基类的析构函数,那样连接器会找不到基类的析构函数。
virtual const char* GetName() const//子类和父类函数类型不一致,去掉const
virtual const char* GetName()
const char *strName = "LiLei";//函数参数类型不匹配
char *strName = "LiLei";
IStudent *pStudent = new CStudent(strName, 24);//IStudent含有纯虚函数则不能定义相应对象
CStudent *pStudent = new CStudent(strName, 24);
Error 1 error C2259: 'CStudent' : cannot instantiate abstract class c:\documents and settings\administrator\桌面\mfc程序\vs2005\vs2005.cpp 48
Error 2 error C2664: 'CStudent::CStudent(char *,int)' : cannot convert parameter 1 from 'const char *' to 'char *' c:\documents and settings\administrator\桌面\mfc程序\vs2005\vs2005.cpp 48
#include <iostream>
#include <string>
using namespace std;
class IStudent
{
public:
//virtual ~IStudent() = 0;
virtual const char* GetName() = 0;
virtual int GetAge() = 0;
};
class CStudent : public IStudent
{
public:
CStudent(char *strName, int nAge)
: m_strName(strName)
, m_nAge(nAge)
{
}
virtual ~CStudent()
{
}
virtual const char* GetName()
{
return m_strName.c_str();
}
virtual int GetAge()
{
return m_nAge;
}
private:
string m_strName;
int m_nAge;
};
int main()
{
char *strName = "LiLei";
IStudent *pStudent = new CStudent(strName, 24);
cout << "Name: " << pStudent->GetName() << ", Age: " << pStudent->GetAge() << endl;
delete pStudent;
return 0;
}
class IStudent
{
public:
virtual ~IStudent() = 0;
virtual const char* GetName() = 0;
virtual int GetAge() = 0;
};
class CStudent : public IStudent
{
public:
CStudent(char *strName, int nAge)
: m_strName(strName)
, m_nAge(nAge)
{
}
virtual ~CStudent()
{
}
virtual const char* GetName()
{
return m_strName.c_str();
}
virtual int GetAge()
{
return m_nAge;
}
private:
string m_strName;
int m_nAge;
};
void main()
{
char *strName = "LiLei";
IStudent *pStudent = new CStudent(strName, 24);
cout << "Name: " << pStudent->GetName() << ", Age: " << pStudent->GetAge() << endl;
delete pStudent;
system("pause");
return;
}