




















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Object oriented programming revision questions
Typology: Exercises
1 / 28
This page cannot be seen from the preview
Don't miss anything!





















Answers:
Question 1:
What is the output in each of the following code?
#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
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:
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() { }
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:
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
A. Copy constructor B. Friend constructor C. Default constructor D. Parameterized constructor
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];
}
#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
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
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: …… };
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; }
bool operator> (Cabinet b)
{return GetTotalBooks()>b.GetTotalBooks(); }
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; }
#include
*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;
A (3 points) B (4 points)
#include
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
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(); }