Answers: Question 1: What is the output in each of the following code? A B #include<iostream.h> class item { int code; public: item() { } item(int n) { code=n; } item(item & a) { code=a.code+1; } void show(void) { cout<<code; }; int main() { item obj1(12); item obj2(obj1); item obj3=obj1; item obj4; obj4=obj1; cout<<"\n code of obj1 : "; obj1.show(); cout<<"\n code of obj2 : "; obj2.show(); cout<<"\n code of obj3 : "; obj3.show(); cout<<"\n code of obj4 : "; obj4.show(); return 0; } #include <iostream> using namespace std; class myClass{ public: int val; myClass () { val = -1; } void f1 (myClass obj) { obj.val = 3; } void f2 (myClass& obj) { obj.val = 5; } }; Output } int main() { myClass obj1, obj2; obj1.f1(obj2); cout << "obj2.val is " << obj2.val << endl; obj1.f2(obj2); cout << "obj2.val is " << obj2.val << endl; return 0; } Output code of obj1 : 12 obj2.val is -1 code of obj2 : 13 obj2.val is 5 code of obj3 : 13 code of obj4 : 12 Page 1 of 23 Question 2: Given the following code, indicate whether or not each of the following lines (a) through (h) is legal or illegal. If it is illegal you have to give a reason. #include <iostream> using namespace std; class myClass { private: double x = 70.0; //a public: int i; int myClass() //b { i = -1; x = 80.0; //c return i; //d } void f1 (int j) const { i = j; //e } }; main() { myClass obj; //f obj.f1(4); cout << "obj.i is " << obj.i << endl; cout << "obj.x is " << obj.x << endl; } line a b c legal illegal X X //g //h reason Cannot initialize at declaration Constructor has no return type X d X Constructor cannot return a value e X Constant function, cannot assign a value to i X Private member f X g X h Page 2 of 23 Question 3: An electricity company charges the following rates for domestic users to discourage large consumption of energy: • For the first 100 units - 50 Fils per unit • Beyond 100 units - 60 Fils per unit If the total cost is more than 25 Dinar then an additional surcharge of 15% is added. Define a class Electricity. The class has two data attributes: units and cost of type double. The class should contain the following: A. B. C. D. E. A constructor to initialize units and cost by 0. A function setUnits to assign the value for units data member. A void function Bill to compute the cost based on above description. A function details to display the details of a user bill: consumed units and cost. A function addBills to add two Electricity objects. The parameter object is to be added to the implicit object: Electricity addBills(Electricty e1) #include <iostream> using namespace std; class Electricity { private: double units; double cost; public: Electricity(); void setUnits(double); void Bill(); void details(); Electricity addBills(const Electricity&); ~Electricity(); }; Electricity::Electricity() { units = cost = 0.0; } void Electricity::setUnits(double u) { units = (u > 0.0) ? u : 0.0; } void Electricity::Bill() { if (units <= 100) cost = units * 50; else cost = units * 60; if (cost > 2500) cost += cost*0.15; } void Electricity::details() Page 3 of 23 { cout << "units: " << units <<" cost: " << cost << endl; } Electricity Electricity::addBills(const Electricity& E) { Electricity Temp; Temp.units = units + E.units; Temp.Bill(); // calling function Bill to calculate the new cost return Temp; } Electricity::~Electricity() { } Page 4 of 23 Question 4 Write a program to test the class Electricity you have written in the previous question. Your program will: a) Declare an array of Electricity objects of size 12 b) Set the amount of units and compute the cost for each element of the array. Let user enter the consumed units for each object. c) Declare an Electricity object named TOTAL_BILL. Then, with a for loop, add the 12 objects to TOTAL_BILL int main () { Electricity data[12]; double u; for (int i=0; i<12; i++) { cin >> u; data[i].setUnits(u); data[i].Bill(); } Electricity TOTAL_BILL; for (int i=0; i<12; i++) TOTAL_BILL = TOTAL_BILL.addBills(data[i]); return 0; } Page 5 of 23 Question 5 For each row in below table, specify how would you create the new class using existing class: inheritance (is-A) or composition (has-A): Existing Class Employee President Hand Student Manager Shape New Class inheritance Full time Employee Country Body Computer Student Business Circle X composition X X X X X Page 6 of 23 Question 6: 1. When class B is inherited from class A, what is the order in which the constructors of those classes are executed A. Class A first Class B next B. Class B first Class A next C. Class B's only as it is the child class D. Class A's only as it is the parent class 2. Which of the following operators below allow defining the member functions of a class outside the class? A. :: B. :? C. ? D. % 3. Which of the following can be overloaded? A. member functions B. constructors C. destructors D. A and B 4. The default access level assigned to members of a class is ___________ A. Private B. Public C. Protected D. Needs to be assigned 5. For which type of class private and protected members of the class can be accessed from outside the same class in which they are declared A. No such class exist B. Friend C. Static D. Virtual 6. The return value of the following code is Class1& test(Class1 obj) { Class1 *ptr = new Class1(); ......... return *ptr; } A. object of Class1 B. reference to ptr C. reference of Class1 D. object pointed by ptr 7. class Example{ public: int a,b,c; Example(){a=b=c=1;} //Constructor 1 Example(int a){a = a; b = c = 1;} //Constructor 2 Example(int a,int b){a = a; b = b; c = 1;} //Constructor 3 Example(int a,int b,int c){ a = a; b = b; c = c;} //Constructor 4 } In the above example of constructor overloading, the following statement will call which constructor Page 7 of 23 Example obj = new Example (1,2,3.3); A. Constructor 2 B. Constructor 4 C. Constructor 1 D. Type mismatch error 8. Which of the following is not a type of constructor? A. Copy constructor B. Friend constructor C. Default constructor D. Parameterized constructor 9. Which of the following keyword is used to overload an operator? A. overload B. operator C. friend D. override 10. Inside a function definition for a member function of an object with data element x, which of the following is equivalent to this->x: A. *this.x B. *this->x C. x D. Both A and C are correct Page 8 of 23 Question 7: What is the output of the following: (a) class test { public: static int n; test () { n++; }; ~test () { n--; }; }; int test::n=0; int main () { test a; test b[5]; cout << b[3].n << endl; test * c = new test; cout << a.n << endl; delete c; cout << test::n << endl; return 0; } 6 7 6 (b) class A { public: A() {x=10;} A(int a):x(a){} void SetX(int a) { x=a;} int GetX() { return x;} virtual void print() { cout << x<<endl;} private: int x;}; class B : public A { public: B(int h):y(h) {A::SetX(3); } B() { y=5;} void print() { cout << (*this).y + this->GetX()<<endl; } private: int y; }; int main() { B b1, b2(2),* b3; A *a1 =new B(7); A *a2 =new A(7); b1.print(); b2.print(); b3 = new B(6); b3->print(); a1->print(); a2->print(); return 0;} 15 5 9 10 7 Page 9 of 23 Question 8: A Course class is used to represent course information which contains a set of grades for certain course. Given the following Course class: class Course { public: Course(int count); ~Course(); Course( const Course &); private: int grades_count; int * grades; }; Write the implementation of the following functions: A. Course(int count): A constructor that creates an array of grades based on count and initlializes the grades count member variable. Course::Course(int count) { grades_count=count; grades= new int[grades_count]; } B. ~Course(): A destrcutor that clears the course object. Course::~Course() { delete [] grades; } C. Course( const Course &): A copy construcor. Course::Course(const Course & c) { grades_count=c.grades_count; grades= new int[grades_count]; for (int i=0;i <grades_count;i++) grades[i]=c.grades[i]; } Page 10 of 23 Question 10 Trace the following program and write the generated output in the box below only? #include<iostream.h> class Product { protected: int price; public: void set_price (int a){ price = a; } Polygon: 10 Total= 0 Car: 40 Total= 400 Computer: 30 Total= 150 Computer: 0 Total= 0 virtual int Selling_Price(){return 0;} virtual void print() { cout <<"Polygon: "<< price; } }; class Computer: public Product { public: int Selling_Price () { return price * 5 ; } void print() { cout<< "Computer: "<< price; } }; class Car: public Product { public: int Selling_Price () { return price * 10;} void print() { cout<< "Car: "<< price; } }; void main() { int i; int x[10] = {10,20,30,40,50,10,30,50,70,0}; Product *ptr[10]; ptr[0] = new Product; for(i=1; i<5 ;i++) ptr[i]= new Car; for(i=5; i<10 ;i++) ptr[i]= new Computer; for(i=0; i<10 ;i++) ptr[i]-> set_price( x[i] ); i=0; while( i < 10 ) { ptr[i]->print(); cout << " Total= "<< ptr[i]->Selling_Price(); cout << endl; i += 3; } } Page 11 of 23 Question 13 1. What does the following declaration declare? int *countPtr, count; a. b. c. d. 2. Two integer variables. One pointer to an integer and one integer variable. Two pointers to integers. The declaration is invalid. Inside a function definition for a member function of an object with data element x, which of the following is equivalent to this->x: a. *this.x b. *this->x c. x d. Both A and C are correct e. None of the above 3. 4. One of the following is not correct when talking about static member functions: a. static member functions can only access other static member functions and static data members. b. static member functions can be called even if no object of their class is created. c. d. static member functions cannot be called from an object of their class. None of the above. The correct function name for overloading the multiplication operator is: a. b. c. d. 5. operator* operator(*) operator:* operator_* A friend function defined in a class is considered a member function of that class: a. True b. False 6. With Inheritance, the order of execution for the constructors when creating an object of a derived class is as follows: a. b. c. d. Parent class constructor and then derived class constructor Derived class constructor and then parent class constructor There is no order of execution, parent class constructors are never called None of the above Page 12 of 23 Question 15 class MyData { private: int x; int y; static int z; public: MyData (int m =4, int n=5) { x=m; y = n; z+=2; cout<< x+y <<endl; return temp; } }; int MyData::z = 6; void main () { MyData var1(1,4), var2(4); MyData MyDataArray [3]; cout<< MyData::getZ() <<endl; MyDataArray[1].setXY(var1+var2,3); MyDataArray[0] = var1 * var2 * MyDataArray[1]; cout <<MyDataArray[1].getX()+MyDataArray[0].getX() <<endl; } int getX () { return x; } int getY () {return y;} static int getZ () {return z;} MyData& setXY (int a, int b) { x=a; y=b; return *this; } int operator+ (MyData t) { return x+y+t.x+t.y; } var1.setXY( MyDataArray[0].getX(),3 ).setXY(12,2); MyDataArray[2] = var2 * MyDataArray[0] ; cout << var1.getX() << endl; if ( MyDataArray[1].getY() ) cout << "Demo ends\n"; else cout << "Demo stops\n"; MyData operator* (MyData t) { MyData temp; temp.x = x * t.x ; temp.y= ++temp.x*3 ; } 16 5 20 85 9 12 Demo ends Page 13 of 23 Question 16: Given the following two classes GasTank and Engine: class GasTank { public: GasTank():level(0),capacity(40) {}; bool IsEmpty() { return level==0;}; void AddGas(float val) { if (level+val<=capacity) level+=val;} void ConsumeGas() {level--;} private: float level; float capacity; }; class Engine {public: void Start() {… }; // will start the engine void Stop() {… }; // will stop the engine void MoveForward() {… } // moves car forward void MoveBackward() {…} // moves car backward void StopMoving() {…} // stops car moving private: …… }; A car has engine and gas tank, build a new class called Car class with two main components engine and gas tank. In addition, class should have gear_level member variable. Write the complete definition of class Car. The class should also include the following functions: 1. void StartEngine() that starts the car engine. The function should check gas level and if empty shows message “can’t start car, no Gas”, 2. void StopEngine() that stops the car engine. 3. void SetGear(int Level) that sets the car gear level (1 means drive forward, 2 drive reverse and 3 Park (stop moving)) 4. void StepOnGas() will do one of three things based on gear level,if drive forward it will ask engine to moveforward, if reverse it will ask engine to move backward, else it will display message “Vrooom Vrooom Vroooooooooooom” and it will not move. 5. void FillGas(float value) Will fill the car gas tank with given value. Page 14 of 23 class Car { public: void StartEngine() { if (tank.IsEmpty()) { cout <<" can't start car, no Gas"<<endl; return; } else engine.Start(); } void StopEngine() {engine.Stop(); } void SetGear(int level) { gear_level=level; } void StepOnGas() { switch(gear_level) { case 1: engine.MoveForward(); break; case 2: engine.MoveBackward(); break; case 3: cout <<" Vrooom Vrooom Vroooooooooooom"; break; } } void FillGas(float value){ tank.AddGas(value); private: Engine engine; GasTank tank; int gear_level; } }; Page 15 of 23 Question 17: Write a class named Cabinet. The class should have private data members named ArabicBooks, EnglishBooks and ComputerBooks all of type integer. The class should have the following: 1. A member function named GetTotalBooks to retrieve the number of all books in the cabinet. int GetTotalBooks() { return EnglishBooks+ArabicBooks+ComputerBooks; } 2. Overload the + operator to allow adding two objects of type Cabinet and return an object of same type (note, add the ArabicBooks to ArabicBooks, EnglishBooks to EnglishBooks, and so on). Cabinet operator+ (Cabinet b) { Cabinet temp; temp.EnglishBooks= EnglishBooks+b.EnglishBooks; temp.ArabicBooks= ArabicBooks+b.ArabicBooks; temp.ComputerBooks= ComputerBooks+b.ComputerBooks; return temp; } 3. Overload the – operator to allow subtracting books from the cabinet. For Example, Cabinet – 2 will return a new cabinet object with 2 books less from each type of books. If one of the categories does not have 2 books or more, it should not subtract from that category. Cabinet operator- (int value) { Cabinet temp; temp=(*this); if (EnglishBooks>=value) temp.EnglishBooks= EnglishBooks-value; if (ArabicBooks>=value) temp.ArabicBooks= ArabicBooks-value; if (ComputerBooks>=value) temp.ComputerBooks= ComputerBooks-value; return temp; } 4. Overload the > operator to compare between two cabinet objects based on their total number of books. bool operator> (Cabinet b) {return GetTotalBooks()>b.GetTotalBooks(); } Page 16 of 23 5. Overload the << operator to allow printing cabinet objects using cout. ostream & operator { output output output return } << ( ostream & output , const Cabinet c) << "Arabic " <<c.ArabicBooks<<endl; << "Computer "<< c.ComputerBooks<<endl; << "English " << c.EnglishBooks<<endl; output; In case you need to combine all: class Cabinet { public: Cabinet(int e,int a,int c) { EnglishBooks=e; ArabicBooks=a; ComputerBooks=c; } Cabinet() { EnglishBooks=0; ArabicBooks=0; ComputerBooks=0; } int GetTotalBooks() { return EnglishBooks+ArabicBooks+ComputerBooks; } Cabinet operator+ (Cabinet b) { Cabinet temp; temp.EnglishBooks= EnglishBooks+b.EnglishBooks; temp.ArabicBooks= ArabicBooks+b.ArabicBooks; temp.ComputerBooks= ComputerBooks+b.ComputerBooks; return temp; } Cabinet operator- (int value) { Cabinet temp; temp=(*this); if (EnglishBooks>=value) temp.EnglishBooks= EnglishBooks-value; if (ArabicBooks>=value) temp.ArabicBooks= ArabicBooks-value; if (ComputerBooks>=value) temp.ComputerBooks= ComputerBooks-value; return temp; } Page 17 of 23 bool operator> (Cabinet b) {return GetTotalBooks()>b.GetTotalBooks(); } friend ostream & operator << ( ostream & , const Cabinet ); private: int EnglishBooks,ArabicBooks,ComputerBooks; }; ostream & operator { output output output return } << ( ostream & output , const Cabinet c) << "Arabic " <<c.ArabicBooks<<endl; << "Computer "<< c.ComputerBooks<<endl; << "English " << c.EnglishBooks<<endl; output; Page 18 of 23 Question 18 Trace the following program and write the generated output in the box below only? #include <iostream> using namespace std; class Demo { private: int x; bool y; static int z; public: Demo (int m =5, bool n=true) { x=m; y = n; z+=5; } int getX () { return x; } bool getY () {return y;} static int getZ () {return z;} Demo& setX (int a) { x=a; return *this; } Demo operator* (Demo d) { x = d.x * 2 ; cout<< x <<endl; return *this; } ~Demo () { x= 0; y = 0; z+=5; } }; int Demo::z = 20; void main () { Demo Demo1(1,false), Demo2(4,false); Demo DemoArray [3]; Demo *ptr; ptr = new Demo[10]; cout<< Demo::getZ() <<endl; DemoArray[0] = Demo1 * Demo2; delete [] ptr; cout << Demo2.getZ() << endl; Demo1.setX( Demo1.getZ() ); DemoArray[2] = Demo1 * DemoArray[1] ; cout << Demo2.getX() << endl; DemoArray[0] = Demo(10,true) * DemoArray[0]; cout<< Demo::getZ() <<endl; } 95 8 155 10 4 16 185 Page 19 of 23 Question 27: Trace the following programs and write the generated output in the boxes below only? A (3 points) B (4 points) #include <iostream> using namespace std; #include <iostream> using namespace std; class myMessage{ class myClass { int code; int count; public: myClass(int m=3) { count=m*2; code=m; } int f2( int v) { return v+count; } void f1(int&x,int y){ x=count; y=f2(code); } }; int main() { public: myMessage(int b=7); void print(); void f2( int); private: int c; }; myMessage::myMessage(int b) { c=b; } void myMessage::f2(int c2) { c=c2; } void myMessage::print() { cout<<c<<endl; } int main() { myMessage P,Q(5); myMessage R[5]; myClass obj1,obj2(7); int val1=3,val2=5; obj2.f1(val1,obj1.f2(val2)); cout<<"\nval1 :"<<val1; cout<<"\nval2 :"<<val2; for (int i=0; i<3;i++) R[i].f2(i); } for (int i=0; i<4;i++) R[i].print(); } val1 :14 val2 :5 P.print(); Q.print(); 7 5 0 1 2 7 Page 20 of 23 Question 28: a) Write a C++ class to define a Circle, the class should have: • Three private member variables: x, y and r where (x,y) represents the center of the circle and r represents the radius of the circle. • Two constructors ( one default no arguments and one with three integer values x_val, y_val, and r_val) • SetXY( int v1,int v2) to set the values for the center. • SetRadius(int v) to set the value of the radius • Move(int dx, int dy) member function that moves the circle center by dx value for the x-axis of the center and dy for the y-axis. For example, if we have circle with center (30,32) and we apply Move(3,-8), the new center will be (33,24). • float GetArea() function that computes the area of the circle. Area is computed using the following formula: Area= π .R2 where π = 22/7 and R is the radius. • Bool IsSmaller( Circle c) to compare area of an object with object c and return true if area of object c is bigger. #include <iostream> using namespace std; class Circle { public: Circle() { x=y=r=0;} Circle(int x_val,int y_val, int r_val) { x=x_val; y=y_val; r=r_val; } void SetXY(int v1, int v2) {x=v1; y=v2; } void SetRadius(int v) {r=v; } void Move(int dx, int dy) { x+=dx; y+=dy;} float GetArea() { return (r*r*(22.0/7) );} bool IsSmaller(Circle c) { return GetArea()<c.GetArea(); } private: int x,y,r; }; Page 21 of 23 Question 29: Based on Circle class you defined in previous question, a) Write a function named ReadCircles that receives an array of objects of type circle and the size of the array, read values for the different member variables, and set them using their corresponding setters. b) Write a main function that declares an array named data of type Circle and size 10. Use ReadCircles functions to read values inside array data. c) Inside main function, move all circle objects inside the array data by move values (dx=24,dy=13) and print their area sizes. void ReadCircles( Circle a[], int size) { int x,y,r; for (int i=0;i<size;i++) { cout << " please enter values for x y and radius of a circle"<<endl; cin >> x >> y >> r; a[i].SetXY(x,y); a[i].SetRadius(r); } } int main() { Circle data[10]; ReadCircles(data,10); for (int i=0; i<10;i++) { data[i].Move(24,13); cout <<"circle number "<<i+1<<" has area "<<data[i].GetArea() <<endl; } return 0; } Page 22 of 23 Question 30: #include <iostream> using namespace std; class Base{ public: Base(int v1, int v2) { x=v1; y=v2; cout << " BC "<<x <<" "<<y<<endl; } ~Base() { cout << " BD "<<x <<" "<<y<<endl; } void SetValues(int v1, int v2) {x=v1; y=v2; } int GetX() { return x;}; int GetY() { return y;}; private: int x; int y; }; class Derived: public Base { public: Derived( int v1, int v2, int v3, int v4 ):Base(v1,v2),k(v3),m(v4) { cout << " DC "<<GetX() <<" "<<GetY()<<" "<<k<<" "<<m<<endl; } ~Derived() { cout << " DD "<<GetX() <<" "<<GetY()<<" "<<k<<" "<<m<<endl; } int SetValues(int v1, int v2, int v3, int v4 ) { Base::SetValues(v1,v2); BC 10 20 k=v3; BC 100 200 m=v4; DC 100 200 300 400 } BC 1 2 DC 1 2 3 4 private: DD 1 2 3 4 int k,m; BD 1 2 DD 100 200 300 400 }; BD 100 200 BD 10 20 int main() { Base o1(10,20); Derived *p,o2(100,200,300,400); p= new Derived(1,2,3,4); delete p; return 0; } Page 23 of 23
© Copyright 2024