C++知识基础题汇总
一、阅读程序,填写输出结果
1)程序
#include<stdio.h>
int main()
{
char a[20]="You_are_a_girl";
char *p=a;
printf怎么加endlchar **ptr=&p;
printf("**ptr=%c\n",**ptr);
ptr++;
printf("**ptr=%c\n",**ptr);
return 0;
}
2)程序
#include<stdio.h>
int main()
{
char str1[] = "abc";
char str2[] = "abc";
const char str3[] = "abc";
const char str4[] = "abc";
const char *str5 = "abc";
const char *str6 = "abc";
char *str7 = "abc";
char *str8 = "abc";
cout << ( str1 == str2 ) << endl;
cout << ( str3 == str4 ) << endl;
cout << ( str5 == str6 ) << endl;
cout << ( str7 == str8 ) << endl;
return 0;
}
3)程序
class CTest
{
public:
CTest(){cout<<"this is CTest"<<endl;}
~CTest(){cout<<"this is ~CTest"<<endl;} };
class CChild:public CTest
{
public:
CChild(){cout<<"this is CChild"<<endl;}
~CChild(){cout<<"this is ~CChild"<<endl;} };
int main()
{
CTest *pTest = new CChild();
delete pTest;
pTest = NULL;
return 0;
}
5)程序
int k = 30;
void main()
{
int k;
300;
=
k
+
k
1;
=
::k
cout << ::k << endl;
}
6)程序
void main()
{
char a='a',b='j';
float x;
x=(b-a)/('F'-'A');
printf("%d\n",(int)(3.14*x));
}
7)程序
void GetMemory(char *p, int num)
{
p = (char *)malloc(num);
}
void main(void)
{
char *str = NULL;
GetMemory(str, 100);
strcpy(str, "hello");
printf(str);
}
8)程序
void main(void)
{
char strOld[5]={'1','2','4','3','5'};
char strNew[10];
cout<<strlen(strOld)<<endl;
strcpy(strNew, strOld);
cout<<strNew<<endl;
}
9)程序
char * Getstr()
{
char *tmp;
tmp = "hello";
return tmp;
}
void main(void)
{
std::cout << Getstr() << std::endl; }
10)程序
#define DOUBLE(x) x+x
void main(void)
{
int i = 5*DOUBLE(5);
cout<<i<<endl;
}
11)程序
void Func(char str[100])
{
printf("%d\n", sizeof(str));
}
void main()
{
char str[] = "world";
cout << sizeof(str) << ": ";
char *p = str;
cout << sizeof(p) << ": ";
char i = 10;
cout << sizeof(i) << ": ";
void *pp = malloc(10);
cout << sizeof(p) << endl;
}
12)程序
#include<stdio.h>
int main()
{
unsigned int a = 6;
-
20;
=
int
b
((a+b )> 6) ? puts("大于6") : puts("小于或等于6");
0;
return
}
13)程序
void value(int nLen)
{
char csValue[100000]={0};
sprintf(csValue, "ok:%i!", nLen);
cout<< csValue <<endl;
if(nLen>10000)
return;
value(nLen+1);
}
int main()
{
value(0);
return 0;
}
14)程序
class CTest
{
public:
CTest(const char *str)
{
=
strlen(str)+1;
nLen
int
m_pStr = new char[nLen];
str);
strcpy(m_pStr,
}
~CTest()
{
m_pStr)
!=
if(NULL
{
m_pStr;
delete
m_pStr = NULL;
}
}
public:
void show(){cout<<m_pStr<<endl;} private:
char *m_pStr;
};
int main()
{
CTest oTest("this is test");
CTest oTest2 = oTest;
oTest2.show();
return 0;
}
15)程序
int main()
{
int a[5] = {1, 2, 3, 4, 5};
int *pa = (int*)(&a+1);
int *pb = (int*)(a+1);
cout<<(*( pa - 1 ))<<endl;
cout<<(*( pb - 1 ))<<endl;
return 0;
}
16)程序
class A
{
public:
virtual void print(void)
{
cout<<"A::print()"<<endl;
}
};
class B:public A
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论