My Implementation to C++ Primer (4th) Exercises (Chapter 3) (Partial)

Exercise 3.1: Rewrite the program from Section 2.3 (p.43) that calculated the result of raising a given number to a given power to use appropriate using declarations rather than accessing library names through a std::prefix.

#include  <  iostream  >  
//  add using declarations to remove std:: prefix 
using  std::cin;
using  std::cout;
using  std::endl;
using  std::cerr;

int  main()
{
    
int   base  , exponent;
     
if  (cin  >>   base  )
      
{
         
if  (cin  >>  exponent)
        
{
            
int  result  =   base  ;
               
for  (  int  cnt  =   0  ; cnt  !=  exponent;  ++ cnt)
                result 
*=   base  ;
                cout 
<<   base   <<   "   raised to the power of   "  
                    
<<  exponent  <<   "  : \t  "  
                    
<<  result  <<  endl;
         }

        
else  
        
{
            cerr 
<<   "  Exponent can't be empty ?!  "   <<  endl;
            
return   - 1 ;
        }
 
    }
 
    
else  
    
{
        cerr  
<<   "  Base can't be empty ?!  "   <<  endl;
        
return   - 1 ;
    }
 
    
return   0 ;
}
 

Exercise 3.2: What is a default constructor?

A constructor is a special member function that defines how objects of that type can be initialized, it is used "by default" when no initializer is specified.

Exercise 3.3: Name three ways to initialize a string.

string s2(s1);             // Initialize s2 as a copy of s1
string s3("value");   // Initialize s3 as a copy of the string literal
string s4(n, 'c');         // Initialize s4 with n copies of the character 'c'

Exercise 3.4: What are the values of s and s2?

string s;
int main()
{
    
string s2;
    
return 0// need a return value which original code ignore it
}

The values of s and s2 are the same, empty string.

Exercise 3.5: Write a program to read the standard input a line at a time. Modify your program to read a word at a time.

A line at a time:
#include <string>
#include 
<iostream>

using std::string;
using std::cin;
using std::cout;
using std::endl;

int main()
{
    
string line;

    
while(getline(cin,line))
        cout 
<< line << endl;
    
return 0;
}

A word at time:
#include <string>
#include 
<iostream>

using std::string;
using std::cin;
using std::cout;
using std::endl;

int main()
{
    
string word;

    
while(cin >> word)
        cout 
<< word << endl;
    
return 0;
}

Exercise 3.6: Explain how whitespace characters are handled in the string input operator and in the getline function.

For the string input operator by reading a word a time, the whitespace is not consider part of the word, there means that whitespace is used to separate seqences of characters into words. On the other hand, the getline function treats the whitespace as part of a line.

posted on 2010-12-06 19:31 The A 阅读(1721) 评论(0)  编辑 收藏 引用 所属分类: C++


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2024年3月>
252627282912
3456789
10111213141516
17181920212223
24252627282930
31123456

导航

统计

常用链接

留言簿(1)

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