云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > 东南大学试卷2011-2012_3c++(B)

东南大学试卷2011-2012_3c++(B)

  • 62 次阅读
  • 3 次下载
  • 2026/4/28 22:25:31

B.setValue(21); cout << B.getValue() << endl; return 0; }

答案:

Teacher id is 35 <----- Teacher 35 Teacher id is 0 <----- Teacher 0 Get Teacher id 35 Set Teacher id 21 Get Teacher id 21

II.Fill in the following blanks to complete programs. (20 score)

1. Open a file which name is gotten from standard input, and get the line numbers of the file. Complete the code. #include #include void main() {

int i =0; ifstream inf;

char fn[20], buf[255]; cin >> fn; inf.open(fn); if ( 1 ) { return; } While ( 2 ) {

inf.getline(buf,sizeof(buf)); i++ }

cout << “ The line number is : ” << i; 3 ; }

答案: 1. !inf 2. !inf.eof 3. inf.close()

共 10 页 第 5 页

2. There is program that use a conditional expression that returns either a double or an int. Provide an int catch handler and a double catch handler. Show that only the double catch handler executes, regardless of whether the int or the double is returned.

#include using std::cerr;

int main() {

try // throw int {

int a = 7;

double b = 9.9;

// throw int to show that only the double catch handler executes 4 ;

} // end try

catch ( 5 ) {

cerr << \} // end catch catch ( 6 ) {

cerr << \} // end catch return 0; }

答案: 4. throw a < b ? a : b 5. int x 6. double y

3. String concatenation requires two operands—the two strings that are to be concatenated. In the textbook, an overloaded concatenation operator that concatenates the second String object to the right of the first String object, thus modifying the first String object. In some applications, it is desirable to produce a concatenated String object without modifying the String arguments. Implement operator+ to allow operations such as string1 = string2 + string3, complete the code.

共 10 页 第 6 页

#ifndef STRING_H #define STRING_H #include #include class String {

riend ostream &operator<<( ostream &output, const String &s ); public:

String( const char * const = \String( const String & ); // copy constructor ~String(); // destructor

const String &operator=( const String & ); String operator+( const String & ); private:

char *sPtr; // pointer to start of string int length; // string length }; // end class String #endif

String String::operator+( 7 ) {

String temp;

delete temp.sPtr; // delete temp object?s underlying memory temp.length = length + right.length;

temp.sPtr = new char[ temp.length + 1 ]; // create space assert( 8 ); // terminate if memory not allocated strcpy( temp.sPtr, sPtr ); // left part of new String strcat( 9 ); // right part of new String return temp; // enables concatenated calls } // end function operator+

// overloaded output operator

ostream & operator<<( ostream &output, const String &s ) {

10 return output; // enables concatenation } /

答案:

共 10 页 第 7 页

7. const String &right 8. sPtr != 0 9. temp.sPtr, right.sPtr 10. output << s.sPtr;

III.Write programs pies according to the requests(20 score)

1.Array is often used in our program, but it cannot implement Append and + operation for append one array to another. Write an ARRAY class template code to implement Append function and + operator for this class. (10scores)

答案:

1. Class template definition 3

2. Append function implementation 3 3. + operator / friend function 3 4. Test code 2

2.According to the following requirements, and write your program (10 scores)

There are several quadrilaterals (四边形), such as Square, Rectangles, and parallelogram(平行四边形) ; You should write a program to get the summary of these quadrilaterals ?area. You should define these necessary classes and write a test program.

答案:

1. Rectangle class definition 5 2. polymorphism 2

3. Driver/ Test/Main function 2

东 南 大 学 考 试 卷( A 卷)

课程名称

程序设计基础及语言2

考试学期 10-11-3

卷面

40分

共 10 页 第 8 页

搜索更多关于: 东南大学试卷2011-2012_3c++(B) 的文档
  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

B.setValue(21); cout << B.getValue() << endl; return 0; } 答案: Teacher id is 35 <----- Teacher 35 Teacher id is 0 <----- Teacher 0 Get Teacher id 35 Set Teacher id 21 Get Teacher id 21 II.Fill in the following blanks to complete programs. (20 score) 1. Open a file which name is gotten from standard input, and get the line numbers of the file. Complete the cod

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com