C++习题-exam
- 格式:docx
- 大小:58.51 KB
- 文档页数:9
1.Please answer the following questions. (20 points, 4 points for each)
(1)Object-Oriented Programming (OOP) is a programming paradigm (范型) using
“objects” to design the program. What is the meaning of “objects” in OOP? What features does OOP have?
An object is a collection of abstracted date type that share common characteristics. It consists of both data values and operations on those values.
The features of OOP have data abstraction, encapsulation, information hiding, inheritance and polymorphism.
(2)What is the purpose of using constructors and destructor in a class? Is constructor of
the class overloaded?
Constructors and destructor are special member functions of a class.
The constructor is invoked implicitly when an object is created. It is used to allocate memory for data members of the class and initialize them.
The destructor is invoked when an object is destroyed. The destructor is used to deallocate memory and do cleanup for the class object.
Yes, the constructor of a class can be overloaded.
(3)What is the difference between private members and protected members of a class?
Private members can be accessible only from within the other members of the same class. Protected members can be accessible from members of the same class, but also from members of its derived class.
Both are also accessible by friends of their classes, and in the case of protected members, by friends of their derived classes. They cannot be accessible outside of their classes.
(4)Why do we need an operator overloaded? Can C++ allow a new operator to be
overloaded?
To an abstract data type, the compiler cannot identify some operators provided by the system. But these operators are supported for built-in type.
To enhance flexibility for manipulating abstract data types, these operators can be overloaded.
No, C++ cannot allow to create a new operator to be overloaded.
(5)When do we use virtual inheritance? When do we use virtual functions?
Virtual inheritance is used when the part of the object that belongs to the top base class becomes a common direct base for the derived class and any other class that derives from it. In this case, the top base class can be defined virtual base class. This can be used to avoid the problem of ambiguous inheritance.
Virtual functions should be used when you want to provide default behaviour in the base class. The derived classes can then override this function and provide their own, more specific behaviour. This is a kind of polymorphism.
2. Please explain the following statements. (28 points)
(1) (2 points)
#include
using namespace std; -- the std namespace is used
(2) (2 points)
void fn(int a =10, int b=20);
Declare function fn with two default parameters within the parameter list and void returning type
(3) (3 points)
int n = i;
int& j = i;
Line 1 states that integer variable n is assigned by the value of variable i. n and i are the same content, but different addresses in the memory.
Line 2 states that variable j refers to another integer variable i. j and i are both the same content and the same address in the memory.
(4) (3 points)
float* p = new float(3.14);
delete p;
Declare a float variable pointer and assign 3.14 to it in Line 1.
Delete memory allocated to p in Line 2.
(5)(5 points)
class AA{
public:
AA(int a); -- constructor with one parameter
AA(const AA& aa); -- copy constructor
~AA(){} -- destructor
private:
int x; -- data member
};
Definition of class AA.
(6)(3 points)
class Ratio{}; -- declare class Ratio
Ratio x, z; -- create two new objects x, z
Ratio y = x; -- create a new object y by existed object x (copy constructor) z = x; -- assign x to z by using overloaded operator =