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、指针的使用( 略 )