尔雅Objectoriented Technology and C++ Programming Language课后答案(学习通2023课后作业答案)

高校邦问答2024-05-19 08:40:2149504抢沙发
尔雅Objectoriented Technology and C++ Programming Language课后答案(学习通2023课后作业答案)摘要: 1 IntroductionTest 11、What is the output of the following program fragment? int a = 3, b = 4; int&am ...

尔雅Objectoriented Technology and C++ Programming Language课后答案(学习通2023课后作业答案)

1 Introduction

Test 1

1、尔雅What is 课课后the output of the following program fragment? int a = 3, b = 4; int& r = a; r = b; r = 6; std::cout << a << std::endl;
A、7
B、后答6
C、案学4
D、习通3

2、作业What is 答案the return type of ‘new int’?
A、int
B、尔雅int &
C、课课后int *
D、后答void

3、案学char* p = new char(65); What's the memory release statement ?
A、习通delete p;
B、作业delete [] p;
C、答案free(p);
D、尔雅delete <char> p;

4、char* p = new char[65]; What's the memory release statement ?
A、free p;
B、free p[];
C、delete *p;
D、delete [] p;

5、Which of the following statements is true?
A、The number of parameters for multiple overloaded functions must be different.
B、There are two functions with the same parameter list and different return value types. These two functions are overloaded functions.
C、When calling a function with both the second and third parameters having default values, you can only write the third argument while not writing the second argument.
D、The purpose of using inline functions is to improve the running speed of the program.

6、If the function declaration is void fun (char * a, int n, int & m); Which of the following statements is true ?
A、char str[10]; int *p, m; fun( str, m, p );
B、char *p = new char[10]; int n, m; fun( p, m, &n );
C、char *p = new char[10]; int n, m; fun( p, m, n );
D、char *p = new char[10]; int n; fun( p, &n, 6 );

