乐之天空

导航

面试题(11 Autodesk)

转自 : http://hi.baidu.com/jinliangma/blog/item/0d6849a87c843ab2cb130c03.html

一个类有基类、内部有一个其他类的成员对象,构造函数的执行顺序是怎样的。(Autodesk)

class Abase
{
    public:
        Abase() { cout << "Abase construct" << endl; }
};

class B
{
    public:
        B() { cout << "B construct" << endl; }
};

class A
{
    public:
        A() { cout << "A construct" << endl; }
   
    private:
        B b;
};

output:
    Abase construct   
    B construct
    A construct

请描述一个你熟悉的设计模式(Autodesk)
bridge模式,将抽象的部分与它实现的部分分离,使它们可以独立的变化。
bridge模式一般使用在一个抽象部分对应多个不同的实现部分的时候。
例如:
class Shape
{
    public:
        virtual Draw() = 0;
    private:
        Draw *_dp;
    protected:
        Shap(Draw *dp) { _dp = dp; }
        void DrawLine(int x1, int y1, int x2, int y2);
        void DrawCircle(int x, int y, int r);
};

class Rectrangle : public Shape
{
    public:
        Draw();
        Rectrangle(Draw* dp, int x1, int y1, int x2, int y2);
    private:
        int _x1, _y1, _x2, _y2;
};

Rectrangle:Rectrangle(Draw* dp, int x1, int y1, int x2, int y2) : Shape(dp)
{
    _x1 = x1, _y1 = y1, _x2 = x2, _y2 = y2;
}

Rectrangle:Draw()
{
    DrawLine(_x1, _y1, _x1, _y2);
    DrawLine(_x1, _y2, _x2, _y2);
    DrawLine(_x2, _y1, _x1, _y1);
    DrawLine(_x1, _y1, _x2, _y2);
}

class Cricle: public Shape
{
    public:
        Draw();
        Circle(Draw* dp, int x, int y, int r);
    private:
        int _x, _y, _r;
};


Circle:Circle(Draw* dp, int x, int y, int r) : Shape(dp)
{
    _x = x, _y = y, _r = r;
}

Circle:Draw()
{
    DrawCircle(_x, _y, _r);
}


Class Draw
{
    public:
        virtual void DrawLine(int x1, int y1, int x2, int y2) = 0;
        virtual void DrawCricle(int x, int y, int r) = 0;
};

Class V1Draw : public Draw
{
    public:
        void DrawLine(int x1, int y1, int x2, int y2);
        void DrawCricle(int x, int y, int r);   
};

Class V2Draw : public Draw
{
    public:
        void DrawLine(int x1, int y1, int x2, int y2);
        void DrawCricle(int x, int y, int r);   
};

 


在UML 中,聚合(aggregation)和组合(composition)有什么区别 (Autodesk)
聚合的特点是比较松的包含关系,如飞机场与飞机的关系,它是指一类事物的集聚关系。每个类都可以单独成为一个个体。
组合的特点是比较强的耦合关系,属于包容关系,比如A是由B,C组合,那么C是A的一部份。

C#和C++除了语法上的差别以外,有什么不同的地方?
1.c#不支持多重继承
2.c#在安全代码中不支持指针
3.c#具有自动垃圾回收机制

new delete 与malloc free 的区别 ( Autodesk)
1.new是关键字,malloc是函数
2.new调用对象的构造函数, malloc只分配内存空间
3.new的内存分配算法与malloc的不同


#define DOUBLE(x) x+x (Autodesk)
i = 5*DOUBLE(10); i 是多少?正确的声明是什么?

i = 5*x + x= 5*10 + 10 = 60;
#define DOUBLE(x) ((x)+(x))

有哪几种情况只能用intialization list 而不能用assignment?
1.初始化基类的成员数据
2.初始化const,referance的成员对象。
3.类中包含的其他类对象,并且该对象需要参数构造。

C++是不是类型安全的? (Autodesk)
不是。

描述内存分配方式以及它们的区别
1.静态内存区域,用于分配全局变量,静态变量和常量,编译期间
2.堆,用于动态分配内存,在调用malloc时,运行期间
3.栈,用于分配局部变量,编译期间

比较一下C++中static_cast 和 dynamic_cast 的区别
1.static_cast编译时进行类型转换检查, dynamic_cast在运行时进行类型转换
2.static_cast有点类似与c中的强制类型转换,dynamic_cast是沿着继承层次进行安全的类型转换,

Struct 和class 的区别 (Autodesk)
1.作用域不同,struct的成员默认都是public; class中的成员默认为private
2.struct中不存在多态和虚拟继承。
3.class中的对象布局并不一定与声明次序一致。


当一个类A 中没有生命任何成员变量与成员函数,这时sizeof(A)的值是多少,如果不是零,请解释一下编译器为什么没有让它为零。(Autodesk)
1.等于1
2.为了在将来能实现对象的内存分配,编译器给空类提供了一个字节的占位符!


main 函数执行以前,还会执行什么代码? (Autodesk)
答案:全局对象的构造函数会在main 函数之前执行。


类成员函数的重载、覆盖和隐藏区别
答案:
成员函数被重载的特征:
(1)相同的范围(在同一个类中);
(2)函数名字相同;
(3)参数不同;
(4)virtual 关键字可有可无。
覆盖是指派生类函数覆盖基类函数,特征是:
(1)不同的范围(分别位于派生类与基类);
(2)函数名字相同;
(3)参数相同;
(4)基类函数必须有virtual 关键字。
“隐藏”是指派生类的函数屏蔽了与其同名的基类函数,规则如下:
(1)如果派生类的函数与基类的函数同名,但是参数不同。此时,不论有无virtual关键字,基类的函数将被隐藏(注意别与重载混淆)。
(2)如果派生类的函数与基类的函数同名,并且参数也相同,但是基类函数没有virtual 关键字。此时,基类的函数被隐藏(注意别与覆盖混淆)

(2)已知两个链表head1 和head2 各自有序,请把它们合并成一个链表依然有序,这次要求用递归方法进行。 ( Autodesk)
答案:
Node * MergeRecursive(Node *head1 , Node *head2)
{
   if ( head1 == NULL )
       return head2 ;
   if ( head2 == NULL)
       return head1 ;
   Node *head = NULL ;
   if ( head1->data < head2->data )
   {
       head = head1 ;
       head->next = MergeRecursive(head1->next,head2);
   }
   else
   {
       head = head2 ;
       head->next = MergeRecursive(head1,head2->next);
   }
   return head ;
}


15.分析一下这段程序的输出 (Autodesk)
class B
{
public:
    B()
    {
       cout<<"default constructor"<<endl;
    }
    ~B()
    {
       cout<<"destructed"<<endl;
    }
    B(int i):data(i)
    {
       cout<<"constructed by parameter" << data <<endl;
    }
private:
    int data;
};
B Play( B b)
{
    return b ;
}
int main(int argc, char* argv[])
{
    B temp = Play(5);
    return 0;
}

constructed by parameter5

destructed

destructed


posted on 2009-09-28 10:58  乐之天空  阅读(410)  评论(0编辑  收藏  举报