上次面试C++发现了很多本身还不会的问题,总结一番。 ios
一,宏函数 面试
好比定义一个求二者最大值的宏函数: 函数
#define MAX(a,b) ((a)>(b)?(a):(b)) spa
注意1,MAX后不能有空格。2,每一个变量最好用括号括起来。3,末尾不要加分号 指针
面试题:写一个求整数a的第n位是1仍是0的宏函数 code
#define fun(a,n) ((a)&(1<<(n)) == (1<<(n)))?1:0 string
二,多态 it
要实现多态(动态绑定),必须知足如下之一
一、使用指针调用
二、使用引用调用
如: io
#include <stdio.h> #include <iostream> #include <string> using namespace std; class base { public: virtual void print() { cout<<"it is in base::print"<<endl; } virtual ~base(){} }; class son:public base { public: virtual void print() { cout<<"it is in son::print"<<endl; } virtual ~son(){} }; class grandson:public son { public: virtual void print() { cout<<"it is in grandson::print"<<endl; } virtual ~grandson(){} }; void fun(base arge) { //基类对print()的调用 arge.print(); } void func(base& arge) { //静态多态 arge.print(); } void func_t(base* arge){ //动态多态 arge->print(); } int main() { base a; son b; grandson c; func_t(&a);//能体现多态 func_t(&b); func_t(&c); base d; son e; grandson f; func(d); //能体现多态 func(e); func(f);
basw g;
son h;
grandson i;
fun(g);//不体现多态,都是调用的base类的print方法
fun(h);//同上
fun(i)//同上 return 0; }