Object oriented programming (C++), Exercises of Object Oriented Programming

Object oriented programming revision questions

Typology: Exercises

2017/2018

Uploaded on 06/09/2018

omaramro
omaramro 🇯🇴

4.7

(3)

1 document

1 / 28

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Page 1 of 28
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;
}
};
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
code of obj2 : 13
code of obj3 : 13
code of obj4 : 12
Output
obj2.val is -1
obj2.val is 5
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c

Partial preview of the text

Download Object oriented programming (C++) and more Exercises Object Oriented Programming in PDF only on Docsity!

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 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; } };

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 code of obj2 : 13 code of obj3 : 13 code of obj4 : 12

Output obj2.val is - obj2.val is 5

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

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; //g

cout << "obj.x is " << obj.x << endl; //h

line legal illegal reason a X^ Cannot^ initialize^ at^ declaration b X^ Constructor^ has^ no^ return^ type

c X

d X Constructor cannot return a value

e X Constant function, cannot assign a value to i

f X

g X

h X Private member

{ 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() { }

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; }

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 Class B. reference to ptr C. reference of Class D. object pointed by ptr

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

Example obj = new Example (1,2,3.3); A. Constructor 2 B. Constructor 4 C. Constructor 1 D. Type mismatch error

  1. Which of the following is not a type of constructor?

A. Copy constructor B. Friend constructor C. Default constructor D. Parameterized constructor

  1. Which of the following keyword is used to overload an operator? A. overload B. operator C. friend D. override
  2. 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

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];

}

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; } 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; }

}

Polygon: 10 Total= 0 Car: 40 Total= 400 Computer: 30 Total= 150 Computer: 0 Total= 0

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; }

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; }

MyData operator* (MyData t) { MyData temp; temp.x = x * t.x ; temp.y= ++temp.x*3 ;

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;

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";

}

Demo ends

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.

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(); }

5. Overload the << operator to allow printing cabinet objects using cout.

ostream & operator << ( ostream & output , const Cabinet c) { output << "Arabic " <<c.ArabicBooks<<endl; output << "Computer "<< c.ComputerBooks<<endl; output << "English " << c.EnglishBooks<<endl; return 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; }

Question 18

Trace the following program and write the generated output in the box below only?

#include 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;

Question 27:

Trace the following programs and write the generated output in the boxes below only?

A (3 points) B (4 points)

#include using namespace std;

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() {

myClass obj1,obj2(7); int val1=3,val2=5; obj2.f1(val1,obj1.f2(val2)); cout<<"\nval1 :"<<val1; cout<<"\nval2 :"<<val2;

}

#include using namespace std; class myMessage{

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];

for (int i=0; i<3;i++) R[i].f2(i);

P.print(); Q.print();

for (int i=0; i<4;i++) R[i].print(); }

val1 :

val2 :