|
这是代码#pragma once
#include<iostream>
#include<string.h>
using namespace std;
enum Gendr {
man=0,
nv
};
class Cstudent
{
public:
Cstudent();
Cstudent(const char* _name, int _age, int _id,bool _Gendr);
char* name;
int age;
int id;
bool Gendr;
string _Name;
Cstudent(Cstudent& lei);
~Cstudent();
bool operator==(const Cstudent& stud);
void Name(string _name) {
_Name = _name;
}
virtual void fun() {
cout << "1" << endl;
}
private:
static char xiaozhang[];
};
class Cxiaostudent :public Cstudent{
public:
int shuxue;
int yuwen;
void fun() {
cout << "2" << endl;
}
};
class Cgaostudent :public Cxiaostudent {
public:
int wuli;
int huaxue;
void fun() {
cout << "3" << endl;
}
};
#include "Cstudent.h"
char Cstudent:: xiaozhang[] = "yuantao";
Cstudent::Cstudent()
{
}
Cstudent::Cstudent(const char* _name, int _age, int _id,bool _Gendr) :age(_age), id(_id),Gendr(_Gendr)
{
int sizf = strlen(_name);
name = new char(sizf+1);
strcpy(name, _name);
age = _age;
id = _id;
Gendr = _Gendr;
}
Cstudent::Cstudent(Cstudent & lei)
{
int zof = strlen(lei.name);
name = new char(zof + 1);
strcpy(name, lei.name);
age = lei.age;
id = lei.id;
Gendr = lei.Gendr;
}
Cstudent::~Cstudent()
{
if (name)
{
delete name;
name = NULL;
}
}
bool Cstudent::operator==(const Cstudent & stud)
{
if (stud.id == this->id) {
cout << "对象非法,进行删除" << endl;
return true;
}
else {
cout << "对象合法" << endl;
return false;
}
}
#include"Cstudent.h"
#include<fstream>
int main() {
Cxiaostudent yzz;
Cgaostudent yz;
Cstudent *P=new Cstudent;
P = &yzz;
P->fun();
P = &yz;
P->fun();
delete (Cstudent*)P;
return 0; //每次到这儿的时候就出错
}
很多问题
1.delete了局部变量
2.Cstudent *P=new Cstudent;没有意义 //Cstudent *P;
3.创建子类时,默认调用的是无参构造函数,name未赋值
3.new 子类对象后,delete P指向子类对象时,析构函数需要指定为虚函数
。。。。。。
|
上一篇: 使用urldownloadtofile的问题下一篇: URLdownloadtofile 初始化无法从 无法从“initializer list”转换为“HRESULT”
|