代码什么的要哭了 2022-04-26 11:48 采纳率: 67.9%
浏览 39
已结题

C++】class类程序运行结果为空

运行结果为空,麻烦各位uu帮忙看下原因,感谢🙇‍



/*程序运行结果为:
1321    samsung message send*/

//StudybarCommentBegin
#include<iostream>
#include<string.h>
using namespace std;
//#include <iostream.h>
class Telephone
{
protected :
    int number;
public:
    Telephone()
    {number=1234;}
    Telephone(int n)
    {number=n;}
    void showNumber()
    {cout<<"my phone number is: "<<number<<endl;}
    void call()
    {cout<<"the phone is calling \n";}
};
class Mobile:public Telephone
{
    char *type;
    int cost;
public:
    Mobile();
    Mobile(char *t,int c, int n);
    void message()
    {cout<<number<<"\t"<<type<<"\tmessage send\n ";}
    friend int operator<(Mobile &x1,Mobile &x2);
};

//StudybarCommentEnd


Mobile::Mobile(){}

Mobile::Mobile(char *t,int c, int n):Telephone(n)
{
    cost = c;
    strcpy(type,t);        
}

int operator<(Mobile &x1, Mobile &x2)
{
    int result(x1.number>x2.number?x2.number:x1.number);
    return result;
}


//StudybarCommentBegin
int main()
{
    Mobile m1;
    Mobile m2("samsung",3500,1321);
    if(m1<m2)
        m1.message();
    else
        m2.message();
}    

//StudybarCommentEnd
  • 写回答

2条回答 默认 最新

  • 浪客 2022-04-26 12:06
    关注

    char *type; 并没有分配内存,
    strcpy(type,t); 操作不晓得t复制到哪去了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 5月10日
  • 已采纳回答 5月2日
  • 创建了问题 4月26日

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分