笔试---C++

1.class和struct的默认权限分别是什么?

class:private

struct:public

2.const和static的作用,说的越多越好

const的了解-CSDN博客

static的了解-CSDN博客

3.c语言中链表

struct node{
int value;
struct node * next;
}
typedef struct node node;

现在给出链表的头部head,代码实现该链表的翻转。

4.发现代码的错误。

(1)

void test1()
{
    char string[10];
    char* str1="0123456789";
    strcpy(string,str1);
}

str1长度为11(加上'\0')

string长度只有10,所以无法存储'\0',输出时会有乱码。 

(2)

void GetMemory(char *p)
{
    p=(char *)malloc(100);
}
void Test(void){
    char * str=NULL;
    GetMemory(str);
    strcpy(str,"hello world");
    printf(str);
}

测试:

void GetMemory(char *p)
{
    qDebug()<<"p:"<<&p;
    p=(char *)malloc(100);
}
void Test(void){
    char * str=NULL;
    qDebug()<<"str:"<<&str;
    GetMemory(str);
    if(str==NULL){
        qDebug()<<"NULL";
    }
    strcpy(str,"hello world");
    printf(str);
    qDebug()<<str;
}

输出: 

str: 0x65fe14
p: 0x65fe00
NULL

C++——函数传参三种形式(包含形参与实参)_c++形参到实参的传递方式-CSDN博客

形参是额外分配的内存

 (3)

char * GetMemory(void)
{
    char p[]="hello world";
    return p;
}
void test(void){
    char * str=NULL;
    str=GetMemory();
}

return p;返回的是字符数据p的地址,函数调用结束后,该数组被销毁。

返回的指针是一个已经释放了空间的指针。 

char * GetMemory(void)
{
    char *p="hello world";
    return p;
}

先定义一个字符串常量“hello world”,指针p指向它,字符串常量在编译时分配内存,程序退出时才被销毁。 

 测试:

char * GetMemory(void)
{
    char p[]="hello world";
    qDebug()<<"p:"<<&p;
    return p;
}
void test(void){
    char * str=NULL;
    qDebug()<<"str:"<<&str;
    str=GetMemory();
    qDebug()<<"str:"<<&str;
}

 输出:

str: 0x65fe14
p: 0x65fdcc
str: 0x65fe14 

c函数中返回字符串数组、char[]和char*的区别与转换(详细)_char *和char[]返回值的区别-CSDN博客

(C++)三种常用的字符串表示方法——char* 和 char[] - 小念子 - 博客园 (cnblogs.com)

C++ 指针与取地址&_c++中 & 取地址-CSDN博客

5.写出输出的内容。

class A{
public:
    A(){std::cout<<"A:"<<std::endl;}
    ~A(){std::cout<<"~A:"<<std::endl;}
    virtual void func(){std::cout<<"AFunc:"<<std::endl;}
};
class B:public A
{
    int mmm;
public:
    B(){std::cout<<"B:"<<std::endl;}
    ~B(){std::cout<<"~B:"<<std::endl;}
    void func(){std::cout<<"BFunc:"<<std::endl;}
};


A * a=new B();
a->func();

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lpl还在学习的路上

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值