Chapter 3 Exercises

1. Fill in the blanks

(1) The access rights of members after the keywords private, public and protected in the class definition are public , protected and private , respectively . If no keyword is used, all members are defined as private permissions by default. Data members with public access can only be accessed directly by functions that do not belong to the class.

(2) When defining a member function, the operator "::" is a scope operator, and "MyClass::" is used to indicate that the following member function is described in " MyClass ".

(3) Objects are created by allocating memory for objects while the program is running. When creating an object, a class is used as a template , so an object is called an instance of a class .

(4) Assuming that Dc is a class, the system will automatically call the constructor of this class for 11 times when the statement "Dc a[10],b(2)" is executed .

(5) For any class, the number of destructors is at most one .

(6) The delete operator is usually used to realize the task of releasing the dynamic storage space pointed to by the pointer member in the object of this class.

(7) The memory pattern of a C++ program is usually divided into four areas: data area , code area , stack area , and heap area .

(8) The data is defined as a global variable, which destroys the encapsulation of the data ; a better solution is to define the data to be shared as a static member of the class .

(9) Static data members and static member functions can be accessed by functions permitted by any access rights .

(10) Friend functions and friend classes are collectively referred to as friends.

(11) The correct use of friends can improve the efficiency of the program , but destroy the encapsulation of classes and the concealment of data.

(12) If a class A needs to be defined as a friend class of a class B, a statement should be added to the definition of class B: friend A; .

2. Multiple-choice questions (choose at least one, you can choose more than one)

(1) The following does not belong to the class access authority is (B).

A.public                   B.static                 C. protected                   D. private

(2) The statement about the class is incorrect (BC).

A. A class is a user-defined data type B. Only the member functions of the class can access the private data members of the class

C. In a class, if no permission description is made, all data members are shared D. In a class, if no permission description is made, all data members are private

(3) Outside the class definition, members that can be accessed by any function are (C).

A. All class members B. private or protected C. public class members D. public or private class members

(4) Statement (C) about classes and objects is false.

A. An object is an instance of a class B. Any object can only belong to a specific class

C. A class can only have one object D. The relationship between classes and objects is similar to the relationship between data types and variables

(5) Suppose MClass is a class, dd is an object of it, pp is a pointer to dd, and cc is a reference to dd, then the access to members, the object dd can be done through (B), and the pointer pp can be accessed through (D ), and citing cc can be done via (B).

A. ::                           B.  .                     C.  &                          D.  ->

(6) Which of the statements about member functions is incorrect is (C).

A. Member functions can have no return value B. Member functions can be overloaded C. Member functions must be inline functions D. Member functions can set default values ​​for parameters

(7) The following incorrect description of the constructor is (B).

A. The system can provide a default constructor B. The constructor can have parameters, so it can also have a return value

C. The constructor can be overloaded D. The constructor can set default parameters

(8) Assuming that A is a class, then executing the statement "A a, b(3), *p;" calls the (B) constructor.

A.1                           B.2                         C. 3                         D. 4

(9) The following correct description of the destructor is (AC).

A. The system can provide a default destructor B. The destructor must be defined by the user C. The destructor has no parameters D. The destructor can set default parameters

(10) The destructor of the class is called when (D).

A. Class creation B. Create object C. Reference object D. Release object

(11) When an object of a class is created, the system automatically calls (B); when the object is revoked, the system automatically calls (C).

A. Member function B. Constructor C. Destructor D. Copy constructor

(12) Usually the parameter of the copy constructor is (C).

A. An object name B. A member name of an object C. A reference name of an object D. A pointer name of an object

(13) The statement about the this pointer is correct (B).

A. The this pointer must show the description B. When an object is created, the this pointer points to the object    

C. Member functions have this pointer D. Static member functions have this pointer

(14) In the following description of sub-objects, (B) is wrong.

A. A sub-object is a data member of a class that is an object of another class B. A sub-object can be an object of its own class

C. The initialization of sub-objects should be included in the constructor of the class D. A class can contain multiple sub-objects as its members

(15) In the following description of the new operator, (B) is false.

A. It can dynamically create objects and object arrays B. It must specify initial values ​​when creating object arrays with it

 C. The constructor is called when an object is created with it D. The array of objects created with it can be released at one time using the operator delete

