C++ Interview Questions and Answers - Part 01

来源:互联网 发布:静态构造函数数据成员 编辑:程序博客网 时间:2024/04/29 18:37

Q001. What is encapsulation?
A001.
Containing and hiding information about an object, such as internal data structures and code. Encapsulation isolates the internal complexity of an object's operation from the rest of the application. For example, a client component asking for net revenue from a business object need not know the data's origin.

Q002. What is inheritance?
A002.
Inheritance allows one class to reuse the state and behavior of another class. The derived class inherits the properties and method implementations of the base class and extends it by overriding methods and adding additional properties and methods.

Q003. What is Polymorphism?
A003.
Polymorphism allows a client to treat different objects in the same way even if they were created from different classes and exhibit different behaviors. You can use implementation inheritance to achieve polymorphism in languages such as C++ and Java. Base class object's pointer can invoke methods in derived class objects.You can also achieve polymorphism in C++ by function overloading and operator overloading.

Q004. What is default constructor?
A004.
Constructor with no arguments or all the arguments has default values.

Q005. What is copy constructor?
A005.
Constructor which initializes the it's object member variables(by shallow copying) with another object of the same class. If you don't implement one in your class then compiler implements one for you. For example:

Boo Obj1(10);    // calling Boo constructorBoo Obj2(Obj1);  // calling Boo copy constructorBoo Obj2 = Obj1; // calling Boo copy constructor


Q006. When are copy constructors called?
A006.
Copy constructors are called in following cases: a) when a function returns an object of that class by value; b) when the object of that class is passed by value as an argument to a function; c) when you construct an object based on another object of the same class; d) When compiler generates a temporary object.

Q007. What is assignment operator?
A007.
Default assignment operator handles assigning one object to another of the same class. Member to member copy(shallow copy).

Q008. What are all the implicit member functions of the class? Or what are all the functions which compiler implements for us if we don't define one?
A008.
a) default ctor; b) copy ctor; c) assignment operator; d) default destructor; e) address operator.

Q009. what is the diff between "new" and "operator new"?
A009. "operator new" works like malloc.

Q010. What are C++ storage classes?
A010.
1) auto; 2) register; 3) static; 4) extern;
A010. auto: the default. Variables are automatically created and initialized when they are defined and are destroyed at the end of the block containing their definition. They are not visible outside that block;
A010. register: a type of auto variable. a suggestion to the compiler to use a CPU register for performance;
A010. static: a variable that is known only in the function that contains its definition but is never destroyed and retains its value between calls to that function. It exists from the time the program begins execution;
A010. extern: a static variable whose definition and placement is determined when all object and library modules are combined (linked) to form the executable code file. It can be visible outside the file where it is defined.

...