Typical C++ interview questions rised by foreign companies

1. Q: What is Encapsulation?

A: Encapsulation is combining data and functions into a single unit.


2. Q: What is Skeleton? (I was asked by an intewvierer but failed to anser)

A: A class skeleton is an outline of a class that is used in software engineering.


3. Q: What is the difference between new (delete) and malloc (free)?

A: Refer to one of my articles.


4. Q: What is the difference between declaration and definition?

A: The declaration tells the compiler that at some later point we plan to present the definition of this declaration.
E.g.: void stars () //function declaration
The definition contains the actual implementation.
E.g.: void stars () // declarator
{
for(int j=10; j>=0; j--) //function body
cout<<”*”;
cout<<endl;
}


5. Q: What is pure virtual function?

A: Pure virtual function is a member function that base class forces its derived classes to provide. Normally these member functions have no implementation. Pure virtual functions are equated to zero.
E.g.: class Shape { public: virtual void draw() = 0; };


6. Q: What is the difference between an object and a class?

A: Classes and objects are separate but related concepts. Every object belongs to a class and every class contains one or more related objects.

E.g: Horse, human, sheep all belong to Animal. In this case horse, human and sheep are all objects, and animal is a class.


7. Q: What is Polymorphism?

A: Poly means many, and morph means form, so polymorphism means many forms.

E.g.: "+" could add two intergers, and could also be used to add two strings.


8. Q: What is Inheritance?

A: A derived class could inherit all the capabilities of the base class. But it could also add embellishments and refinements of its own.


9. Q: What is the difference between class and structure?

A: The major difference is that all the members inside a class are private and all declarations inside a structure are by default public.


10. Q: What is namespace?

A: We group a set of global classes, objects or function under a name. This name is refered as namespace. To say it somehow, thsy serve to split the global scope into several sub-scopes, known as namespaces.


11. Q: What is pure base class?

A: E.g.:

class a
{
public:
	a(){cout<<"construct a";}
	virtual ~a(){cout<<"destruct a";}
};
class b:public a
{
public:
b(){cout<<"construct b";}
~b(){cout<<"destruct b";}
};
class c:public  a
{
public:
	c(){cout<<"construct c";}
	~c(){cout<<"destruct c";}
};
class d:public b, public c{
public:
	d(){cout<<"construct d";}
	~d(){cout<<"destruct d";}
};
int _tmain(int argc, _TCHAR* argv[])
{
	d di;
	system(
		"pause");
	return 0;
}
It will cout "construct a construct b construct a construct c construct d".

We could find that "construct a" shows up twice. While this is sometimes what you want, in other times, you may only want one "construct a", which indicates that class b and class c share the same base class a. OK, add virtual when create class b and c, and find out difference youself.


12. Q: What's the difference between overloading and overriding?

A: overloading is the ability of functions of the same name but are defined to have different sets of parameters.

overriding means the member function in derived class makes another implementation, compared with the implementation of the function in base class.

E.g.:

class Base
{
public:
virtual void DoSomething() {x = x + 5;}
private:
int x;
};
class Derived : public Base
{
public:
virtual void DoSomething() { y = y + 5; Base::DoSomething(); }
private:
int y;
};


13. Q: What are dangling pointer and wild pointer?

A: If an object has been deallocated or freed, but a pointer still points to this object, then this pointer is a dangling pointer.

E.g.: 

class Sample
{
public:
        int *ptr;
        Sample(int i)
        {
         ptr = new int(i);
        }

        ~Sample()
        {
         delete ptr;
        }
        void PrintVal()
        {
         cout << "The value is " << *ptr;
        }
};

void SomeFunc(Sample x)
{
 cout << "Say i am in someFunc " << endl;
}

int main()
{
 Sample s1 = 10;
 SomeFunc(s1);
 s1.PrintVal(); // dangling pointer
}

wild pointer : int *a;  a is a wild pointer

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值