(16) In the following description of the delete operator, (D) is false.

A. It can be used to release objects and arrays of objects created with the new operator B. When it is used to release an object, it acts on a pointer returned by new

C. When using it to release an object array, the subscript operator [ ] must be added before the name of the pointer it acts on. D. It can be used to release multiple objects created with the new operator at one time

(17) Regarding static data members, the following statement is incorrect (C).

A. Use static data members, in fact, to eliminate global variables B. You can use "object name.static member" or "class name::static member" to access static data members

C. Static data members can only be referenced in static member functions D. Static data members of all objects occupy the same memory unit

(18) An incorrect description of a static data member is (CD).

A. Static members do not belong to objects, but are shared members of the class B. Static data members should be defined and initialized in the class

C. When calling a static member function, it needs to be activated through the class or object, so the static member function has this pointer D. Only static member functions can operate static data members

(19) Among the options below, the one that cannot be directly accessed by static member functions is (D).

A. Static data members B. Static member functions C. Functions and data other than classes D. Non-static data members

(20) In the definition of a class, the reason for introducing a friend is (A).

A. Improve efficiency B. Deepen the use of class encapsulation C. Improve program readability D. Improve data concealment

(21) The declarative method with a metaclass is (A).

A.friend class<class name> B. youyuan class<class name> C. class friend<class name> D. friends class<class name>

(22) The following incorrect description of the friend is (D).

A. The keyword friend is used to declare friends B. A member function in one class can be a friend of another class

C. The members of the object accessed by the friend function are not affected by the access characteristics D. The friend function accesses the members of the object through the this pointer

(23) Among the options below, (C) is not a member function of a class.

A. Constructor B. Destructor C. Friend function D. Copy constructor

3. Short answer questions

(1) What is the relationship between class and object?

    Answer: A class is actually a type defined by a class called a class type, a programmer can use this new type to declare new variables in a program, a variable with a class type is called an object. When creating an object, a class is used as a template and the object is called an instance of the class.

(2) What is the general form of a class definition? What kinds of access rights does its members have?

    Answer: The general form of the definition class is:

            class class name

           {

               public:

                        <public data and functions>

               protected:

                         <Protect data and functions>

                private:

                         <private data and functions>

};

There are three types of access rights: public, protected, and private.

(3) Does the instantiation of a class mean creating an object of the class or defining the class?

      Answer: Create an object of a class

(4) What is this pointer? What is its main function?

      Answer: The this pointer is an implicit object pointer provided by C++ for member functions, and it cannot be explicitly declared. The this pointer is a local quantity, local to an object. When different objects have the same member function, the compiler determines which data member of the object should be referenced according to the this pointer.

(5) What is a copy constructor? When is the copy constructor called?

      Answer: The copy constructor is a special constructor, its role is to use an existing object to initialize another object. In order to ensure that the referenced object is not modified, the reference parameter is usually declared as a const parameter.

        In the following three cases, the copy constructor will be called by itself:

a) When an object of a class is used to initialize another object of that class.

b) When the formal parameter of the function is an object of the class, the formal parameter and the actual parameter are combined.

c) When the return value of the function is an object of the class and the function execution returns to the caller.

Fourth, program analysis questions (write the output of the program, and analyze the results)

(1)

#include<iostream>
using namespace std;
class Test
{
   private:
      int num;
   public:
      Test();
      Test(int n);
};
Test::Test()
{
    cout<<"Init defe"<<endl;
    num=0;
}
Test::Test(int n)
{
    cout<<"Init"<<""<<n<<endl;
    num=n
}
intmain()
{
    Test x[2];
    Y test (15);
    return 0;
}

The output of the program is as follows:


The analysis results are as follows:

Program analysis: The program declares 2 objects Test x and Test y, and there are 2 constructors in the class. When the object Test x is created, a constructor with one parameter is called. Since the object Test x is an array of objects, the constructor must be called when each array element is created, so the constructor with one parameter is called twice, and the first, 2 lines of results. When the object Test y is created, the constructor with 1 parameter is called, and the result of the third line is output.

(2)

