Unformatted text preview:

Midterm Fall 2022 PIC10B October 28th 2022 1 Instructions you ll do just fine You have 50 minutes to complete this exam Read each question carefully and relax There are 6 questions worth a total of 50 points If you don t know an answer put down your best estimate or guess in order to get partial credit Write your solutions clearly and if appropriate also provide neat readable workings Remember if you don t know the answer write down your thinking to get partial credit Rough workings should be done on scratch paper You may not use any resources such as the internet books help from other people to complete this exam You are permitted to use notes from a single A4 page of paper Ensure that you write your name and UID in the space provided below NAME UID Question Points Bonus Points Score 1 8 0 2 9 0 4 10 0 5 6 0 6 12 0 Total 50 0 3 5 0 1 1 In our study of operator overloading we encountered the keywords mutable explicit and friend as well as the specifier delete explain their usage in this context Highlight where a keyword specifier is placed and on what types of operator overloads e g member operator global operator or constructor function it is relevant for a 2 points mutable b 2 points explicit c 2 points friend d 2 points delete 2 Finish the declaration and definition of the following class by filling out the sections marked a 3 points Complete the definition of the pre increment operator when applied to an object of type A This operator should increase the value of the member variable x by 1 and then return a reference to the A object in question b 3 points Complete the definition of the operator the parameters of this oper ator should be two type A references and it should return a type A object whose x is the sum of the x member variables of the two arguments and whose y is the concatenation of the y member variables of the two arguments c 2 points Complete the operator overload so that if a1 is of type A then std cout a1 prints the x member then a space followed by the y member variable d 1 point Ensure that addition between a type A object and an integer cannot A int x std string y x x y y A int x x x y operator Complete pre increment operator Declare and define operator A operator A left return friend std ostream operator std ostream os A right return os occur 1 class A 2 public 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 private 19 20 21 int x std string y 3 In our study of inheritance we encountered three access specifiers private protected and public which control access to the members of a class Describe how each of these specifiers controls where a member is accessible a 1 point Private b 1 point Protected c 1 point Public We also encountered three types of inheritance with the same names which vary in terms of how they map access membership in the base to the derived class For example public inheritance makes public members of the base also be public in the derived class and protected members in the base protected members in the derived class Describe how protected and private inheritance work d 1 point Private inheritance e 1 point Protected inheritance 4 Consider the following base class B and the class C which inherits from B 1 class B 2 public 3 explicit B double x x x i 0 j 0 virtual void operator B r i x r x virtual std string info float d const j return Type B object 11 12 double x int i 13 14 protected 15 16 17 18 class C public B 19 public 20 mutable int j using B B virtual std string inf const j return Type C object 24 25 4 5 6 7 8 9 10 21 22 23 Predict the output from the following independently run code snippets If the code snippet produces an error then write Error followed by the reason why a 2 points Code snippet 1 B b1 3 2 std cout b1 j n Output b 2 points Code snippet 1 B b1 2 2 b1 info 1 3 std cout b1 info 3 n c 2 points Code snippet 1 B b1 2 2 B b2 3 Output Question 4 continues on the next page Output 3 b2 b1 b2 4 std cout b2 x n d 2 points Code snippet 1 C c1 1 2 std cout c1 info 1 n Output e 2 points Code snippet 1 C c1 1 2 B b1 3 3 c1 b1 4 std cout c1 inf c1 x c1 i n Output 5 In our study of inheritance we also encountered the keywords virtual override as well as pure virtual functions declared using 0 a 2 points How and why is virtual used In addition which types of function is it used for member function of a class or global function where is it placed in the function declaration and what are the downsides of using it b 2 points What is the purpose of using override In particular which types of function is it used for member function of a class or a global function where is it placed in the function declaration and describe how it makes code more robust c 2 points What is the purpose of declaring a member function as abstract pure virtual using 0 Does an abstract function have to be virtual If a class has an abstract function can it be instantiated 6 In this question we will look at a Pet class from which we will derive a Dog class and in turn a Corgi class a 2 points Complete the following definition of a Pet class so that all derived classes have to override the noise member function 1 struct Pet 2 3 4 5 Pet std string species species species virtual void noise const std string species Is it possible to create instances of a Pet Answer True you can create Pets or False you can t b 3 points Complete the following definition of a Dog class in particular complete the constructor and add a specifier to the noise member function in order that it can t change any member variables 1 struct Dog public Pet 2 Dog bool fluffy virtual void noise override std cout Woof n 6 7 c 2 points What is the output from running the following code snippet If it gives an error write Error and explain why bool fluffy Dog d1 true Pet ref d1 ref noise d 3 points Complete the following definition for a Corgi class a Corgi is a small dog originally used to herd cattle in Wales The Corgi should use the constructor of the class it inherits from choose the most appropriate base class out of Pet …


View Full Document

UCLA PIC 10B - Midterm

Download Midterm
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Midterm and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Midterm and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?