Page 113 - C++
P. 113
cout<<x<<'*'<<y<<'*'<<z<<endl;
}
void main()
{
int a = 2, b = 5;
demo(::a,a,b);
demo(::a,a,b);
}
Ans: 3*5*10
8*10*20
1 (f) Write a function in C++ to accept two integers as parameters and returns the greater of these numbers.
Ans:
int findbig(int a, int b)
{ if(a>b)
return a;
else if(b>a)
return b;
}
2(a) What do you understand by Data Encapsulation and Data Hiding? Also, give a suitable C++ code to
illustrate both.
Ans: Data Hiding: A class groups its members into three sections: private, protected and public. The private and
protected members remain hidden from outside world. Thus through private and protected members, a class enforces
data – hiding.
Encapsulation : The wrapping up of data and functions into a single unit (class) is called as encapsulation.
A class binds together data and its associated functions under one unit thereby enforcing encapsulation. It is a way of
implementing abstraction.
class ABC
{private: int a,b; //data hiding Data
protected: int c,d; + Encapsulation
public:
void disp( ) Function
{ }
};
(b) What is constructor overloading? Give an example to illustrate the same.
Ans: Constructor Overloading:The constructor of a class may also be overloaded so that even with different number
and types of initial values, an object may still be initialized. It implements poly(many)morphism(form).
class Rectangle
{ float l,b,a;
public:
Rectangle(){l=b=a=0;} //Default Constructor
Rectangle ( float len = 5.0, float bre = 5.0) //Parametrized Constructor with Default arguments
{l = len; b = bre;}
};
(c) Define a class HandSet in C++ with following description:
Private members: