Q.) Write Any Four Benefits of OOP
Q.) Write Any Four Benefits of OOP
1. We can eliminate redundant code and extend the use of existing classes.
2.We can build programs from the standard working modules that communicate
with one another, rather than having to start writing the code from scratch. This
leads to saving of development time and higher productivity.
3. The principle of data hiding helps the programmer to build secure programs
that cannot be invaded by code in other parts of the
program.
4. It is possible to have multiple instances of an object to co-exist without any
interference.
5. It is possible to map objects in the problem domain to those in the program.
Q.]Describe ‘this’ pointer with an example.
this’ pointer: C++ uses a unique keyword called „this‟ to represent an object
that invokes a member function. This unique pointer is automatically passed to
a member function when it is invoked. „this‟ is a pointer that always point to
the object for which the member function was called.
For example, the function call A.max ( ) will set the pointer „this‟ to
the address of the object A. Then suppose we call B.max ( ), the
pointer „this‟ will store address of object B.
Example:
#include<iostream.h>
class sample
{
int a;
public:
void setdata(int x)
{
this ->a=x;
}
void putdata()
{
cout<<this ->a;
}
};
void main()
{
sample s;
s.setdata(100);
s.putdata( );
}
In the above example, this pointer is used to represent object s when
setdata ( ) and putdata ( ) functions are called.
Q.]State the rules for writing destructor function.
Rules for writing destructor function are:
1) A destructor is a special member function which should destroy
the objects that have been created by constructor.
2) Name of destructor and name of the class should be same.
3) Destructor name should be preceded with tilde (~) symbol.
4) Destructor should not accept any parameters.
5) Destructor should not return any value.
6) Destructor should not be classified in any types.
7) A class can have at most one destructor
Q.]What are the rules for virtual function?
Rules for virtual function:
1. The virtual functions must be members of some class.
2. They cannot be static members.
3. They are accessed by using object pointers.
4. A virtual function can be a friend of another class.
5. A virtual function in a base class must be defined, even though it may not be
used.
6. The prototypes of the base class version of a virtual function and all the
derived class versions must be identical.
7. We cannot have virtual constructors, but we can have virtual destructors.
8. While a base pointer can point to any type of the derived object, the reverse is
not true.
Q.]Write any three rules of operator overloading.
Rules for overloading operators:
1. Only existing operators can be overloaded. New operators cannot be created.
2. The overloaded operator must have at least one operand that is of user
defined data type.
3. We can’t change the basic meaning of an operator. That is to say, we can’t
redefine the plus(+) operator to subtract one value from other.
4. Overloaded operators follow the syntax rules of the original operators. They
can’t be overridden.
5. There are some operators that can’t be overloaded.
6. We can’t use friend functions to overload certain operators. However,
member function scan be used to overload them.
9. When using binary operators overloaded through a member function, the left
hand operand must be an object of the relevant class.
10. Binary arithmetic operators such as +,-,* and / must explicitly returna value.
They must not attempt to change their own arguments.
Q.]What is parameterized constructor?
A constructor that accepts parameters is called as parameterized
constructor.
In some applications, it may be necessary to initialize the various data
members of different objects with different values when they are created.
Parameterized constructor is used to achieve this by passing arguments to
the constructor function when the objects are created.
Example:
class ABC
{
int m;
public:
ABC(int x)
{
m=x;
}
void put()
{
cout<<m;
}
};
void main()
{
ABC obj(10);
obj.put();
}
In the above example, constructor ABC (int x) is a parameterized
constructor function that accepts one parameter. When „obj‟ object is
created for class ABC, parameterized constructor will invoke and data
member m will be initialized with the value 10 which is passed as an
argument. Member function put ( ) displays the value of data member „m‟
Q.]Explain the friend function with proper example.
Friend function:
The private members of a class cannot be accessed from outside the class but in
some situations two classes may need access of eachother‟s private data. So a
common function can be declared which can be made friend of more than one
class to access the private data of more than one class. The common function is
made friendly with all those classes whose private data need to be shared in that
function. This common function is called as friend function. Friend function is
not in the scope of the class in which it is declared. It is called without any
object. The class members are accessed with the object name and dot
membership operator inside the friend function. It accepts objects as arguments.
#include<iostream.h> {
#include<conio.h> cout<<"\n Before swapping:";
class B; cout<<"\n Value for x="<<a.x;
class A cout<<"\n Value for y="<<b.y;
{ int temp;
int x; temp=a.x;
public: a.x=b.y;
void accept() b.y=temp;
{ cout<<"\n After swapping:";
cout<<"\n Enter the value for x:"; cout<<"\n Value for x="<<a.x;
cin>>x; cout<<"\n Value for y="<<b.y;
} }
friend void swap(A,B); void main()
}; {
class B A a;
{ B b;
int y; clrscr();
public: a.accept();
void accept() b.accept();
{ swap(a,b);
cout<<"\n Enter the value for y:"; getch();
cin>>y; }
}
friend void swap(A,B);
};
void swap(A a,B b)
Q.]State and explain the visibility modes used in inheritance.
Visibility modes: private protected public
Private:
When a base class is privately inherited by a derived class, ‘public members’
and , ‘protected member’ of the base class become , ‘Private member’ of the
derived class.
Therefore, the public and protected members of the base class can only be
accessed by the member functions of derived class but, cannot be accessed by
the objects of the derived class.
Syntax:
class derived: private base
{
//Members of derived class;
};
Public:
When a base class is publicly inherited by a derived class then, ‘protected
members’ of base class becomes ; ‘protected members’ and ‘public members’
of the base class become , ‘public members’ of the derived class.
Therefore the public members of the base class can be accessed by both the
member functions of derived class as well as the objects of the derived class.
Syntax:
class derived: public base
{
//Members of derived class;
};
Protected:
When a base class is protectedly inherited by a derived class, ‘public and
protected members’ of the base class become ,‘protected members’ of the
derived class.
Therefore the public and protected members of the base class can be accessed
by the member functions of derived class as well as the member functions of
immediate derived class of it but they cannot be accessed by the objects of
derived class
Syntax:
class derived: protected base
{
//Members of derived class;};
Q.]Differentiate between run time and compile time polymorphism.
Consider a hybrid inheritance as shown in the above diagram. The child class
has two direct base classes, Parent1 and Parent2 which themselves have a
common base class as Grandparent. The child inherits the members of
Grandparent via two separate paths. All the public and protected members of
Grandparent are inherited into Child twice, first via Parent1 and again via
Parent2. This leads to duplicate sets of the inherited members of Grandparent
inside Child class. The duplication of inherited members can be avoided by
making the common base class as virtual base class while declaring the direct or
intermediate base classes as shown below.
class Grandparent
{
};
class Parent1:virtual public Grandparent
{
};
class Parent2:virtual public Grandparent
{
};
class Child: public Parent1,public Parent2
{
};
Q.]Write a C++ program to find Q.]Write a ‘C++’ program to find
whether the entered number is factorial of given number using
even or odd. loop.
#include<iostream.h> #include<iostream.h>
#include<conio.h> #include<conio.h>
void main() void main()
{ {
int num; int no,fact=1,i;
clrscr(); clrscr();
cout<<"\nEnter a Number ";
cout<<"Enter number:";
cin>>num;
if(num%2==0)
cin>>no;
{ for(i=1;i<=no;i++)
cout<<"\nEntered number is even"; {
} fact=fact*i;
else }
{ cout<<"Factorial ="<<fact;
cout<<"\nEntered number is odd"; getch();
} }
getch();
}
Q.]Write a C++ program to accept void get1()
array of five elements, find and {
display smallest number from an cout<<"Enter number 1:";
array. cin>>no1; }
#include<iostream.h> friend void smallest(class1
#include<conio.h> no1,class2 no2);
void main() };
{ class class2
int a[5],smallest,i; {
clrscr(); int no2;
cout<<" Enter array elements:"; public:
for(i=0;i<5;i++) void get2()
cin>>a[i]; {
smallest=a[0]; cout<<"Enter number 2:";
for(i=1;i<5;i++) cin>>no2;
{ }
if(a[i]<smallest) friend void smallest(class1
{ no1,class2 no2);
smallest=a[i]; };
} void smallest(class1 c1,class2 c2)
} {
cout<<endl<<"Smallest if(c1.no1<c2.no2)
number="<<smallest; cout<<"no1 is smallest";
getch(); else
} cout<<"no2 is smallest";
Q.]Write a C++ program to find }
smallest number from two void main()
numbers using friend function. {
(Hint: use two classes).
class1 c1;
#include<iostream.h>
class2 c2;
#include<conio.h>
clrscr();
class class2;
c1.get1();
class class1
c2.get2();
{
smallest(c1,c2);
int no1;
getch();
public:
}
Q.]Describe use of static data member in C++ with example.
Use of static data member:
1. Static data member is used to maintain values common to the entire class.
2. It is initialized to zero when the first object of its class is created.
3. Only one copy of that member is created for the entire class and is shared by
all the objects of that class.
Example:
#include<iostream.h>
#include<conio.h>
class test
{
static int count;
int obj_no;
public:
void getdata()
{
obj_no=++count;
cout<<"\n Object number="<<obj_no;
}
static void showcount()
{
cout<<"\n total number of objects="<<count;
}
};
int test::count;
void main()
{
test t1,t2;
clrscr();
t1.getdata();
t2.getdata();
test::showcount();
test t3;
t3.getdata();
test::showcount();
getch();
}
Q.]Write a program to swap two Q.]Write a C++ program to swap
integers using call by reference two integer numbers and swap
method. two float numbers using function
#include<iostream.h> overloading.
#include<conio.h> #include<iostream.h>
void swap(int*p, int*q) #include<conio.h>
{ void swap(int a,int b)
int t; {
t=*p; int temp;
*p=*q; temp=a;
*q=t; a=b;
} b=temp;
void main() cout<<"\nInteger values after
{ swapping are:"<<a<<" "<<b;
int a,b; }
float x,y; void swap(float x,float y)
clrscr(); {
cout<<"Enter values of a and b\ float temp1=x;
n"; x=y;
cin>>a>>b; y=temp1;
cout<<"Before swapping\n"; cout<<"\nFloat values after
cout<<"a="<<a<<"\ swapping are:"<<x<<" "<<y;
tb="<<b<<endl; }
swap(&a, &b); void main()
cout<<"After swapping\n"; {
cout<<"a="<<a<<"\ clrscr();
tb="<<b<<endl; swap(10,20);
getch(); swap(10.15f,20.25f);
} getch();
}
Q.]Write a C++ program to add two 3 x 3 matrices and display addition.
#include<iostream.h> }
#include<conio.h> cout<<"Adding the two matrix to
void main() form the third matrix\n";
{ for(i=0; i<3; i++)
clrscr(); {
int mat1[3][3], mat2[3][3], i, j, for(j=0; j<3; j++)
mat3[3][3]; {
cout<<"Enter matrix 1 mat3[i][j]=mat1[i][j]+mat2[i][j];
elements :"; }
for(i=0; i<3; i++) }
{ cout<<"The two matrix added
for(j=0; j<3; j++) successfully...!!";
{ cout<<"The new matrix will be :\
cin>>mat1[i][j]; n";
} for(i=0; i<3; i++)
} {
cout<<"Enter matrix 2 for(j=0; j<3; j++)
elements :"; {
for(i=0; i<3; i++) cout<<mat3[i][j]<<" ";
{ }
for(j=0; j<3; j++) cout<<"\n";
{ }
cin>>mat2[i][j]; getch();
} }