程序输出
程序输出:
1.
class A{public: virtual void f(){cout<<"A"<<endl;} }
class B{public: virtual void f(){cout<<"B"<<endl;} }
int main()
{
A* pa = new A(); pa->f(); // A
B* pb = (B*)pa; pb->f(); // A,pa转换成B类型只是指针类型变化。
delete pa,pb;
pa = new B(); pa->f(); // B
pb = (B*)pa; pb->f(); // B
}
2.
class A{public: char k[3]; virtual void aa(){};};
class B : public A{public: char j[3]; void bb(){};};
class C : public B{public: char i[3]; void cc(){};};
cout<<sizeof(A)<<endl; // 8
cout<<sizeof(B)<<endl; // 12
cout<<sizeof(C)<<endl; // 16
class A{public: char k[3]; virtual void aa(){};};
class B : public virtual A{public: char j[3]; void bb(){};};
class C : public virtual B{public: char i[3]; void cc(){};};
cout<<sizeof(A)<<endl; // 8
cout<<sizeof(B)<<endl; // 20
cout<<sizeof(C)<<endl; // 32
3.
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; //0
cout<<(str3 == str4)<<endl; //0
cout<<(str5 == str6)<<endl; //1
cout<<(str7 == str8)<<endl; //1
文本常量区中的两个常量值若相等,编译器只会保留一个副本(优化机制)
4.
#include <iostream>
using namespace std;
struct bit
{
int a:3;
int b:2;
int c:3;
};
int _tmain(int argc, _TCHAR* argv[])
{
bit s;
char *c = (char*)&s;
cout<<sizeof(bit)<<endl; // 输出4
*c = 0x99;
cout<<s.a<<endl<<s.b<<endl<<s.c<<endl; // 输出1,-1,-4
system("pause");
return 0;
}
0x99在内存中表示为10011001,故a=001,b=11,c=100,即1,-1,-4
5.
#include "stdafx.h"
#include <iostream>
using namespace std;
struct bit
{
int a:5;
int b:2;
};
int _tmain(int argc, _TCHAR* argv[])
{
bit s;
char cc[100];
strcpy(cc,"0123456789");
memcpy(&s,cc,sizeof(bit)); //sizeof(bit)=4
cout<<s.a<<endl<<s.b<<endl;
system("pause");
return 0;
}
0123在内存中的值为:00110011,00110010,00110001,001 10000
前5位为1000,5到7位为01,即s.a=-16,s.b=1
6.
#include "stdafx.h"
#include <iostream>
using namespace std;
class test
{
public:
test(){};
void hello(){printf("hello\n");}
};
int main(int argc, char* argv[])
{
test *p = new test();
p = NULL;
p->hello();
system("pause");
return 0;
}
输出了hello,只要test类型的指针就能调用test的成员函数,前提是函数里没有涉及到成员变量。
#include "stdafx.h"
#include <iostream>
using namespace std;
class test
{
private:
int i;
public:
test(){i = 1;};
void hello(){printf("%d\n",i);}
};
int main(int argc, char* argv[])
{
test *p = new test();
p = NULL;
p->hello();
system("pause");
return 0;
}
程序崩溃。
7.
#include "stdafx.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
char a;
char *str = &a;
strcpy(str,"hello");
printf(str);
system("pause");
return 0;
}
正常输出hello
#include "stdafx.h"
#include <iostream>
using namespace std;
char a;
int main(int argc, char* argv[])
{
char *str = &a;
strcpy(str,"hello");
printf(str);
system("pause");
return 0;
}
程序崩溃。
#include "stdafx.h"
#include <iostream>
using namespace std;
char a =’a‘;
int main(int argc, char* argv[])
{
char *str = &a;
strcpy(str,"hello");
printf(str);
system("pause");
return 0;
}
正常输出hello
8.
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
using namespace std;
struct bit
{
int a:3;
int b:2;
int c:3;
};
int main(int argc, char* argv[])
{
bit s;
char* c = (char*)&s;
*c = 0x99;
cout<<s.a<<s.b<<s.c<<endl;
system("pause");
return 0;
}
0x99在内存中表示为10011001,a:001, b:11, c:100
故a=1 ,b=-1 ,c=-4
9.
#include "stdafx.h"
#include <iostream>
using namespace std;
union
{
int i;
char x[2];
}a;
int main(int argc, char* argv[])
{
a.x[0] = 10;
a.x[1] = 1;
printf("%d",a.i); // 输出266
printf("%x",a.i); // 输出10A
return 0;
}
低位低地址,高位高地址,内存占用情况为0x010A,即266.
10.
typedef union{long i ; int k[5]; char c}DATA; //sizeof(DATA) = 20
struct data {int cat; DATA cow; double dog;}too; //sizeof(too) = 32
struct A{char a:3; char b:5;}; //sizeof(A)=1
struct B{char a:3; char b:6;}; //sizeof(A)=2
11.
void foo(){unsigned int a = 6; int b = -20; (a+b>6)?puts(">6"):puts("<6");}
输出>6,因为表达式a+b,b被自动转换为unsigned型,-20变成一个非常大的数,a+b>6为真。
12.
class B
{
private:
int data;
public:
B(){cout<<"default constructor"<<endl;}
~B(){cout<<"destructed"<<endl;}
B(int i):data(i){cout<<"constructor"<<data<<endl;}
};
B play(B b){return b;}
int main(int argc, char* argv[])
{
B tmp = play(5);
return 0;
}
在play(5)处,5通过隐含的类型转行调用了B::B(int i),相当于B(5),输出constructed5
play()函数返回时调用B::~B()析构函数,输出destructed
B tmp定义一个B类,tmp的构造函数调用编译器生成的拷贝构造函数 ,返回时输出destructed
13.
class A
{
protected:
int data;
public:
A(int i=0){data=i;}
int GetData(){return doGetData();}
virtual int doGetData(){return data;}
};
class B:public A
{
protected:
int data;
public:
B(int i=1){data=i;}
int doGetData(){return data;}
};
class C:public B
{
protected:
int data;
public:
C(int i=2){data=i;}
};
int main(int argc, char* argv[])
{
C c(10);
cout<<c.GetData()<<endl; //1
cout<<c.A::GetData()<<endl; //1
cout<<c.B::GetData()<<endl; //1
cout<<c.C::GetData()<<endl; //1
cout<<c.A::doGetData()<<endl; //0 直接调用A的doGetData
cout<<c.B::doGetData()<<endl; //1
cout<<c.C::doGetData()<<endl; //1
return 0;
}
14.
int _tmain(int argc, _TCHAR* argv[])
{
int a = 4;
a += (a++);
cout <<a<< endl;
int b = 4;
b += (++b);
cout << b << endl;
int c = 4;
(++c) += (c++);
cout << c << endl;
system("pause");
return 0;
}
输出9,10,11
15.
Void GetMemory(char **p, int num){
*p = (char *)malloc(num);
}
void Test(void){
char *str = NULL;
GetMemory(&str, 100);
strcpy(str, "hello");
printf(str);
}
请问运行Test 函数会有什么样的结果?
答:输出“hello”
16
#include<iostream>
using namespace std;
class MyClass
{
public:
MyClass(int i = 0)
{
cout<<i;
}
MyClass(const MyClass &x)
{
cout<<2;
}
MyClass& operator=(const MyClass &x)
{
cout<<3;
return *this;
}
~MyClass()
{
cout<<4;
}
};
int main()
{
MyClass obj1(1) , obj2(2) , obj3(obj1);
return 0;
}
运行时的输出结果是122444
17.
#include<iostream>
using namespace std;
class A
{
public:
A(int i )
{
cout<<"A ";
}
~A() { }
};
class B
{
public:
B(int j )
{
cout<<"B ";
}
~B() { }
};
class C
{
public:
C(int k )
{
cout<<"C ";
}
~C() { cout<<"~C "; }
};
class D : public C
{
public:
D(int i , int j , int k ) : a(i) , b(j) , C(k)
{
cout<<"D ";
}
~D() { cout<<"~D "; }
private:
B b;
A a;
};
int main()
{
C *pc = new D(1 , 2 , 3);
delete pc;
return 0;
}
输出C B A D ~C
18.
void main()
{
char *x = "abcd";
x += 2;
cout<<x;
}
输出cd
19.
class Name
{
char name[20];
public:
Name()
{
strcpy(name , "");
cout<<'?';
}
Name(char *fname)
{
strcpy(name , fname);
cout<<'?';
}
};
void main()
{
Name names[3] = {Name("张三") , Name("李四") };
}
输出?个数为三个。