#include<iostream>
using namespace std;
class Xx
{
   private:
      int num;
   public:
      Xx(int x){num=x;};
      ~Xx(){cout<<"dst"<<num<<endl;}
};
intmain()
{
    Xx w(5);
     cout<<"Exit main"<<endl;
    return 0;
}

The output of the program is as follows:


Result analysis:

Program analysis: The program declaration declares only one object Xx w. The constructor is called when the object is created, and the first line of results is output. When the program ends, an object is released, the destructor is called, and the second line of results is printed.

(3) Modify the Whole class in Example 3.10 as follows, leaving the other parts unchanged, and write the output result.

#include<iostream>
using namespace std;
class Part
{
   public:
       Part();
       Part(int x);
       ~Part();
   private:
       int val;
};
Part::Part()
{
    val=0;
    cout<<"Default constructor of Part"<<endl;
}
Part::Part(int x)
{
    val=x;
    cout<<"Constructor of Part"<<","<<val<<endl;
}
Part::~Part()
{
   cout<<"Destructor of Part"<<","<<val<<endl;
}
class Whole
{
    public:
       Whole(int i);
       Whole(){};
       ~Whole();
   private:
       Part p1;
       Part p2;
       Part p3;
};
Whole::Whole(int i):p2(i),p1()
{
    cout<<"Constructor of Whole"<<endl;
}
Whole::~Whole()
{
   cout<<"Destructor of Whole"<<endl;
}
intmain()
{  Whole w(3);
   return 0;
}

The output is as follows:


Result analysis:

Program analysis: In the Whole class of the program, three objects p1, p2 and p3 of the class Part appear as data members of the class, then p1, p2 and p3 are called sub-objects. When the object w of the Whole class is created, the sub-objects p1, p2, and p3 are created, and the specified constructor is executed. Since p2 is first described in the Whole class, p2 executes the parameterized constructor it uses, and then p1 executes the constructor it uses, that is, the default constructor of the class Part. After all sub-objects are constructed, the object w The constructor is executed, resulting in the first four lines of results. The last four lines are the output results of executing the corresponding destructor. It can be seen that the calling order of the destructor is exactly opposite to that of the constructor.

(4)

#include<iostream>
using namespace std;
class Book
{
   public:
       Book(int w);
       static int sumnum;
   private:
       int num;
};
Book::Book(int w)
{
    num=w;
    ;
}
int Book::sumnum=120;
intmain()
{
    Book b1(20);
    Book b2(70);
    cout<<Book::sumnum<<endl;
   return 0;
}

Output result:


Result analysis:

Program analysis: In the program, use the cout<<Book::sumnum<<endl format to refer to static data members. When the program is executed, b1 and b2 are created at the same time, and the constructor is called. In the program sumnum-=w, int Book::sumnum=120, it can be seen that 120-20-70=30. So the output is 30.

Five, programming problems

(1) Declare a Circle, with data member radius (radius) and member function area(), calculate the area of ​​the circle, and construct a Circle object for testing.

Write the program as follows:

 
 
#include<iostream>
using namespace std;
#define pi 3.14
class Circle {
private:
double Radius;

public:
Circle () : Radius( 0 ) {  }
explicit Circle ( double r ) : Radius( r ) {  }
Circle ( Circle & rhs )  {  Radius = rhs.Radius;  }
const Circle & operator= ( Circle & rhs )  {  Radius = rhs.Radius;  }
void setRadius( double r )  {  Radius = r;  }
double Area()  {  return pi * Radius * Radius;  }
};

intmain()
{
Circle test;
double r;
cout << "Please enter the radius of the circle:" << endl;
cin >> r;
test.setRadius( r );
cout << endl << "The area of ​​the circle is: " << test.Area() << endl;
return 0;
}


The result of running the program is as follows:


(2) Rewrite the program of program analysis problem (4), and design a static member function to output the value of the static data member in program analysis problem (4).

Write the program as follows:

#include<iostream>
using namespace std;
class Book
{
   public:
       Book(int w);
       static int sumnum;
       static int getsum(){return sumnum;}
   private:
       int num;
};
Book::Book(int w)
{
    num=w;
    sumnum-=w;
}
int Book::sumnum=120;
intmain()
{
    Book b1(20);
    Book b2(70);
    cout<<Book::getsum<<endl;
   return 0;
}

The result of running the program is as follows:


Guess you like

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