VC驿站

 找回密码
 加入驿站

QQ登录

只需一步,快速开始

搜索
查看: 605|回复: 2

[已解决]42课虚函数的时候创建类指针 最后老是出错

[复制链接]
23_avatar_middle
最佳答案
0 
在线会员 发表于 2020-4-4 16:34:53 | 显示全部楼层 |阅读模式
这是代码#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;  //每次到这儿的时候就出错
}


42课虚函数的时候创建类指针 最后老是出错
最佳答案
15_avatar_small
2020-4-4 20:44:37
很多问题
1.delete了局部变量
2.Cstudent *P=new Cstudent;没有意义   //Cstudent *P;
3.创建子类时,默认调用的是无参构造函数,name未赋值
3.new 子类对象后,delete P指向子类对象时,析构函数需要指定为虚函数
。。。。。。




上一篇:使用urldownloadtofile的问题
下一篇:URLdownloadtofile 初始化无法从 无法从“initializer list”转换为“HRESULT”
15_avatar_middle
最佳答案
6 
online_vip 发表于 2020-4-4 20:44:37 | 显示全部楼层    本楼为最佳答案   
bestAnswer
很多问题
1.delete了局部变量
2.Cstudent *P=new Cstudent;没有意义   //Cstudent *P;
3.创建子类时,默认调用的是无参构造函数,name未赋值
3.new 子类对象后,delete P指向子类对象时,析构函数需要指定为虚函数
。。。。。。

评分

参与人数 1威望 +2 驿站币 +2 热心值 +2 收起 理由
51_avatar_small Syc + 2 + 2 + 2 赞一个!

查看全部评分

23_avatar_middle
最佳答案
0 
ico_lz  楼主| 发表于 2020-4-5 15:31:29 | 显示全部楼层
jadeshu 发表于 2020-4-4 20:44
很多问题
1.delete了局部变量
2.Cstudent *P=new Cstudent;没有意义   //Cstudent *P;

好的  谢谢
您需要登录后才可以回帖 登录 | 加入驿站 qq_login

本版积分规则

×【发帖 友情提示】
1、请回复有意义的内容,请勿恶意灌水;
2、纯数字、字母、表情等无意义的内容系统将自动删除;
3、若正常回复后帖子被自动删除,为系统误删的情况,请重新回复其他正常内容或等待管理员审核通过后会自动发布;
4、感谢您对VC驿站一如既往的支持,谢谢合作!

关闭

站长提醒上一条 /2 下一条

QQ|小黑屋|手机版|VC驿站 ( 辽ICP备09019393号-4 )|网站地图wx_jqr

GMT+8, 2023-4-2 21:14

Powered by CcTry.CoM

© 2009-2021 cctry.com

快速回复 返回顶部 返回列表