Q. Is it possible to involve the private member data outside the
class? If yes? How? When do you need it?
Ans. By using friend function we
can use the private data of any class outside the class. A friend function of
any class can use there private data. For this we just have to declare the function
as a friend of particular class. E.g.
class abc
{
private:
int a;
friend void show();
};
};
void show()
{
{
abc obj;
obj.a=100;
cout<<”a = “<<obj.a;}
Q. Make a diagram to show the accessibility of private, public and protected.
Ans:-
In this example Class A is the
base class and class C is the subclass which derived from class A. class C can
use only ‘public’ and ‘protected’ members of class A private is not accessible.
And Class B can access only ‘public’ members of class A ‘protected’ and
‘private’ members are not accessible.
So, ‘protected’ mode allows a
private member can be access from out of Class.
Q. Discuss the role of ‘default constructor’.
Ans. Many times we have to
initialize a class type variable (object) when it is declared, much the same
way as initialization of an ordinary variable. C++ provide a special member
function called constructor which enable the initialization of object when it
is declare, this concept is also known as ‘automatically initialization’ of
object. A ‘constructor’ is a special function which name is same of class name
and not has any return type, it should be declared in public section, a
construction never be virtual. Constructor called/invoked automatically when we
make an object of class. It is called constructor because it constructs the
value of data members of the class. C++ also provides the facility of
constructor overloading. A constructor without any parameters is called default
constructor. If a default constructor is not made by user compiler supplies a
default constructor automatically. A default constructor is very use full
because calling of it is very convenience, to call a default constructor we not
have to take any extra step. We have to make an object of class to access the
members of it. As we make an object default constructor called automatically
which solve the many problems related to program ex. initialization of
variable, and other steps which we want to take at the very beginning of
program ex.
class abc
{
____
____
abc() //default constructor
{
____
____
}
____
____
};
void main()
{
abc obj; //creating an object of class.
____
____
}
}
As we make the object of class
abc, constructor is called automatically.
Q. ‘Employee’ is a class having member data_name, organization
and amount.
‘Contribution’ is another class having member data_name and amount.
Or
Write a program to find the total amount using a friend function.
Ans. #include<iostream.h>
#include<conio.h>
class Contribution;
class Employee
{
public:
char organisation[100];
void call()
{
cout<<"\n Enter Name of organisation ::
";
cin>>organisation;
calculate();
}
friend void calculate();
};
class Contribution
{
public:
void call()
{
calculate();
}
friend void calculate();
};
void calculate()
{
char ch,data_name[100];
int amount,total;
ch='y';
total=0;
while(ch!='n')
{
cout<<"\n Enter Item Name :: ";
cin>>data_name;
cout<<"\n Enter Price of Item ::" ;
cin>>amount;
total=total+amount;
cout<<"\n Press Enter to Continue 'n' for exit
:: ";
ch=getche();
}
cout<<"\n Total Amount = "<<total;
}
void main()
{
clrscr();
int choice;
cout<<"\n Enter Choice \n 1. Employee \n 2.
Contribution ";
cin>>choice;
switch(choice)
{
case 1:
Employee objEmp;
objEmp.call();
break;
case 2:
Contribution objCon;
objCon.call();
break;
default:
cout<<"\n Sorry ! wrong input ";
}
getch();
}
No comments:
Post a Comment