7、The legal comments for C++ is
A、/*This is a C++ program/*
B、// This is a C++ program
C、“This is a C++ program”
D、/*This is a C++ program//

8、The steps of developing C ++ program are
A、edit, debug, compile, connect
B、compile, debug, edit, connect
C、compile, edit, connect, run
D、edit, compile, connect, run

9、Function overloading means
A、Two or more functions have the same function name, but the number or type of parameters are different
B、Two or more functions have the same name and the same number of parameters, but the types of parameters can be different
C、Two or more functions have different names, but the number or type of parameters are the same
D、Two or more functions have the same function name and return type

10、About C++ and C language, which of the following statements is wrong?
A、C is a subset of C++
B、C program can run in C++ environment
C、C++ program can run in C environment
D、C++ is object-oriented and C is process oriented

Assignment 1

1、Write a program with input and output stream operators. Input two numbers from the keyboard, and add, subtract, multiply and divide the two numbers respectively. Output the calculation results, such as: 23 + 123 = 146.

2、Use “new” and “delete” operators to dynamically allocate memory space. Input the data of 3X3 integer array (9 integers) from the keyboard, and calculate the sum of all elements. Print the maximum and minimum integers.

3、Write C++ style program to solve the problem of “100 cents”: how many ways to change RMB 1 yuan into RMB 1,2,5 cents?

Assignment 1 (update)

1、(1) Input two numbers from the keyboard. (2) Add, subtract, multiply and divide the two numbers respectively. Output the calculation results, such as: 23 + 123 = 146.

2、Use “new” and “delete” operators to dynamically allocate memory space. Input N integers from the keyboard. (1) Calculate and print the sum of all integers. (2) Print the maximum and the minimum of all integers.

3、Write a program to swap two integers in the function void swap (int & X, int & Y) by using reference as function parameters. Print the results in the main function.

2 Class and Object

Test 2

1、What is the incorrect statement about “class” and “object”?
A、Class is a data type. It encapsulates data and functions.
B、Object is an instance of the class.
C、There is only one object for a class.
D、An object must belong to a class

2、Which of the following statements is incorrect?
A、A class can have many member functions with the same name
B、In a class, we can declare objects of another other class as its data members
C、The definition of a class should end with a semicolon.
D、The storage space allocated by the system for the class can be calculated by "sizeof"

3、A member function in class A is described as void set(A &a);,where A &a means_______
A、The pointer to class A is a
B、Assign the address of a to the variable set
C、a is the object reference of class A, which is used as the parameter of function set()
D、a is an independent object of class A, which is used as the parameter of function set()

4、Function void f() is a public member function in class A. Which of the following statements is wrong?
A、A a; a.f();
B、A a; A*a1 = &a; a1->f();
C、A a; A &a1 = a; a1->f();
D、A a; A &a1 = a; a1.f();

5、In the following Time class, which of the following statements is correct? class Time { int H,M,S; public: void Time(int h,int m,int s) { }; //A } //B
A、There is an error in line A
B、There is an error in line B
C、There are errors in lines A and B
D、There is no error in lines A or B

6、Read the following codes and choose the correct output. #include<iostream> using namespace std; class S { int x; public: S( ){ x=0;} S (int a) { x=++a;} void show(){ cout<<"x="<<x<<endl;} }; int main() { S s1=100; s1.show(); return 0; }
A、100
B、101
C、0
D、syntax error

7、In the following description about "static" , which one is wrong?
A、Static members can be classified into static data and static functions
B、Static data is shared by all objects of the class, so static data cannot be private.
C、Static data is not initiated by constructor
D、Non-static members cannot be directly accessed in static functions of the class

8、Which is the wrong in the description about "friend"?
A、Friend functions can access all members of a class, including private members
B、Friend functions are declared within a class, so they are equivalent to member functions
C、Friend functions destroy encapsulation, so we should use them less
D、All member functions in a friend class are friend functions

9、Which of the following statements about "const" is incorrect?
A、Objects, data members, and member functions can be defined with const
B、The constant object can only call the constant member functions of a class
C、Constant data members can only be initialized by the constructor through the initialization list.
D、Constant member functions can call non-constant member functions of the class

10、About “constructor” and “destructor”, which one is incorrect?
A、Every class has at least one constructor which is responsible for initializing objects.
B、Constructor can be overloaded.
C、Destructor can be overloaded.
D、Neither constructor nor destructor has return type.

Assignment 2

1、A fraction class is defined as follows. Each member function is required to be implemented, and operations such as addition, subtraction, multiplication, and division of two fractions are tested in the main function. class Rational { public: Rational(int nn=1,int mm=1); //Constructor Rational R_add(Rational & A); //Addition Rational R_sub(Rational & A); //Subtraction Rational R_mul(Rational & A); //Multiplication Rational R_div(Rational & A); //Division void print(); //Contract the fraction as a simple fraction private: void simple( ); //Contract int m; //Denominator int n; //Molecule };

2、The store distributes a kind of goods which are purchased and sold in boxes by weight, and the weight and price of each box varies. Therefore, the store needs to record the total weight and value of the goods currently in stock. Write a program to simulate the purchase and sale of goods in the store by defining the “Carlo” class. (The main purpose of this task is to train students to use static data members. Define private variables to store the price and weight of each item, and use static data members to store the total weight and total price of the item; define the constructor. When the definition and initialization of a new object is completed or the object is deleted, the weight and price of the corresponding object are added or subtracted from the total weight and total price.)

3、There is a “Point” class and the main function is as follows: void main() { Point p[3] ; p[2].set(1.0,2.0) p[3].set(3.0,4.0) ; p[2].show( ); } After running the program, the output is as follows: Create point(0,0). Create point(0,0). Create point(0,0). point is (1.0,2.0). Please complete the declaration and implementation of the “Point” class. (Both x and y coordinates are float type)

3 Operator Overloading

Test 3

1、Which of the following descriptions about operator overloading is correct?
A、Operator overloading can change the priority, associativity and the number of operands
B、Overloading can change the operation of operators on basic type data
C、Through operator overloading, new operators can be defined for user-defined type data
D、None of the above analysis is correct

2、In the following description about the return type of the C + + operator function overloading, the wrong statement is_________
A、It can be int
B、It can be void
C、It can be float
D、It can be class

3、For the object obj1 and obj2 of class A, the expression obj1>obj2 is interpreted by the C++ compiler as____
A、If the operator is overloaded as a member function, it is interpreted as operator>(obj1, obj2)
B、If the operator is overloaded as a friend function, it is interpreted as operator>(obj1, obj2)
C、If the operator is overloaded as a member function, it is interpreted as obj2.operator>(obj1)
D、If the operator is overloaded as a friend function, it is interpreted as obj1.oprator>(obj2)

4、In one class, the same operator can be overloaded_________
A、once
B、twice
C、many times
D、no more than once

5、For objects of class Rational, overload the unary operator " - ", and the declaration of the operator function is_______
A、void Rational::operator-();
B、friend Rational operator-( );
C、Rational Rational::operator-()
D、friend void operator-( Rational a );

Assignment 3

1、Define a rational number class which overloads the comparison operator. Write the main function to test it. class rational { private: long denom, den; // denom is the numerator //and den is the denominator public: rational(int num=0, int denom=1); int operator<(rational r) const; int operator<=(rational r) const; int operator= =(rational r) const; int operator!=(rational r) const; };

4 Inheritance

Test 4

1、When a derived class inherits a base class, the default inheritance method is ( ).
A、public
B、protected
C、private
D、None of the above

2、The derived class adopts ( ) method to make the protected data member in the base class become its own private data member.
A、private inheritance
B、protected inheritance
C、public inheritance
D、private, protected or public

3、Which statement has no grammatical errors ( ). #include<iostream> using namespace std; class Base{ private: void fun1( ) const { cout<<”fun1”;} protected: void fun2( ) const { cout<<”fun2”;} public: void fun3( ) const { cout<<”fun3”;} }; class Derived : protected Base{ public: void fun4( ) const { cout<<”fun4”;} }; int main(){ Derived obj; obj.fun1( ); //A obj.fun2( ); //B obj.fun3( ); //C obj.fun4( ); //D }
A、A
B、B
C、C
D、D

4、Under the following inheritance hierarchy, which constructor is incorrectly implemented ( ) class A{ protected: int a; public : A( int ); }; class B : public A { int b; public: B(int x, int y); } ; class C: protected B{ char c; public: C(char c, int x, int y); } ;
A、A::A(int a):a(a){ }
B、B::B(int x, int y): A(x), b(y){ }
C、C(char cc, int x, int y): B(x, y),c(cc){ }
D、C(char cc, int x, int y): A(x), B(x, y),c(cc){ }

5、Under the following inheritance hierarchy, the incorrect description is ( ). class A{ protected: int x; public : void set_X( int i){ x = i;} int get_X( ){ return x; } }; class B : public A { int y; } b; class C: protected B{ …} c;
A、In class B, x can be accessed directly.
B、You can't directly access b.x and b.y through B's object b
C、object c can call get_X() to access x
D、In class C, get_X() can be called to access x

6、The derived class and the base class have member functions with the same name and the same parameter list. This is called ( )
A、Function overloading
B、Function Override. The function of the same name in the base class is useless.
C、Function Override. It is an improvement of the derived class to the member function inherited from the base class
D、Duplicate definition is not allowed.

7、Which of the following statements does not meet the assignment compatibility rule ( ).
A、Objects of derived classes can be assigned to objects of base class
B、The object of the base class can be assigned to the object of the derived class
C、Objects of derived classes can initialize references to base classes
D、The address of the object of the derived class can be assigned to the pointer to the base class

Assignment 4

1、a) Define a base class “MyArray” which can store an array. Write constructors, destructors, and other functions to implement functions such as dynamically allocating memory, releasing memory, and data input and output. Class MyArray { int *alist; int length; public: MyArray( int leng); MyArray(const MyArray&); ~MyArray(); void input(); void display(); }; b) Define an “AverArray” class derived from “MyArray” class, which can calculate the average of an array and output it. c) Define a “RevArray” class derived from “MyArray” class, which can store the array in reverse order and output it. Write a main function to test all of them.

5 Polymorphism

Test 5

1、About dynamic polymorphism in C++, which of the following statement is incorrect ( )
A、Must be under the public inheritance
B、Declare virtual function in the parent class
C、Each subclass should override the virtual function of the parent class as needed
D、The function must be called by the parent class objec

2、Which is incorrect about C++'s polymorphism ()
A、Polymorphism is divided into compile-time polymorphism and runtime polymorphism
B、Polymorphism at compile time can be achieved through function overloading
C、Polymorphism at runtime can be achieved through function overloading
D、The mechanism to achieve runtime polymorphism is called dynamic polymorphism

3、Which is the correct pure virtual function declaration ().
A、virtual void tt()=0;
B、void tt(int)=0;
C、virtual void tt(int);
D、virtual void tt(int){ }

4、Which can be inherited by derived classes in the C++ ()
A、Constructor
B、virtual function
C、destructor
D、friend function

5、Which of the following statements about abstract classes is correct ()
A、Classes containing virtual functions are called abstract classes
B、The functions in the abstract class are all pure virtual functions
C、The abstract class has at least one pure virtual function
D、Abstract classes cannot be inherited, but can be instantiated

Assignment 5

1、There is a “Vehicle” class which is the base class of the “Car” class, the “Truck” class and the “Boat” class. Define these classes and define a virtual function “show()” to display various types of information. Try changing “show()” in “Vehicle” to pure virtual function.

6 Template

Test 6

1、Which of the following statement is correct for template ().
A、template<T>
B、template<class T1,T2>
C、template<class T1,class T2>
D、template<typename T1,T2>

2、About the type parameter “T” in the C++ class template, which of the following statements is correct ().
A、Types can only be used as data members
B、Types can only be used as the return type of member functions
C、Types can only be used as the parameter type of member functions
D、All three of the above are correct

3、There is a function template defined as follows: template <class T> void Max(T a, T b, T &c) { c=a+b;} Which of the following options is correct ().
A、float x, y, z; Max(x,y,z);
B、int x; float y,z; Max(x,y,z);
C、int x,y; float z; Max(x,y,z);
D、float x; int y, z; Max(x,y,z);

4、About template, which of the following statement is wrong ()
A、template< c1ass T> void fun(T& t) { T t;......}
B、template <class T1, class T2> void fun(T1 t1, T2 t2 ) { ……}
C、template<typename T1, typename T2> T2 fun( T1 t1 ) { ……}
D、template< typename T1, T2> void fun( T1 t1,T2 t2) { ……}

5、Suppose there is class template declaration: template <class T> class Array{ T * st; int size; public: Array(int s); ...... }; The form of the constructor defined in the class template is ().
A、template<class T>  Array::Array(int s){ ......}
B、template<class T>  Array::Array<T>(int s){ ......}
C、template<class T>  Array<T>::Array (int s){ ......}
D、template<class T> Array<T>::Array<T>(int s){ ......}

Assignment 6

1、Define the function template “min”, which can return the smallest element in the array. Define the template, and then use different types of arrays to test the template.

7 Exception Handling

Test 7

1、About the exception in C++, which of the following statement is wrong ( ).
A、Compiler errors are exceptions and can be thrown
B、Runtime errors are exceptions
C、Exception handling mechanism needs the support of runtime environment
D、Exception can be build-in type or class object.

2、About “throw”, “try”, and “catch” in C++, which of the following statement is wrong ( ).
A、“try block” must be followed by one or more “catch blocks”
B、“throw statement” must be placed in the “try block”
C、{ } can’t be omitted even if there is only one line in “try block”
D、a program can have a “try block” without “throw statement”

3、The output of the following program is ( ). #include <iostream> using namespace std; class A{ public: ~A(){ cout<<'A'<<'\t';} }; char test(){ A a; throw 'T'; return 'N'; } int main(){ try { cout<<test()<<'\t'; } catch(char c) { cout<<c<<'\t'; } return 0; }
A、N T
B、T
C、A N T
D、A T

Final Test

1

1、About “class” and “object”, which one is incorrect?
A、Classes help achieve data abstraction and encapsulation.
B、Class contains member functions and member data. Access specifiers in C++ can only be public or private.
C、An object is known as instance of a given class.
D、Once a class is declared, the class name becomes a type name and can be used to declare variables.

2、About “new” and “delete”, which one is incorrect?
A、“new” can be used to dynamically create objects and object arrays.
B、When an object is created by “new”, the constructor of the class is called.
C、If an object is created by “new”, “delete” should be used to destruct it.
D、“new” and “delete” are often used in pairs, and they must be used in the same function.

3、If declare a pre-increment operator in class of Date, which one is correct?
A、Date operator ++ ( ) ;
B、Date& operator ++ ( ) ;
C、void operator ++ ( int ) ;
D、Date& operator ++ ( int ) ;

4、About “friend”, which one is correct?
A、If class A is a friend of class B, all member functions in class B are friend functions of class A.
B、Friend functions access object members by “this” pointer.
C、A friend of a class can directly access the public and protected members of the class, but cannot directly access a private member
D、Friend function of a class can access the private members of the class, but it destroys the encapsulation of the class.

5、About “static”, which one is incorrect?
A、Static data members must be initialized outside the class.
B、Non-static function members can access static data members and function members.
C、Static function members do not have this pointer, and they can access the non-static data members and function members.
D、A static data member is unique per class and exists independently of any object of the class.

6、About “const”, which one is incorrect?
A、Const object cannot call the non-const member function of the object.
B、Const function cannot call the non-const function.
C、Const function can access the const data members, but cannot access the non-const data members.
D、Objects, data and functions can be “const” in C++.

7、Considering the class A, class B and class C, which one is incorrect? class A{ protected: int x; public : void set_X( int i){ x = i} ; int get_X( ){ return x; } ; }; class B : protected A { int y; }; class C: private B{ …}; void main( ){ B objB; … }
A、Data x in class A can be accessed in class C.
B、objB in function main() can invoke function set_X( int i) inherited from class A
C、Function get_X() can be called in class C to access data x in class A.
D、Data y in class B cannot be accessed in class C.

8、Considering the member functions in the following two classes, which implements function overriding? class Base { public: virtual void f1( ) ; virtual int f2( ) ; char f3( char* );}; class Derv: public Base { public: int f1( ) ; int f2( int ) ; virtual char f3( char* );};
A、Base::f1( ) and Derv::f1( )
B、Base::f2( ) and Derv::f2( int )
C、Base::f3(char * ) and Derv::f3( char* )
D、None of the above answers are correct.

2

1、Write the output of the following program. #include <iostream> using namespace std; class A{ public: virtual ~A(){ cout <<"A is destructed;"<<endl;} virtual void function() { cout <<"A:function"<<endl;} }; class B : public A{ public : virtual ~B(){ cout <<"B is destructed;"<<endl;} virtual void function() { cout << "B:function"<<endl;} }; void test(A & a){ a.function(); A *temp = new A; delete temp; } int main(){ B b; test(b); }

2、Write the output of the following program. #include <iostream> using namespace std; template <typename T> T Max(T x,T y) { cout<<"Max_1"<<" "; //cout in the screen return x > y ? x : y ; } template <typename T> T Max(T x,T y,T z){ T temp = Max(x,y) ; cout<<"Max_2"<<" "; //cout in the screen return Max(temp,z) ; } double Max(double x,double y) { cout<<"Max_3"<<" "; //cout in the screen return x > y ? x : y ; } int main ( ) { cout<<Max(1.0, 2 )<<endl ; cout<<Max(2.0, 5.0)<<endl ; cout<<Max('a', 'z', 'b')<<endl ; }

3、Given a class Room: #include < iostream > using namespace std; class Room { protected: double length; // length of Room double width; // width of Room public: Room(double l, double w) :length(l),width(w){ } //constructor}; Please design and define class ClassRoom which is a derived class of Room. ClassRoom has a seat property which represents the number of seats in the classroom. ClassRoom also has function calAverArea which return average area per seat. In main function, an instance of ClassRoom is created and show function is called. void main(){ ClassRoom cr(30, 10, 12); // 30 seats, length is 10, width is 12 cr.show(); //show on the screen length:10, width:12, seat:30, averarea:4 } Please design and define class ClassRoom:

学习Object-oriented Technology and C++ Programming Language

Object-oriented technology and C++ programming language are two important tools for computer programming. As the world becomes more digital and interconnected, there is an increasing demand for people who can use these tools to develop software that can solve complex problems and help businesses and organizations function more efficiently.

What is Object-oriented Technology?

Object-oriented technology is a programming paradigm that focuses on creating objects that can be manipulated and combined to create larger and more complex systems. These objects can be thought of as individual units of code that have their own properties and behaviors, and can interact with one another to perform specific tasks.

One of the key benefits of object-oriented technology is that it allows developers to create reusable code. By breaking a system down into smaller, modular objects, developers can create code that can be easily re-purposed and adapted to different situations. This makes it possible to design and build complex systems more quickly, and with fewer errors.

What is C++ Programming Language?

C++ is a high-level programming language that is commonly used for developing operating systems, game engines, and other complex software systems. It is an extension of the C programming language, which was developed in the 1970s as a low-level system programming language.

One of the key features of C++ is its support for object-oriented programming. This allows developers to create complex software systems using modular objects, which can be easily re-used and adapted to different situations.

Another key feature of C++ is its support for low-level system programming. This allows developers to write code that can directly access and manipulate hardware resources, which is essential for developing operating systems, device drivers, and other low-level software systems.

Why Learn Object-oriented Technology and C++ Programming Language?

There are several reasons why learning object-oriented technology and C++ programming language is important:

  • Career Advancement: As mentioned earlier, there is a growing demand for people who can develop software systems using object-oriented technology and C++. By mastering these skills, you can position yourself for a rewarding career in software development.
  • Flexibility: Object-oriented technology and C++ programming language are used in a wide range of industries, from healthcare to finance to entertainment. This means that by learning these tools, you can position yourself for a wide range of career opportunities.
  • Ease of Use: While object-oriented technology and C++ programming language may seem intimidating at first, they are actually designed to make programming easier and more efficient. By breaking down complex systems into smaller, modular objects, developers can create code that is easier to write, test, and maintain.

How to Learn Object-oriented Technology and C++ Programming Language?

There are several ways to learn object-oriented technology and C++ programming language:

  • Online Courses: There are a variety of online courses that teach object-oriented technology and C++. These courses typically include video lectures, interactive exercises, and quizzes to help you solidify your knowledge.
  • Books: There are many books available that teach object-oriented technology and C++. These books typically include comprehensive explanations of the language and its features, as well as examples and exercises to help you practice your skills.
  • Practice: The best way to learn object-oriented technology and C++ programming language is to practice writing code. Start by creating simple programs, and gradually work your way up to more complex systems.

Conclusion

Object-oriented technology and C++ programming language are two powerful tools for developing software systems. By mastering these skills, you can position yourself for a rewarding career in software development, and help businesses and organizations function more efficiently.

文章版权及转载声明

本文地址:http://www.zzxhsh.org/63f799149.html发布于 2024-05-19 08:40:21
文章转载或复制请以超链接形式并注明出处五煦查题

评论列表 (暂无评论,44人围观)参与讨论