0% found this document useful (0 votes)
24 views

Q.) Write Any Four Benefits of OOP

This program copies the contents of one text file to another text file. It takes the source and destination file names from the user. It opens both files, reads the source file character by character and writes to the destination file. After copying all contents, it displays a success message.

Uploaded by

Jay Bhakhar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Q.) Write Any Four Benefits of OOP

This program copies the contents of one text file to another text file. It takes the source and destination file names from the user. It opens both files, reads the source file character by character and writes to the destination file. After copying all contents, it displays a success message.

Uploaded by

Jay Bhakhar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

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.

Q.]Differentiate between contractor and destructor


Q.] Write a C++ program to count {
number of spaces present in clrscr();
contents of file ifstream fs;
Program: ofstream ft;
#include<iostream.h> char ch, fname1[20], fname2[20];
#include<conio.h> cout<<"Enter source file name
#include<fstream.h> with extension (like files.txt) : ";
void main() gets(fname1);
{ ifstream file; fs.open(fname1);
int s=0; if(!fs)
char ch; {
clrscr(); cout<<"Error in opening source
file.open("abc.txt"); file..!!";
while(file) getch();
{ exit(1); }
file.get(ch); cout<<"Enter target file name
if(ch==' ') with extension (like filet.txt) : ";
{ gets(fname2);
s++; ft.open(fname2);
} if(!ft)
} {
cout<<"\nNumber of spaces in cout<<"Error in opening target
text file are:"<<s; file..!!";
getch(); fs.close();
} getch();
exit(2);
Q.]copies contents of one file into }
another file. while(fs.eof()==0)
Assuming input file to be copied {
file1.txt contents are “Hello fs>>ch;
Friends…” and file where the ft<<ch;
contents need to copy is file2.txt }
already created cout<<"File copied
#include<iostream.h> successfully..!!";
#include<conio.h> fs.close();
#include<fstream.h> ft.close();
#include<stdio.h> getch();
#include<stdlib.h> }
void main()
Q.]Describe with examples, passing #include<iostream.h>
parameters to base class constructor #include<conio.h>
and derived class constructor by class base
creating object of derived class. {
When a class is declared, a int x;
constructor can be declared inside public:
the class to initialize data members. base(int a)
When a base class contains a {
constructor with one or more x=a;
arguments then it is mandatory for cout<<"Constructor in base
the derived class to have a x="<<x;
constructor and pass arguments to }
the base class constructor. When };
both the derived and base classes class derived: public base
contain constructors, the base {
constructor is executed first and int y;
then the constructor in the derived public:
class is executed. The constructor derived(int a,int b):base(a)
of derived class receives the entire {
list of values as its arguments and y=b;
passes them on to the base cout<<"Constructor in
constructors in the order in which derived.y="<<y;
they are declared in the derived }
class. };
General form to declare derived void main()
class constructor: {
Derived-constructor (arglist1, clrscr();
arglist (D)):Base1(arglist1) derived ob(2,3);
{ getch();
Body of derived class constructor }
} In the above example, base class
Derived constructor declaration constructor requires one argument
contains two parts separated with and derived class constructor
colon (:). First part provides requires one argument. Derived
declaration of arguments that are class
passed constructor accepts two values and
to the derived constructor and passes one value to base class
second part lists the function calls constructor.
to
the base constructors.
Example:
Q.]Describe following terms: Inheritance, data abstraction, data
encapsulation, dynamic binding.
Inheritance:
1. Inheritance is the process by which objects of one class acquire the
properties of objects of another class.
2. It supports the concept of hierarchical classification. It also provides the
idea of reusability.
Data abstraction:
1. Data abstraction refers to the act of representing essential features
without including the background details or explanations.
2. Classes use the concept of abstraction and are defined as a list of
abstract attributes such as size, weight and cost and functions to operate on
these attributes.
Data encapsulation:
1. The wrapping up of data and functions together into a single unit (called
class) is known as encapsulation.
2. By this attribute the data is not accessible to the outside world, and only
those functions which are wrapped in the class can access it.
Dynamic Binding:
1. Dynamic binding refers to the linking of a procedure call to be executed
in response to the call.
2. It is also known as late binding. It means that the code associated with a
given procedure call is not known until the time of the call at run-time.
Q.]Write a program to overload cout<<"\n After negation:";
the ‘—’ unary operator to negate N1. display ();
the values. getch ();
#include<iostream.h> Q.]Write a C++ program to
#include<conio.h> overload binary operator ‘+’ to
#include<string.h> concatenate two strings.
class Number #include<iostream.h>
{ #include<conio.h>
int x,y; #include<string.h>
public: class opov
Number (int a, int b) {
{ char str1[10];
a =x; public:
b =y; void getdata()
} {
void display() cout<<"\nEnter a strings";
{ cin>>str1;
cout<<"value of x=”<<x<<”\n }
Value of y= ”<<y; void operator +(opov o)
} {
void operator - ( ) cout<<strcat(str1,o.str1);
{ }
x = - x; };
y = - y; void main()
} {
}; opov o1,o2;
void main () clrscr();
{ o1.getdata();
Number N1(5,6); o2.getdata();
clrscr (); o1+o2;
N1. display (); getch();
-N1; }
Q.]Write a C++ program to {
append data from abc.txt to f.get(ch);
xyz.txt file. cout<< ch;
Assuming input file as abc.txt }
with contents "World" and output f.close();
file return 0;
named as xyz.txt with contents }
"Hello" have been already
created. Q.]Write a C++ program to write
‘Welcome to poly’ in a file. Then
#include <iostream.h> read the data from file and display it
#include<fstream.h> on screen.
int main() #include<iostream.h>
#include<conio.h>
{
#include<fstream.h>
fstream f; void main()
ifstream fin; {
fin.open("abc.txt",ios::in); char str[25] = "Welcome to
ofstream fout; poly",ch;
fout.open("xyz.txt", ios::app); clrscr();
if (!fin) ofstream fout;
{ fout.open("output.txt");
cout<< "file not found"; fout<<str;
} fout.close();
else ifstream fin;
{ fin.open("output.txt");
fout<<fin.rdbuf(); while (!fin.eof())
} {
fin.getline(str, 25);
char ch;
cout<<str<<endl;
f.seekg(0); }
while (f)
Q.] Describe structure of C++ program with diagram.
INCLUDE HEADER FILES
DECLARE CLASS
DEFINE MEMBER FUNCTIONS
DEFINE MAIN FUNCTION
Description:-
1. Include header files
In this section a programmer include all header files which are require to
execute given program. The most important file is iostream.h header file. This
file defines most of the C++statements like cout and cin. Without this file one
cannot load C++ program.
2. Declare Class
In this section a programmer declares all classes which are necessary for given
program. The programmer uses general syntax of creating class.
3. Define Member Functions
This section allows programmer to design member functions of a class. The
programmer can have inside declaration of a function or outside declaration of a
function.
4. Define Main Functions
This section the programmer creates object and call various functions writer
within various class.
Q.]With suitable example, describe effect of ++ and - - operators used with
pointer in pointer arithmetic.
++ Operator: - It is referred as increment operator that increments the value
of variable. If ++ operator is used with pointer variable, then pointer variable
points to next memory address that means pointer increment with respect to
size of the data type used to declare pointer variable.
Example:-
int a[5]={10,20,30,40,50},*ptr;
ptr=a[0];
for(i=0;i<5;i++)
{
cout<<*ptr;
ptr++;
}
In the above example, ptr points to memory location of a[0]. Increment
statement ptr++ increments ptr by memory size of int i.e 2 bytes and ptr
points to a[1].
- - Operator: - It is referred as decrement operator that decrementsthe value of
variable. If - - operator is used with pointer variable, then pointer variable
points to previous memory address that means pointer decrement with
respect to size of the data type used to declare pointer variable.
Example:-
int a[5]={10,20,30,40,50},*ptr;
ptr=a[4];
for(i=0;i<5;i++)
{
cout<<*ptr;
ptr- -;
}
In the above example, ptr points to memory location of a[4]. Decrement
statement ptr- - decrements ptr by memory size of int i.e 2 bytes and ptr
points to a[3].
Q.]Describe the concept of virtual base class with suitable example.
Virtual Base Class: An ancestor class is declared as virtual base class which is
used to avoid duplication of inherited members inside child class due to
multiple path of inheritance.

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();
} }

You might also like