Chapter 4 Exercises

Chapter 4 After-School Exercises

1. Fill-in-the-blank questions
(1) If class A inherits class B, then class A is called   the base  class, and class B is called the   derived  class.
(2) There are two types of inheritance in C++: single inheritance and multiple inheritance   .
(3) Inheritance mode by default is private inheritance mode  .
(4) When a class is publicly derived from the base class, the public members of the base class become the public  , and the protected members of the base class become the protected .
(5) C++ provides a  multiple inheritance   mechanism, allowing a derived class to inherit multiple base classes, even if these base classes are unrelated to each other.
(6) Name N in class X Dominating the name N of the same name in class Y means that class X takes class Y as one of its base classes, which is called the   domination rule   .
(7) The purpose of introducing virtual base class is to   resolve ambiguity  .
(8) In an inheritance structure, the methods of resolving ambiguity include the   use of scope operators   and   the introduction of virtual base classes   .
2. Multiple-choice questions (choose at least one, you can choose more than one)
(1) The C++ language establishes the class family through (B).
  A. Nesting of classes B. Inheritance of classes C. Virtual functions D. Abstract classes
(2) Inheritance is (CD) method.
  A. Turn a special class into a general class
  B. Pass the general parameters to the object of the special class
  C. Turn the general class into a special class
  D. Add new features to the existing class, but not heavy The advantage of writing them
(3) inheritance is ( ABC ).
  A. Expand the scope of use of the class, making it easier to use the class library
  B. Avoid rewriting program code and provide a useful conceptual framework
  C. Transform the class into an organized hierarchy
  D. Make the class further through natural selection of inheritance and rewriting Expand
(4) The following statement is incorrect (C).
  A. Protected members of base class are still protected in protected derived classes
  B. Protected members of base class are still protected in public derived classes
  C. Protected members of base class are still protected in private derived classes
  D. Access to protected members of the base class must be unambiguous
(5) Objects of the derived class are accessible to members of its base class (A).
  A. Publicly inherited public members
  B. Publicly inherited private members
  C. Publicly inherited protected members
  D. Privately inherited public members
(6) (C) are private data members that can access class objects.
   A. The object of this class B. The member function derived from the friend class of this class C. The friend function in the class D. The member function of the public derived class
(7) Multiple inheritance is (B).
   A. Overlay of multiple single inheritance 
   B. Derived classes have multiple direct base classes
   C. Multiple derived classes have unique base class
   D. Each derived class has at most one direct base class, but it can have multiple indirect base classes Class
(8)'s description of the ambiguity of multiple inheritance, (D) is wrong.
   A. When there are members of the same name in multiple base classes of a derived class, the derived class may have ambiguity in accessing this member.
   B. Due to the ambiguity, a class cannot directly inherit from the same class more than once
   C. Use The scope operator restricts members to resolve the ambiguity
   D. When a function of the same name appears in a derived class and its base class, the derived class may have ambiguity in accessing this member function
(9) The scope operator is usually used Come (AD).  
   A. Specify a specific class 
   B. Indicate which base class is derived from
   C. Limit the visual scope of static variables in some member functions 
   D. Resolve ambiguity
(10) Multiple inheritance derived class destructor release object, ( A ) is called first.
   A. The destructor of the derived class itself
   B. The destructor of the base class
   C. The destructor of the root base class
   D. The destructor of the sub-object class in the derived class
  3. True or false
(1) Adding a derived class of a base class requires fundamental changes to the base class. (wrong)
(2) If no constructor is specified for the derived class, the object of the derived class will call the constructor of the base class. (Yes)
(3) For a class, the possible access rights are: private.public.protected and inaccessible. (wrong)
(4) No matter which way of derivation, private members in the base class are not accessible in the derived class. (Yes)
(5) During the derivation process, the derived class inherits all base class members including constructors and destructors. (wrong)
(6) In single inheritance, the access of the derived class object to the base class member function may also be ambiguous. (Wrong)
  4. Answer Questions  
(1) In object-oriented technology, how to express the relationship between classes?
Answer: In object-oriented technology, a class is a collection of data and operations, and there are three main relationships between them: Has-A, Uses-A, and Is-A.
    Has-A represents the inclusion relationship of a class, which is used to describe that a class is composed of multiple "component classes". In object-oriented technology, the realization of Has-A relationships is represented by class members, that is, sub-objects, which you have learned in Chapter 3.
    Uses-A means that one class partially uses another class. In object-oriented technology, this relationship is realized through the mutual connection of member functions between classes or the passing of object parameters. In addition, this relationship can also be achieved by defining friends.
    Is-A represents a classification method that describes the abstraction and hierarchical relationship of classes.
(2) Briefly describe the assignment compatibility rules.
Answer: The so-called assignment compatibility rule means that in the case of public inheritance, an object of a derived class can be used as an object of the base class. Specifically, it is the following three situations:
① The object of the derived class can be assigned to the object of the base class.
②The object of the derived class can initialize the reference of the base class.
③ The address of the object of the derived class can be assigned to a pointer to the base class.
(3) Briefly describe the access rights of base class members in three inheritance modes.
Answer: When the inheritance mode of the class is public inheritance, in the derived class, the public members and protected members of the base class are inherited as the public members and protected members of the derived class respectively, so that the member functions of the derived class can directly access them , while the member functions of the derived class cannot directly access the private members of the base class. Outside the class, objects of the derived class can access the inherited public members of the base class.
When the inheritance mode of the class is private inheritance, in the derived class, the public members and protected members of the base class are regarded as the private members of the derived class, and the member functions of the derived class can directly access them, but the member functions of the derived class cannot directly access the base class. private members of the class. Outside the class, objects of the derived class cannot access all members of the base class.
    When the inheritance mode of a class is protected inheritance, in a derived class, the public members and protected members of the base class are protected members of the derived class, and the member functions of the derived class can directly access them, but the member functions of the derived class cannot directly access the base class. private members of the class. Outside the class, objects of the derived class cannot have all the members of the base class.
(4) Briefly describe the calling sequence of constructors when creating derived class objects in inheritance mode, and the calling sequence of derived class destructors when deleting derived class objects.
Answer: When creating a derived class object, the constructor call sequence: first, the base class constructor is called, followed by the class constructor of the child object, and finally the derived class constructor is executed.
    The calling sequence of the derived class destructor when deleting the derived class object: first call the derived class destructor; then call the destructor of the sub-object class in the derived class; then call the common base class destructor; finally call the virtual base class The destructor of the class.
(5) Briefly describe the rules for constructors of derived classes.
Answer: Case 1: If the derived class has a constructor but the base class does not, when an object of the derived class is created, the corresponding constructor of the derived class will be automatically called.
    Case 2: If the derived class does not have a constructor and the base class does, the base class must have a default constructor. Only then can the default constructor of the base class be called automatically when an object of the derived class is created.
    Case 3: If the derived class has a constructor and the base class has a default constructor, when creating an object of the derived class, the default constructor of the base class will be called automatically, unless the currently called derived class constructor is initialized in its The list shows that the parameterized constructor of the base class is called.
    Case 4: If both the derived class and the base class have constructors, but the base class has no default constructor, each constructor of the derived class must explicitly call a constructor of the base class in its initialization list. Only then can the constructor of the base class get a chance to execute when an object of the derived class is created.
  Five, program analysis questions (write the output of the program)
#include<iostream>
using namespace std;
class A
{
public:
    A(int i,int j){a=i;b=j;}
    void move(int x,int y){a+=x;b+=y;}
    void disp()
    {
        cout<<"("<<a<<","<<b<<")"<<endl;
    }
private:
    int a,b;
};
class B:public A
{
public:
   B (int i, int j, int k, int l): A (i, j), x (k), y (l) {}
   void disp()
   {
       cout<<x<<","<<y<<endl;
   }
   void fun1(){ move(13,15);}
   void fun2(){A::disp();}
private:
    int x,y;
};
intmain()
{
   A aa(11,12);
   aa.disp ();
   B bb(13,14,15,16);
   bb.fun1();
   bb.A::disp();
   bb.B::disp();
   bb.fun2();
   return 0;
}
The output is as follows:

Analysis:

Through public inheritance, the derived class B obtained from class A has only one base class, so it is single inheritance. Base class A defines two data members and two member functions. In the main function, disp() of class A is called to output the first line of results, fun1() calls the constructor of class A move(int x,int y){a+=x;b+=y;}, and the move function The given initial value is {13,15}, and after a=a+x, b=b+y, a=26, b=29 is obtained. Therefore, the second line of results is obtained, since k=15, l=16, So output x(k), y(l), and get the third line of results. Call the fun2() function. Since the a and b values ​​in class A have become 26 and 29, the result of fun2() is the running result of the fourth line.

Six, programming questions
Define a point class (Point), rectangle class (Rectangle) and cube class (Cube) hierarchy. The rectangle includes two new data members, length and width. The position of the rectangle is inherited from the point class, and the cube class is composed of length, width and height. Various types are required to provide constructors that support initialization and member functions that expose their own members. Write the main function, test this hierarchy, and output information about the cube class.

The procedure is as follows:

#include<iostream>
using namespace std;
class Point
{
 protected:
 int x,y;
    public:
    Point (int myx, int sell) {x = myx; y = sell;}
    void displayxy()
    {
    cout<<"The position of point:";
    cout<<"("<<x<<","<<y<<")"<<endl;
    }
};
class Rectangle:public Point
 {
    private:
     int l,w;
       public:
          Rectangle (int myx, int sell, int myl, int myw): Point (myx, sell)
          {l = myl; w = myw;}
  void displaylw()
   {
     cout<<"The length and width of rectangle:";
          cout<<l<<","<<w<<endl;
            }
        };
class Cube:public Rectangle
{
 private:
  int h;
  public:
   Cube (int myx, int myy, int myl, int myw, int myh): Rectangle (myx, myy, myl, myw)
    {h=myh;}
 void displayh()
 {
     cout<<"The height of cube:"<<h<<endl;
     }
};
intmain()
{
 Cube v (20,40,3,5,6);
   cout<<"The data of cube:"<<endl;
    v.displayxy();
    v.displaylw();
    v.displayh();
    return 0;
}

The results are as follows:


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325836637&siteId=291194637