2014 - CE104 OOPC++ 1st - Internal Solution
2014 - CE104 OOPC++ 1st - Internal Solution
Q-1 (a) Mention whether the following statements are True of False with respect to C++. [03]
FALSE 1. A member function in a class cannot be a friend function of another class.
FALSE 2. void is the default return type of any constructor.
TRUE 3. If the function is recursive it should not be inline.
TRUE 4. The function call can appear on the left-hand side of assignment operator.
FALSE 5. A member function of a class can’t be private.
TRUE 6. When a function is defined inside a class, it is treated as an inline function.
(b) What is the output of the following code? [04]
(1) void fun(char c, int q=2) (2) void dob(int &l, int m, int &n)
{c=c+q; { l=n+n;
cout<<c;} n=l+l; }
main( ){ main( ){
char z='p'; int no=4; int n1=3,n2=5,n3=2;
fun(z,no); dob(n1,n3,n2);
fun('m',no); cout<<n1<<" "<<n3;
} }
ANS: tq ANS: 10 2
(3) int z=5; (4) void f(long int arg) {cout<<int(arg);}
main( ){ void f(float arg) {cout<<int(arg);}
int z=0; z++; main( ){
cout<<z<<" "<<::z; f(3.0f);
} ANS: 1 5 } ANS: 3
(c) Attempt any TWO. [08]
1. Define class char_array having char s[10] member. Define length, input and
display functions. Also define a member function test( ) that compares length
of two strings belongs to two objects.
Ans:
#include<iostream>
using namespace std;
class char_array
{
private:
char s[10];
Page 1 of 6
public:
int length();
void input();
void disp();
void test(char_array);
};
int char_array::length()
{
int i;
for(i=0;s[i]!='\0';i++);
return i;
}
void char_array::input()
{
cout<<"Enter STRING:";
cin>>s;
}
void char_array::disp()
{
cout<<"STRING:"<<s<<endl;
}
void char_array::test(char_array T)
{
if(length()<T.length())
cout<<"First is shorter than Second String."<<endl;
else
cout<<"First is longer than Second String."<<endl;
}
main()
{
char_array s1,s2;
s1.input();
s2.input();
s1.test(s2);
s1.disp();
s2.disp();
return 0;
}
2. Define class Number having int n as data. Define constructor, enter and output
functions. A class has member function diff( ) that displays difference of two
objects. (Function should support N3=N1.diff(N2);)
ANS:
#include<iostream>
using namespace std;
class Number
{
private:
int n;
public:
Number();
void enter();
void output();
Number diff(Number);
};
Number::Number()
{
n=0;
}
void Number::enter()
{
cout<<"Enter Integer:";
cin>>n;
}
Page 2 of 6
void Number::output()
{
cout<<"Integer:"<<n<<endl;
}
Number Number::diff(Number T)
{
Number temp;
temp.n=n-T.n;
return temp;
}
int main()
{
Number N1,N2,N3;
N1.enter();
N2.enter();
N3=N1.diff(N2);
N3.output();
return 0;
}
3. Define class Mobile having data members: year of manufacture, price. A class
has member functions to get and print data. Define function validity( ) that
checks whether the mobile is older than 2005 or not. If mobile is older, it is
invalid. Input data for 10 mobiles in main( ) and display information about the
phone which is invalid.
ANS:
#include<iostream>
using namespace std;
class Mobile
{
private:
int yom;
int price;
public:
void get();
void disp();
void validity();
};
void Mobile::get()
{
cout<<"Enter Manufacturing Year:";
cin>>yom;
cout<<"Enter Price of Mobile:";
cin>>price;
}
void Mobile::disp()
{
cout<<"YEAR:"<<yom<<" "<<"PRICE:"<<price;
}
void Mobile::validity()
{
if(yom<2005)
{
disp();
cout<<" IS INVALID.";
}
cout<<endl;
}
int main()
{
Mobile M[10];
int i;
for(i=0;i<10;i++)
M[i].get();
for(i=0;i<10;i++)
Page 3 of 6
M[i].validity();
return 0;
}
(b) Define a class house with length and width. Define another class office with length [06]
and width. Both classes have member functions to input and print data. Write a
function that receives objects of both classes and compare them according to
their area. Also define main( ) to test the function. Define all functions outside the
class.
ANS:
#include<iostream>
using namespace std;
class office;
class house
{
private:
int width;
int length;
public:
void enter();
void print();
friend void compare(house,office);
};
class office
{
private:
int width;
int length;
public:
void input();
void display();
friend void compare(house,office);
};
void house::enter()
{
cout<<"Enter HOUSE DATA:"<<endl;
cout<<"WIDTH:";
cin>>width;
cout<<"LENGTH:";
cin>>length;
}
void office::input()
{
cout<<"Enter OFFICE DATA:"<<endl;
cout<<"WIDTH:";
cin>>width;
cout<<"LENGTH:";
cin>>length;
}
void house::print()
{
cout<<"HOUSE DATA:"<<endl;
cout<<"WIDTH:"<<width<<endl;
cout<<"LENGTH:"<<length<<endl;
Page 4 of 6
}
void office::display()
{
cout<<"OFFICE DATA:"<<endl;
cout<<"WIDTH:"<<width<<endl;
cout<<"LENGTH:"<<length<<endl;
}
void compare(house H,office O)
{
int HA,OA;
HA=H.width*H.length;
OA=O.width*O.length;
cout<<"Area of House:"<<HA<<endl;
cout<<"Area of Office:"<<OA<<endl;
if(HA==OA)
cout<<"AREA of House and Office is EQUAL:"<<HA;
else if(HA<OA)
cout<<"House is Smaller than Office";
else cout<<"House is Larger than Office.";
}
int main()
{
house H;
office O;
H.enter();
O.input();
H.print();
O.display();
compare(H,O);
return 0;
}
OR
Define a class room having members: length, width and No of beds. A class has
constructor, destructor and member functions to get and display data. Write a
member function ==( ) that compare two rooms based on their area. Also define
main( ) to test the class. Define all functions outside the class.
ANS:
#include<iostream>
using namespace std;
class room
{
int length;
int width;
int nob;
public:
room();
~room();
void get();
void disp();
int operator ==(room);
};
room::room()
{
length=width=nob=0;
}
room::~room(){}
void room::get()
{
cout<<"Enter Length:";
cin>>
Page 5 of 6
length;
cout<<"Enter Width:";
cin>>width;
cout<<"Enter No of Beds:";
cin>>nob;
}
void room::disp()
{
cout<<"LENGTH:"<<length<<endl;
cout<<"WIDTH :"<<width<<endl;
cout<<"No of BEDS:"<<nob<<endl;
}
int room::operator ==(room R)
{
int Area1, Area2;
Area1=length*width;
Area2=R.length*R.width;
if(Area1==Area2)
return 1;
else return 0;
}
int main()
{
room R1,R2;
R1.get();
R2.get();
if(R1==R2)
cout<<"Both the Rooms have EQUAL area";
else
cout<<"Both the Rooms have DIFFERENT area";
return 0;
}
*********
Page 6 of 6