zhzbd
zhzbd
采纳率39.1%
2021-03-04 12:55

能生成成功,却无法显示内容??应该怎么修改代码

10
已结题
//Information.h
#include <iostream>
using namespace std;
class Employee
{ private:
char *m_name;
char *m_no;
public: Employee(char *name,char *no);
		~Employee()
		{delete []m_name;
		delete []m_no;};
		void Display();};
class Leader:public Employee
{ private:
char *m_posdes;
public:
	Leader(char name[50],char no[50],char posdes[50]):Employee( name,no){strcpy_s(m_posdes,50,posdes);};
	~Leader()
	{ delete []m_posdes;}
	void Display();};

//Information.cpp

#include"stdafx.h"
#include"Information.h"
#include<iostream>
using namespace std;

Employee::Employee( char *name,char *no)
{ m_name=new char [50];
 m_no =new char [50];
	strcpy_s(m_name,50,name);
  strcpy_s ( m_no,50,no);
}
void Employee::Display()
{ cout<<"员工的姓名"<<m_name<<endl;
cout<<"员工编号"<<m_no<<endl;}


void Leader::Display()
{ Employee::Display();
cout<<"员工职务为"<<m_posdes<<endl;

//8.1.1.cpp
#include "stdafx.h"
#include"Information.h"
#include<iostream>
using namespace std;

int main()
{  Employee *pe=new Leader("张三","1011","软件开发部部门经理");
pe->Display();
delete pe;
system("pause");
	return 0;
}

  • 点赞
  • 收藏
  • 复制链接分享

3条回答

  • qq_40344524 书山客 1月前
    //Information.h
    #include <iostream>
    using namespace std;
    class Employee
    {
    private:
    	char* m_name;
    	char* m_no;
    public: Employee(char* name, char* no);
    		~Employee()
    		{
    			delete[]m_name;
    			delete[]m_no;
    		};
    		void Display();
    };
    class Leader :public Employee
    {
    private:
    	char* m_posdes;
    public:
    	Leader(char name[50], char no[50], char posdes[50]) :Employee(name, no) {
    		m_posdes = (char*)malloc(50 * sizeof(char));        //需要修改的地方,添加字符串的初始化
    		strcpy_s(m_posdes, 50, posdes); 
    	};
    	~Leader()
    	{
    		delete[]m_posdes;
    	}
    	void Display();
    };
    
    //Information.cpp
    #include"stdafx.h"
    #include"template1.h"
    #include<iostream>
    using namespace std;
    
    Employee::Employee(char* name, char* no)
    {
    	m_name = new char[50];
    	m_no = new char[50];
    	strcpy_s(m_name, 50, name);
    	strcpy_s(m_no, 50, no);
    }
    void Employee::Display()
    {
    	cout << "员工的姓名" << m_name << endl;
    	cout << "员工编号" << m_no << endl;
    }
    
    
    void Leader::Display()
    {
    	Employee::Display();
    	cout << "员工职务为" << m_posdes << endl;
    }
    
    //8.1.1.cpp
    #include "stdafx.h"
    #include"template1.h"
    #include<iostream>
    using namespace std;
    
    int main()
    {
    	char name[50] = "张三";
    	char num[50] = "1011";
    	char position[50] = "软件开发部部门经理";
    	Employee* pe = new Leader(name, num, position);
    	pe->Display();
    	delete pe;
    	system("pause");
    	return 0;
    }
    点赞 评论 复制链接分享
  • qq_34124780 qq_34124780 1月前

    你的第17行strcpy_s(m_posdes,50,posdes);请问你的m_posde初始化在哪里?没有初始化怎么能拷贝呢,能生成不代表运行不会报错啊。加个初始化应该就可以了。

    	Leader(char name[50], char no[50], char posdes[50]) :Employee(name, no) { m_posdes=new char[50]; strcpy_s(m_posdes, 50, posdes); };
    点赞 评论 复制链接分享
  • bosaidongmomo bosaidongmomo 1月前
    class Leader :public Employee
    {
    private:
    	char *m_posdes;
    public:
    	Leader(char name[50], char no[50], char posdes[50]) :Employee(name, no){ m_posdes = new char[50]; strcpy_s(m_posdes, 50, posdes); };
    	~Leader()
    	{
    		delete[]m_posdes;
    	}
    	void Display();
    };
    

    m_posdes 没有初始化变量

    点赞 评论 复制链接分享

为你推荐