当前位置:首页 > 东南大学试卷2011-2012_3c++(B)
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
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
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
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 页
共分享92篇相关文档