class A
{
private:
static int c_count;
public:
A()
{
c_count++;
}
~A()
{
c_count--;
}
static void Count()
{
cout<<c_count<<endl;
}
};
int A::c_count = 0;
int main()
{
A* a = static_cast<A*>(malloc(sizeof(A)));
a->Count();
delete a;
a->Count();
return 0;
}
结果:0 -1
a指向一段内存,非指向对象,不调用构造函数
调用静态成员只使用类型,delete后a为野指针,但所指向类型仍明确,等价于A::Count()
cout<<sizeof(A)<<endl; //1
——————/
class A
{
public:
virtual void test(int i)
{
cout<<"A::test"<<i<<endl;
}
void test()
{
cout<<"A::test"<<endl;
}
};
class B : public A
{
public:
void test(double i)
{
cout<<"B::test"<<i<<endl;
}
};
int main()
{
A* a = new B(); //只能看到void test(int i)
B* b = new B(); //只能看到test(double i)
a->test(5); //没有发生多态
b->test(5); //发生覆盖,子类test将父类俩test均覆盖
return 0;
}
结果:A::test5 B::test5
发生多态前提函数签名完全相同
——————/
虚函数:有函数体,实现多态
纯虚函数:无函数体,实现接口
class A
{
private:
char i;
public:
virtual void test()
{
cout<<"A::test"<<endl;
}
};
class B : public A
{
private:
int i;
public:
void test()
{
cout<<"B::test"<<endl;
}
};
void f(A* p, int len)
{
for(int i=0; i<len; i++)
{
p[i].test();
}
}
int main()
{
cout<<sizeof(A)<<sizeof(B)<<endl;
B b[3];
f(b, 3);
return 0;
}
32位机打印一次B::test后,程序跑分
因p[i]一次前进sizeof(A),小于实际sizeof(B)
不要在数组上运用多态概念
——————/
描述正确的是:( )
A. 一个应用程序启动后成为一个进程
B. 进程是操作系统分配资源的基本单位
C. 一个进程中可以创建多个线程,每个线程都共享进程的资源
D. 操作系统可以在创建进程的时不创建任何一个线程
main由进程主线程执行
ABC
——————/
class A
{
private:
static int i;
public:
A()
{
i++;
cout<<"A()"<<endl;
}
A(const A&)
{
i++;
cout<<"A(const A&)"<<endl;
}
static void output()
{
cout<<i<<endl;
}
};
int A::i = 0;
A f(A& a)
{
A aa = a; //调用拷贝构造函数
return a;
}
int main()
{
A a;
f(a).output();
return 0;
}
结果:
A()
A(const A&)
A(const A&)
3
output静态函数,通过临时对象类型调用
return a时,用对象a初始化一临时对象,故…
——————/
#define FUNC(a, b) do{ a = a + b; \
b = a - b; \
a = a – b; \
}wile(0) //避免宏的副作用
Telnet协议基于TCP协议开发而来
——————/
中断服务程序ISR(非OS情况下)需满足如下要求:
(1)不能返回值;
(2)不能向ISR传递参数; //利用全局变量
(3)ISR应该尽可能的短小精悍;
(4)printf(char * lpFormatString,…)函数会带来重入和性能问题,不能在ISR中采用
——————/
一组整型数,其中除2个数字以外其它数字都成对出现,编程找出。
int first_one_bit(unsigned int v)
{
int ret = 0;
while( (v != 0) && ((v & 1) == 0) )
{
v = v >> 1;
ret++;
}
return ret;
}
void search_diff(int*array, int len, int*pa, int*pb)
{
int r = 0;
int flag = 0;
for(int i=0; i<len; i++)
r = r ^ array[i]; //r=a^b,异或
flag = 1 << first_one_bit(r);
for(int i=0; i<len; i++)
{
if( array[i] & flag )
*pa = *pa ^ array[i];
else
*pb = *pb ^ array[i];
}
}
int main()
{
int a = 0;
int b = 0;
int array[] = {3, 4, 5, 5, 3, 4, 1, 6, 6, 7, 2, 8, 7, 8};
search_diff(array, sizeof(array)/sizeof(*array), &a, &b);
cout<<a<<" "<<b<<endl;
}
如使用先排序再挑单O(nlogn),即便编程正确也得不了高分,本法为O(n)
——————/
打印一N * N矩阵,从首坐标(0, 0)开始顺时针依次增大,如:
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9 //考编程能力,先生成再打印
答案:
template <int N>
class SpinMatrix
{
private:
int m_matrix[N][N];
struct Offset
{
int dx;
int dy;
};
bool valid(int x, int y);
public:
SpinMatrix();
void run();
void println();
};
template <int N>
SpinMatrix<N>::SpinMatrix() //将所有元素初始化为0
{
for(int i=0; i<N; i++)
{
for(int j=0; j<N; j++)
{
m_matrix[i][j] = 0;
}
}
}
template <int N>
bool SpinMatrix<N>::valid(int x, int y)
{
bool ret = true;
ret = ret && ((0 <= x) && (x < N));
ret = ret && ((0 <= y) && (y < N));
ret = ret && (m_matrix[x][y] == 0);
return ret;
}
template <int N>
void SpinMatrix<N>::run()
{
const Offset OFFSET[] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
const int OSLEN = sizeof(OFFSET) / sizeof(*OFFSET);
int cx = 0;
int cy = 0;
int cd = 0;
int i = 1;
do
{
m_matrix[cx][cy] = i;
if( !valid(cx + OFFSET[cd].dx, cy + OFFSET[cd].dy) )
{
cd = (cd + 1) % OSLEN;
}
cx += OFFSET[cd].dx;
cy += OFFSET[cd].dy;
i++;
} while ( i <= N*N );
}
template <int N>
void SpinMatrix<N>::println()
{
for(int i=0; i<N; i++)
{
for(int j=0; j<N; j++)
{
cout<<m_matrix[i][j]<<'\t';
}
cout<<endl;
}
cout<<endl;
}
int main()
{
SpinMatrix<4> sm;
sm.run();
sm.println();
}
——————/
下面代码是否有错?如有错,错在哪里?
struct Test
{
Test() {}
Test(int i) {}
void func() {}
};
int main()
{
Test t1(1);
Test t2();
t1.func();
t2.func();
}
①没有return语句
②Test t2();变成函数声明
——————/
class Test
{
int m_i;
int m_j;
public:
Test(int v) : m_j(v), m_i(m_j) //初始化列表
{}
int getI()
{ return m_i; }
int getJ()
{ return m_j; }
};
int main()
{
Test t1(1);
Test t2(2);
cout<<t1.getI()<<" "<<t1.getJ()<<endl;
cout<<t2.getI()<<" "<<t2.getJ()<<endl;
}
结果:
0 1
0 2
不同编译器,m_i值可能为随机数
因初始化列表执行顺序为变量定义顺序,若:
int m_j;
int m_i;
结果:
1 1
2 2
——————/
class Test
{
public:
Test(const Test&)
{
cout<<"Test(const Test&)"<<endl;
}
Test()
{
cout<<"Test()"<<endl;
}
Test(int v)
{
cout<<"Test(int v)"<<endl;
}
~Test()
{
cout<<"~Test()"<<endl;
}
};
Test Play(Test t)
{
return t;
}
int main()
{
Test t = Play(5);
}
结果:
Test(int v)
Test(const Test&)
~Test()
~Test()
——————/
const T func(); //表返回const类型
T func() const; //表该函数不能改变该类非静态成员的值
——————/
class Base
{
public:
virtual void func()
{
cout<<"Base::func()"<<endl;
}
};
class Child : public Base
{
public:
void func()
{
cout<<"Child::func()"<<endl;
}
};
int main()
{
Base* pb = new Base();
pb->func();
Child* pc = (Child*)pb;
pc->func();
}
打印两行Base::func(),程序不会崩溃,因pb与pc指向同一父类对象,通过虚函数表找到func同(该写法是玩火)
——————/
A C++ developer wants to handle a static _cast<char*>() operation for the String class shown below. Which of the following options are valid declarations that will accomplish this task?
即
class String
{
public:
operator char*() //考察类型转换函数
{
cout<<"operator char*()"<<endl;
return NULL;
}
};
int main()
{
String s;
char*p=static_cast<char*>(s);
return 0;
}
——————/
(1) new一个10个元素的数组
(2) 分10次new一个整型变量
哪个占用的空间更大?
因除40字节外,还会额外分配空间用于存储信息,选2大的多
——————/
int main()
{
int v[2][10] ={ { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{11, 12, 13, 14, 15, 16, 17, 18, 19, 20}};
//元素v[0]是个含10个元素的数组,*v第0个数组,**v第0个数组的第0个元素
int (*a)[8] = (int(*)[8])v; //一维数组指针
cout<<**a<<endl;
cout<<**(a + 1)<<endl;
cout<<*(*a + 1)<<endl;
cout<<*(a[0] + 1)<<endl;
cout<<*a[1]<<endl;
}
结果:1 9 2 2 9
——————/
class Base
{
public:
int a;
Base()
{
a = 1;
}
void println()
{
cout<<a<<endl;;
}
};
class Child : public Base
{
public:
int a;
Child()
{
a = 2;
}
};
int main()
{
Child c;
c.println(); //父类中的函数看不到子类定义的成员变量
cout<<c.a<<endl; //将父类a覆盖
}
结果:1 2