C++ 基础练习 - Chapter 6 (英文版)

Review Questions

6.1 What is a constructor? Is it mandatory to use constructors in a class?

Answer:

A constructor is a special member function whose task is to initialize the object of its class. It is not mandatory to use constructor in a class.

6.2 How do we invoke(调用) a constructor function?

Answer:

Constructor function is invoked automatically when the objects are created.

6.3 List some of the special properties of the constructor functions.

Answer:

1. They should be declared in the public section.

2. They are invoked automatically when the objects are created.

3. They do not have return type, not even void.

4. They can not inherited.

5. Like other C++ function, they can have default arguments.

6. Constructors cannot be virtual.

6.4 What is a parameterized constructor?

Answer:

The constructors that can take arguments are called parameterized constructors.

6.5 Can we have more than one constructors in a class? If yes, explain the need for such a situation.

Answer:

Yes,when we need to overload the constructors, then we have to do this.

6.6 What do you mean by dynamic initialization of objects? why do we need to this?

Answer:

Initialiazing value of object during run time is called dynamic initialization of objects.
One advantage of dynamic initialization is that we can provide various initialization formats using overloaded constructors.

6.7 How is dynamic initialization of objects achieved?

Answer:

Appropriate function of a object is invoked during run-time and thus dynamic initialization of object is achieved.
Consider following constructor:
sunny(int a, int b, float c);
sunny(int p, int q, int r);
If two int type and one float type value are passed the first one;
if three int type value are passed the second.

6.8 Distinguish between the following two statements:

time T2(T1);

time T2 = T1; T1 and T2 are objects of time class.

Answer:

time T2(T1); -> explicitly (显示) called of copy constructor.
time T2 = T1; -> implicitly (隐式)called of copy constructor.

6.9 Describe the importance of destructors.

Answer:

Destructors are important to release memory space for future use.

6.10 State whether the following statements are TRUE or FALSE.

a. Constructors, like other member functions, can be declared anywhere in the class.

b. Constructors do not return any values.

c. A constructor that accepts no parameter is known as the default constructor.

d. A class should have at least one constructor.

e. Destructors never take any argument.

Answer:

a. FALSE; b. TRUE;c. TRUE;d. TRUE; e. TRUE

Debugging Exercises

6.1 Identify the error in the following program.

#include <iostream>
using namespace std;

class Room
{
    int length;
    int width;
    public:
        Room(int l, int w)
        {
            int sqr = w * l;
            cout << "Area is : " << sqr << endl;
        }
};

int main()
{
   Room objRoom1;
   Room objRoom2(12, 8);
    
    return 0;
}

Answer:

Here is no default constructor, so object could not be written without any argument.
Correction:

int main()
{
	Room objRoom2(12, 8);
	return 0;
}

6.2 Identify the error in the following program.

#include <iostream>
using namespace std;

class Room
{
    int length;
    int width;
    public:
        Room()
        {
            length = 0;
            width = 0;
        }
        
        Room(int value = 8)
        {
            length = width = 8;
        }
        
        void display()
        {
            cout << length << " , " << width;
        }
};

int main()
{
   Room objRoom1;
   objRoom1.display();
    
    return 0;
}

Answer:

error: call of overloaded ‘Room()’ is ambiguous.
Correction: Only keep one constructor function.

Programming Exercises

6.1 A book shop maintains the invertory of books that are being sold at the shop. The list includes details such as author,title,price,publisher and stock position. Whenever a customer wants a book, the sales person inputs the title and author and the system searches the list and displayed. If it is , then the system displays the book details and requests for the number of copies required. If the requested copied are available, the total cost of the requested copies is displayed; otherwise “Required copies not in stock” is displayed.

Design a system using a class called books with suitable member functions and constructors. Use new operator in constructors to allocate memory space required.

Answer:

#include <iostream>
#include <cstring>
#include <iomanip>
using namespace std;

class BookStore
{
    private:
        char **author;
        char **title;
        char **publisher;
        float *price;
        int *stock;
        int size;
    
    public:
        BookStore();    // constructor
        void book_detail(int i);
        void buy(int i);
        int search();
};

BookStore::BookStore()
{
    size = 4;
    author = new char*[80];
    title = new char*[80];
    publisher = new char*[80];
    
    for(int i=0; i<size; i++)
    {
        author[i] = new char[80];
        title[i] = new char[80];
        publisher[i] = new char[80];    
    }
    
    stock = new int[size];
    price = new float[size];
    
    title[0] = "C++ Prime Plus";
    title[1] = "Atomic Habits";
    title[2] = "5 Second Rule";
    title[3] = "Machine Learning";
    
    author[0] = "stanley";
    author[1] = "james";
    author[2] = "mel";
    author[3] = "mel";
    
    stock[0] = 200;
    stock[1] = 150;
    stock[2] = 50;
    stock[3] = 80;
    
    price[0] = 120.5;
    price[1] = 112.75;
    price[2] = 125;
    price[3] = 132;
}

void BookStore::book_detail(int i)
{
    cout << "***************Book Detail****************\n";
    cout << setw(12)<<"Title"<<setw(25)<<"Author Name"<<setw(18)<<"Stock Copy\n";
    cout << setw(15)<<title[i]<<setw(16)<<author[i]<<setw(15)<<stock[i]<<endl;
}

void BookStore::buy(int i)
{
    if(i < 0){
        cout << " This book is not available ! \n";
    }
    else{
        book_detail(i);
        cout << " How many copies of this book is required ? ";
        int copy;
        cin>>copy;
        int remaining_copy;
        if(copy <= stock[i])
        {
            remaining_copy = stock[i] - copy;
            
            float total_price;
            total_price = price[i] * copy;
            cout << " Total Price = " << total_price << "euro.\n";
        }
        else
        {
            cout << " Sorry, your required copies is not available ! \n";
        }
    }
}

int BookStore::search()
{
    char name[80], t[80];
    cout << " Enter author name : ";
    
    gets(name);
    cout << " and title of book in small letter : ";
    gets(t);
    
    int count = -1;
    int a, b;
    for(int i=0; i<size;i++)
    {
        a = strcmp(name, author[i]);
        b = strcmp(t, title[i]);
        
        if(a==0 && b==0)
            count = i;
    }
    
    return count;
}

int main()
{
    BookStore b1;
    int result;
    
    result = b1.search();
    b1.buy(result);

    return 0;
}
  • 17
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值