ForThirdWork-No.2:C/C++笔试题回忆并整理

1、运行结果分析题

#include <iostream>

using namespace std;

int main()
{
    char str1[] = "abc" ;  char str2[] = "abc" ;
    const char str3[] = "abc" ; const char str4[] = "abc" ;
    char *str5 = "abc" ; char *str6 = "abc" ;  // warning见截图
    const char *str7 = "abc" ; const char * str8 = "abc" ;
    cout << "( str1 == str2 ):" << ( str1 == str2 ) << std::endl;
    cout << "( str3 == str4 ):" << ( str3 == str4 ) << std::endl;
    cout << "( str5 == str6 ):" << ( str5 == str6 ) << std::endl;
    cout << "( str7 == str8 ):" << ( str7 == str8 ) << std::endl;
    return 0;
}


分析:待


2、long、int 、double 占用的内存空间-->跟平台有关,待测试。

      union变量所占用的内存长度等于最长的成员的长度。


3、运行结果分析题

#include <iostream>

using namespace std;

class A{

};

class B{

    B() ;

    ~B() ;

};

class C{

    C() ;

    virtual ~C() ;

};

int main()

{

    cout << "sizeof( A ):" << sizeof( A ) << endl << "sizeof( B ):" << sizeof( B ) << endl << "sizeof( C ):" << sizeof( C ) << endl ;

    return 0;

}


分析:待


4、运行结果分析题


//qmakeCpp

#include <iostream>

using namespace std;

class A{
private:
    int n1 , n2 ;
public:
    A():n2( 0 ),n1( n2 + 2 ){

    }
    void print(){
        cout << "n1:" << n1 << endl << "n2:" << n2 << endl ;
    }
};

int main()
{
    A a;
    a.print();
    return 0;
}


分析:按照声明的顺序初始化。


5、运行结果分析题



//qmakeCpp

#include <iostream>

using namespace std;

class A{
public:
    A(){
        cout << "A is created" << endl ;
    }
    ~A(){
        cout << "A is destroyed" << endl ;
    }
};

class B:public A{
public:
    B(){
        cout << "B is created" << endl ;
    }
    ~B(){
        cout << "B is destroyed" << endl ;
    }
};

int main()
{
    A *pA = new B() ;
    delete pA ;
    return 0;
}


分析:有什么问题   未调用B的析构函数,会导致内存泄漏。



6、实现strcpy函数


7、实现String类

参见下列博客第2题:http://blog.csdn.net/jandunlab/article/details/16352217


8、程序运行结果题


class A{

    //……

public:

    void f( void ){

        

    }

};

int main()

{

    A *pA = NULL ;

    pA->f();

    return 0;

}


运行时崩溃


9、指针的使用( 略 )



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值