CPP IMP Questions by Vishal Sir
CPP IMP Questions by Vishal Sir
CPP IMP Questions by Vishal Sir
Author:
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 1
VJTech Academy
- Example:
for(i=1;i<=5;i++)
{
cout<<”\nHello World”;
- Output
Hello World
Hello World
Hello World
Hello World
Hello World
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 2
VJTech Academy
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 3
VJTech Academy
4. State the use of scope resolution operator and its use in C++.
Ans:
1) It is used to identify a hidden variable. Scope resolution operator allows access to the global
version of a variable. The scope resolution operator is used to refer variable of class anywhere in
program.
:: Variable_name
2) Scope resolution operator is also used in classes to identify the class to which a member function
belongs. Scope resolution operator is used to define function outside of class.
Return_type class_name :: function_name( ) { }
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 4
VJTech Academy
6. Write a C++ program that replaces the string “Computer” in the String “Diploma in Computer
Engineering” with string “Information Technology”.
#include <iostream>
using namespace std;
int main()
{
string str1="Diploma in Computer Engineering";
string str2="Information Technology";
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int no,rev=0,rem;
cout<<"\nEnter any integer number:";
cin>>no;
while(no>0)
{
rem=no%10;
rev=rev*10+rem;
no=no/10;
}
cout<<"\nReverse Number="<<rev;
return 0;
}
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 5
VJTech Academy
strlen()
strcpy()
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 6
VJTech Academy
strrev()
strcat()
strcmp()
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 7
VJTech Academy
10. Explain working of insertion and extraction operators in C++ with the help of suitable
example.
Ans: Insertion operators: The operator << is called the put to operator. It inserts (or sends) the
contents of the variable on its right to the object on its left.
Example: cout<< string;
Extraction operators: The operator >> is known as extraction or get from operator. It extracts (or
takes) the value from the keyboard and assigns it to the variable on its right.
Example: cin>> number1;
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 8
VJTech Academy
#include <iostream>
using namespace std;
int main()
{
int i;
cout<<"\nFirst 10 Odd Numbers:";
for(i=1;i<20;i++)
{
if(i%2!=0)
{
cout<<"\t"<<i;
}
}
return 0;
}
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 9
VJTech Academy
14. Write a program to swap two integer values by using call by reference.
#include <iostream>
using namespace std;
void swap(int *x,int *y);
int main()
{
int a=100,b=200;
cout<<"\n***Before Swapping***";
cout<<"\na="<<a<<"\tb="<<b;
swap(&a,&b);
cout<<"\n***After Swapping***";
cout<<"\na="<<a<<"\tb="<<b;
return 0;
}
void swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 10
VJTech Academy
16. Give four differences between object-oriented programming and procedure oriented
programming.
Ans:
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 11
VJTech Academy
17. Explain scope resolution operator and memory management operator in C++.
Ans:
(i) Scope resolution operator:
1) It is used to identify a hidden variable. Scope resolution operator allows access to the global
version of a variable. The scope resolution operator is used to refer variable of class anywhere in
program.
:: Variable_name
2) Scope resolution operator is also used in classes to identify the class to which a member
function belongs. Scope resolution variable is used to define function outside of class.
Return_type class_name :: function_name( ) { }
(ii) Memory management operator:
There are two types of memory management operators in C++:
new
delete
These two memory management operators are used for allocating and freeing memory block in
efficient and convenient ways.
New operator: The new operator in C++ is used for dynamic storage allocation. This operator can
be used to create object of any type.
Example: int *p=new int;
Delete operator: The delete operator in C++ is used for releasing memory space when the object is
no longer needed. Once a new operator is used, it is efficient to use the corresponding delete
operator for release of memory
Example: delete p;
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 12
VJTech Academy
#include<iostream.h>
using namespace std;
int main()
{
int a[10],N,x,Loc,i;
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 13
VJTech Academy
#include <iostream>
using namespace std;
int main()
{
int a[10],N,i,largest;
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 14
VJTech Academy
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 15
VJTech Academy
21. List two memory management operators available in C++ and state its use in one line.
Ans: There are two types of memory management operators in C++:
new
delete
These two memory management operators are used for allocating and freeing memory block in
efficient and convenient ways.
New operator: The new operator in C++ is used for dynamic storage allocation. This operator can
be used to create object of any type. General syntax of new operator in C++ is as follows:
datatype pointer_variable = new datatype;
In the above statement, new is a keyword and the pointer variable is a variable of type datatype.
For example:
int *a=new int;
Delete operator: The delete operator in C++ is used for releasing memory space when the object is
no longer needed. Once a new operator is used, it is efficient to use the corresponding delete
operator for release of memory.
delete pointer_variable;
#include<iostream.h>
#include<conio.h>
void main()
{
//Allocates using new operator memory space in memory for storing a integer datatype
int *a= new int;
clrscr();
*a=100;
cout << " The Output is:a= " << *a;
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 16
VJTech Academy
22. Write a C++ program to add two 3 x 3 matrices and display addition
#include<iostream>
using namespace std;
int main()
{
int a[3][3],b[3][3],c[3][3],i,j;
cout<<"\nEnter first 3*3 matrix elements:";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>a[i][j];
}
}
cout<<"\nEnter second 3*3 matrix elements:";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>b[i][j];
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 17
OOP Important Questions
Author:
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 1
OOP Important Questions
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 2
OOP Important Questions
}
friend void calc_distance(Distance m,Distance n)
{
int sum;
sum=m.z+n.z;
cout<<"\nSum of distance="<<sum;
}
};
int main()
{
Distance d1,d2;
d1.getdata();
d2.getdata();
calc_distance(d1,d2);
return 0;
}
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 3
OOP Important Questions
2. Explain the concept of memory allocation for object/ explain how memory is allocated to
objects of a class.
Ans:
The memory space for object is allocated when object is declared & not when the class
is declared.
The member functions are created & placed in memory space only once when they are
defined as a part of a class definition.
Since all the objects belonging to that class use the same member functions, no
separate space is allocated for member functions.
When the objects are created only space for (data) member variables are allocated
separately for each object.
Separate memory locations for the objects are essential because the (data) member
variables will hold different data values for different objects
In the above diagram, member functions 1 and 2 are stored in the common memory
space as they require access by all objects.
Each object (object 1, object 2, object 3) has its own separate memory space for its
member variables.
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 4
OOP Important Questions
Program:
#include <iostream>
using namespace std;
class Addition
{
int a,b;
public:
Addition(int x,int y)
{
a=x;
b=y;
}
void display()
{
cout<<"Addition="<<a+b;
}
};
int main()
{
Addition a1(100,200);
a1.display();
return 0;
}
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 5
OOP Important Questions
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 6
OOP Important Questions
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 7
OOP Important Questions
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 8
OOP Important Questions
{
int sum;
sum=m.z+n.z;
cout<<"\nSum of distance="<<sum;
}
};
int main()
{
Distance d1,d2;
d1.getdata();
d2.getdata();
calc_distance(d1,d2);
return 0;
}
Ans:
In above given class data member B_name is declared as character array of 15
size then it will require 15 bytes of memory.
Data member B_id declared as integer then it required 2 bytes of memory
Data member Price declared as integer then it required 2 bytes of memory.
So total memory required for B1 object is 19 bytes.
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 9
OOP Important Questions
#include <iostream>
#include<string.h>
using namespace std;
class Train
{
private:
int train_no;
char train_name[20];
char source[20];
char destination[20];
char journey_date[10];
int capacity;
public:
Train()
{
train_no=0;
strcpy(train_name,"");
strcpy(source,"");
strcpy(destination,"");
strcpy(journey_date,"");
capacity=0;
}
void get_train_info()
{
cout<<"\nEnter Train No:";
cin>>train_no;
cout<<"\nEnter Train Name:";
cin>>train_name;
cout<<"\nEnter Train Source City:";
cin>>source;
cout<<"\nEnter Train Destination city:";
cin>>destination;
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 10
OOP Important Questions
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 11
OOP Important Questions
9. Write a C++ program to calculate sum of distance and display the results using friend function.
#include <iostream>
using namespace std;
class Distance
{
private:
int z;
public:
void getdata()
{
cout<<"\nEnter Distance:";
cin>>z;
}
friend void calc_distance(Distance m,Distance n)
{
int sum;
sum=m.z+n.z;
cout<<"\nSum of distance="<<sum;
}
};
int main()
{
Distance d1,d2;
d1.getdata();
d2.getdata();
calc_distance(d1,d2);
return 0;
}
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 12
OOP Important Questions
10. Write a C++ program to display number of objects created using static member.
#include <iostream>
using namespace std;
class Item
{
private:
static int count;
public:
Item()
{
count++;
cout<<"\nObject created successfully";
}
static void display()
{
cout<<"\nNo of Objects created="<<count;
}
};
int Item::count;
int main()
{
Item i1,i2,i3;
Item::display();
return 0;
}
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 13
OOP Important Questions
Member functions:
- Initialize members
- Deposit Amount
- Withdraw Amount
- Display Balance
Write a C++ program to test the Bank Account class for 10 customers.
#include <iostream>
using namespace std;
class Bank
{
private:
int acc_no;
char cust_name[20];
char acc_type[10];
float available_amt;
public:
Bank()
{
cout<<"\nEnter Account Number:";
cin>>acc_no;
cout<<"\nEnter Customer Name:";
cin>>cust_name;
cout<<"\nEnter Account Type:";
cin>>acc_type;
cout<<"\nEnter account opening balance:";
cin>>available_amt;
}
void deposit_amount()
{
int deposit_amt;
cout<<"\nHow much amount do you wants to deposit:";
cin>>deposit_amt;
available_amt=available_amt+deposit_amt;
cout<<"\nAmount deposit successfully";
}
void withdraw_amount()
{
int withdraw_amt;
cout<<"\nHow much amount do you wants to withdraw:";
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 14
OOP Important Questions
cin>>withdraw_amt;
available_amt=available_amt-withdraw_amt;
cout<<"\nAmount withdraw successfully";
}
void display_acc_info()
{
cout<<"\n****BANK ACCOUNT DETAILS****";
cout<<"\nAccount No:"<<acc_no;
cout<<"\nCustomer Name:"<<cust_name;
cout<<"\nAccount Type:"<<acc_type;
cout<<"\nAvailable Balance:"<<available_amt;
}
};
int main()
{
Bank b[10];
int i;
for(i=0;i<10;i++)
{
b[i].deposit_amount();
b[i].withdraw_amount();
b[i].display_acc_info();
}
return 0;
}
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 15
OOP Important Questions
12. Fix the compilation errors and find the output of following program
#include class Test
{
private: int x;
public:
Test(int x = 0)
{
this->x = x;
}
void change(Test *t)
{
this = t;
}
void print()
{
cout << "x = " << x << endl;
}
};
void main()
{
Test obj(5);
Test *ptr = new Test (10);
obj.change(ptr);
obj.print();
}
Ans:
- Missing iostream.h header file so include header file iostream.h in this program.
- Pointer object t assigning on normal object obj (this=t) which is incorrect assignment.
- The statement this=t is incorrect so we have modified it to this->x=t->x.
- After fixing all above errors, output of above program is x=10;
13. State the use of static data member of a class. List properties of static member function.
Characteristics of static data members:
1. It is initialized to zero when the first object of its class is created. No other initialization is
permitted.
2. Only one copy of that member is created for the entire class.
3. Created copy is shared by all the objects of that class, no matter how many objects are
created.
4. It is visible only within the class, but its lifetime is the entire program.
- Since objects are created anywhere in a program, and all objects shares the value of static
member it is necessary to make static members global and re-declared outside of the class.
====================================================================
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 16
OOP Important Questions
14. Describe various places at which member functions can define using suitable example.
1) Member function definition inside the class.
2) Member function definition outside the class.
Note: take help from our class notebook
15. Define a structure that represents Fruit with properties fruit name, fruit type, fruit color.
Write a program that accepts data of four fruits and displays the results.
#include <iostream>
#include<string.h>
using namespace std;
struct Fruit
{
char fruit_name[20];
char fruit_type[20];
char fruit_color[20];
};
int main()
{
struct Fruit f[4];
int i;
for(i=0;i<4;i++)
{
cout<<"\nEnter Fruit Name:";
cin>>f[i].fruit_name;
cout<<"\nEnter Fruit Type:";
cin>>f[i].fruit_type;
cout<<"\nEnter Fruit Color:";
cin>>f[i].fruit_color;
}
for(i=0;i<4;i++)
{
cout<<"\nFruit Name:"<<f[i].fruit_name;
cout<<"\nFruit Type:"<<f[i].fruit_type;
cout<<"\nFruit Color:"<<f[i].fruit_color;
}
return 0;
}
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 17
OOP Important Questions
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 18
OOP Important Questions
17. State any one use of this pointer with suitable example.
“this‟ pointer is used to represent an object that invokes a member function.
It points to the object for which the function is called.
It is also used to access members of object inside function definition.
Example: this->rollno=1;
Normally, we are not using this keyword regularly because it makes code complex.
“this” pointer always keeps track of current calling object of that member function.
Program:
#include <iostream>
using namespace std;
class Addition
{
private:
int a,b;
public:
void getdata()
{
this->a=100;
this->b=200;
}
void putdata()
{
cout<<"\nAddition="<<(this->a+this->b);
}
};
int main()
{
Addition a1;
a1.getdata();
a1.putdata();
return 0;
}
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 19
OOP Important Questions
18. Correct the syntactical and logical errors in the following and explain program:
Ans:
1) T is a normal object but while calling getdata() and display() (->) operator is used instead of
dot operator(.). This is the mistake.
2) p is pointer object but while calling getdata() used dot operator instead of (->).This is the
mistake.
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 20
OOP Important Questions
21. Write a program to declare class Time having data member as hrs, mins, secs. Write
constructor to assign values and destructor to destroy values. Accept & display data for one
object.
#include <iostream>
using namespace std;
class Time
{
private:
int hrs,mins,secs;
public:
Time()
{
cout<<"\nEnter Hours:";
cin>>hrs;
cout<<"\nEnter Minues:";
cin>>mins;
cout<<"\nEnter Seconds:";
cin>>secs;
}
void display()
{
cout<<"\nTime="<<hrs<<":"<<mins<<":"<<secs;
}
~Time()
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 21
OOP Important Questions
{
cout<<"\nObject destroyed successfully";
}
};
int main()
{
Time t1;
t1.display();
return 0;
}
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 22
OOP Important Questions
23. Write a program to declare class student having data members name and percentage. Write
constructor to initialize these data members. Accept and display this data for one object.
#include <iostream>
using namespace std;
class Student
{
private:
char name[20];
float percentage;
public:
Student()
{
cout<<"\nEnter Name of student:";
cin>>name;
cout<<"\nEnter Student Percentage:";
cin>>percentage;
}
void display()
{
cout<<"\nStudent Name:"<<name;
cout<<"\nStudent percentage:"<<percentage;
}
};
int main()
{
Student s1;
s1.display();
return 0;
}
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 23
OOP Important Questions
s1.getdata();
s2.getdata();
s1.display(s2);
return 0;
}
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 24
OOP Important Questions
25. What do you mean by default argument? Illustrate concept of constructor with default
argument using suitable example.
#include <iostream>
using namespace std;
class Addition
{
private:
int a,b;
public:
Addition(int x,int y=0)
{
a=x;
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 25
OOP Important Questions
b=y;
}
void display()
{
cout<<"Addition="<<(a+b);
}
};
int main()
{
Addition a1(100);
a1.display();
return 0;
}
26. What are the rules governing the declaration of a destructor member function?
Ans:
Rules for declaration of destructor member function:
1. Destructor name is same as class name but is preceded by a tilde(~).
2. Destructor is declared in public area of a class.
3. Destructor never takes any argument.
4. Destructor never returns any value.
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 26
OOP Important Questions
27. Write a program to declare class Account having data member as acc_no and balance. Accept
and display data for five object using pointer to array of object.
#include<iostream>
using namespace std;
class Bank
{
private:
int acc_no;
char name[20];
float balance;
public:
void get_bank_info()
{
cout<<"\nEnter Account Number:";
cin>>acc_no;
cout<<"\nEnter Customer Name:";
cin>>name;
cout<<"\nEnter Account Balance:";
cin>>balance;
}
void disp_bank_info()
{
cout<<"\n"<<acc_no<<"\t"<<name<<"\t"<<balance;
}
};
int main()
{
Bank *b[5];
int i;
for(i=0;i<5;i++)
{
b[i]->get_bank_info();
}
cout<<"\n************************************";
cout<<"\n*********VJTECH BANK***************";
cout<<"\n************************************";
cout<<"\nACCNO\tNAME\tBALANCE";
cout<<"\n===================================";
for(i=0;i<5;i++)
{
b[i]->disp_bank_info();
}
return 0;
}
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 27
OOP Important Questions
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 28
OOP Important Questions
29. What is copy constructor? Give the syntax and example for copy constructor.
Ans:
Program:
#include <iostream>
using namespace std;
class Code
{
private:
int id;
public:
Code() //default Constructor
{
id=100;
}
Code(Code &m) //Copy Constructor
{
id=m.id;
}
void display()
{
cout<<"\nValue of ID="<<id;
}
};
int main()
{
Code c1;
Code c2(c1);
c1.display();
c2.display();
return 0;
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 29
OOP Important Questions
30. What do you mean by inline function? Write its syntax and example.
To eliminate the cost of calls to small functions, c++ proposes a new feature called inline
function.
An inline function is a function that is expanded in line when it is invoked. That is the
compiler replaces the function call with the corresponding function code.
If we make a function as inline, then the compiler replaces the calling function location with
the definition of the inline function at compile time.
Any changes made to an inline function will require the inline function to be recompiled
again.
Because the compiler would need to replace all the code with a new code, otherwise, it will
execute the old code.
The main use of the inline function in C++ is to save memory space.
When we call the function then it takes some time to jump over function definition and also
It consume some memory for function call. But it will save time and memory due to inline
function.
Syntax:
-------
inline return_type function_name(parameters)
{
// function code?
}
Example:
#include<iostream>
using namespace std;
class Addition
{
private:
int a,b;
public:
inline void getdata()
{
a=100;
b=200;
}
inline void putdata()
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 30
OOP Important Questions
{
cout<<"\nAddition="<<(a+b);
}
};
int main()
{
Addition a1;
a1.getdata();
a1.putdata();
return 0;
}
31. Write a program to declare class ‘city’ with data member’s cityname and state. Create array of
object of size 5. Read and print data for array using pointer to object.
#include<iostream>
using namespace std;
class City
{
private:
char city_name[20];
char state_name[20];
public:
void get_city_info()
{
cout<<"\nEnter city name:";
cin>>city_name;
cout<<"\nEnter state name:";
cin>>state_name;
}
void disp_city_info()
{
cout<<"\n"<<city_name<<"\t"<<state_name;
}
};
int main()
{
City *c[5];
int i;
for(i=0;i<5;i++)
{
c[i]->get_city_info();
}
cout<<"\nCITYNAME\tSTATENAME";
cout<<"\n=====================";
for(i=0;i<5;i++)
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 31
OOP Important Questions
{
c[i]->disp_city_info();
}
return 0;
}
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 32
OOP Important Questions
33. Write a program to declare class ‘staff’ having data members as name and post. Accept this
data for 5 staffs and display name of staff who are HOD.
#include <iostream>
#include <string.h>
using namespace std;
class staff
{
private:
char name[20];
char post[20];
public:
void get_staff_info()
{
cout<<"\nEnter Staff Name:";
cin>>name;
cout<<"\nEnter Staff Post:";
cin>>post;
}
void disp_staff_info()
{
if(strcmp(post,"HOD")==0)
{
cout<<"\n"<<name<<"\t"<<post;
}
}
};
int main()
{
staff s[5];
int i;
for(i=0;i<5;i++)
{
s[i].get_staff_info();
}
cout<<"\nNAME\tPOST";
cout<<"\n===================";
for(i=0;i<5;i++)
{
s[i].disp_staff_info();
}
}
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 33
OOP Important Questions
34. Write a program to define a structure ‘Tender’ having data members’ tender-no., cost and
company-name. Accept & display this data for two variables of this structure.
#include <iostream>
using namespace std;
struct Tender
{
int tender_no;
float tender_cost;
char company_name[20];
};
int main()
{
struct Tender t[2];
int i;
for(i=0;i<2;i++)
{
cout<<"\nEnter Tender Number:";
cin>>t[i].tender_no;
cout<<"\nEnter Tender Cost:";
cin>>t[i].tender_cost;
cout<<"\nEnter Tender Company Name:";
cin>>t[i].company_name;
}
for(i=0;i<2;i++)
{
cout<<"\nTender No:"<<t[i].tender_no;
cout<<"\nTender Cost:"<<t[i].tender_cost;
cout<<"\nCompany Name:"<<t[i].company_name;
}
return 0;
}
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 34
OOP Important Questions
35. Write a program to declare a class ‘Journal’ having data members as journal-name, price & no-
of-pages. Accept this data for two objects & display the name of journal having greater price.
#include <iostream>
using namespace std;
class Journal
{
public:
char journal_name[20];
float price;
int no_of_pages;
void get_Journal_details()
{
cout<<"\nEnter Journal Name:";
cin>>journal_name;
cout<<"\nEnter journal price:";
cin>>price;
cout<<"\nEnter no of pages:";
cin>>no_of_pages;
}
void disp_journal_details()
{
cout<<"\n****Journal Information****";
cout<<"\nJournal Name:"<<journal_name;
cout<<"\nJournal Price:"<<price;
cout<<"\nNo of Pages:"<<no_of_pages;
}
};
int main()
{
Journal j1,j2;
j1.get_Journal_details();
j2.get_Journal_details();
if(j1.price>j2.price)
{
j1.disp_journal_details();
}
else
{
j2.disp_journal_details();
}
return 0;
}
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 35
OOP Important Questions
36. Write a program to declare a class birthday having data members day, month, year. Accept
this information for five objects using pointer to the array of objects.
#include <iostream>
using namespace std;
class Birthday
{
private:
int day,month,year;
public:
void get_birthday_info()
{
cout<<"\nEnter Day:";
cin>>day;
cout<<"\nEnter Month:";
cin>>month;
cout<<"\nEnter Year:";
cin>>year;
}
void disp_birthday_info()
{
cout<<"\nBirth date:"<<day<<"/"<<month<<"/"<<year;
}
};
int main()
{
Birthday *b[5];
int i;
for(i=0;i<5;i++)
{
b[i]->get_birthday_info();
}
for(i=0;i<5;i++)
{
b[i]->disp_birthday_info();
}
return 0;
}
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 36
OOP Important Questions
37. Write a C++ program to find smallest number from two numbers using friend function.
#include <iostream>
using namespace std;
class Number
{
private:
int a;
public:
void getdata()
{
cout<<"\nEnter any number:";
cin>>a;
}
friend void Calc_Smallest(Number m,Number n)
{
if(m.a<n.a)
{
cout<<"\nSmallest Number="<<m.a;
}
else
{
cout<<"\nSmallest Number="<<n.a;
}
}
};
int main()
{
Number n1,n2;
n1.getdata();
n2.getdata();
Calc_Smallest(n1,n2);
return 0;
}
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 37
OOP Important Questions
Author:
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 1
OOP Important Questions
1. Write a program to implement single inheritance. Declare base class ‘Employee’ with emp_no
and emp_name. Declare derived class ‘Fitness’ with height and weight. Accept and display
data for one employee.
#include <iostream>
using namespace std;
class Employee
{
public:
int emp_no;
char emp_name[20];
void get_emp_info()
{
cout<<"\nEnter Employee No:";
cin>>emp_no;
cout<<"\nEnter Employee Name:";
cin>>emp_name;
}
void disp_emp_info()
{
cout<<"\nEmployee No:"<<emp_no;
cout<<"\nEmployee Name:"<<emp_name;
}
};
class Fitness:public Employee
{
public:
int height,weight;
void get_fitness_info()
{
cout<<"\nEnter Employee Height:";
cin>>height;
cout<<"\nEnter Employee Weight:";
cin>>weight;
}
void disp_fitness_info()
{
cout<<"\nEmployee Height:"<<height;
cout<<"\nEmployee Weight:"<<weight;
}
};
int main()
{
Fitness f1;
f1.get_emp_info();
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 2
OOP Important Questions
f1.get_fitness_info();
f1.disp_emp_info();
f1.disp_fitness_info();
return 0;
}
OUTPUT:
Enter Employee No:1010
Enter Employee Name:Dennis
Enter Employee Height:5
Enter Employee Weight:26
Employee No:1010
Employee Name:Dennis
Employee Height:5
Employee Weight:26
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 3
OOP Important Questions
2. Define classes to appropriately represent class hierarchy as shown in above figure. Use
constructors for both classes and display Salary for a particular employee.
#include <iostream>
using namespace std;
class Employee
{
public:
int emp_no;
char emp_name[20];
Employee()
{
cout<<"\nEnter Employee No:";
cin>>emp_no;
cout<<"\nEnter Employee Name:";
cin>>emp_name;
}
void disp_emp_info()
{
cout<<"\nEmployee No:"<<emp_no;
cout<<"\nEmployee Name:"<<emp_name;
}
};
class Salary:private Employee
{
public:
int basic_pay,HRA,DA,CLA;
Salary():Employee()
{
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 4
OOP Important Questions
cin>>basic_pay;
cout<<"\nEnter HRA:";
cin>>HRA;
cout<<"\nEnter DA:";
cin>>DA;
cout<<"\nEnter CLA:";
cin>>CLA;
}
void Calculate_Salary()
{
disp_emp_info();
int gross_salary;
gross_salary=(basic_pay+HRA+DA+CLA);
cout<<"\nBasic Salary:"<<basic_pay;
cout<<"\nHRA:"<<HRA;
cout<<"\nDA:"<<DA;
cout<<"\nCLA:"<<CLA;
cout<<"\nGross Salary:"<<gross_salary;
}
};
int main()
{
Salary s1;
s1.Calculate_Salary();
return 0;
}
OUTPUT
Enter Employee No:1010
Enter Employee Name:James
Enter basic pay:12000
Enter HRA:1200
Enter DA:500
Enter CLA:800
Employee No:1010
Employee Name:James
Basic Salary:12000
HRA:1200
DA:500
CLA:800
Gross Salary:14500
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 5
OOP Important Questions
#include <iostream>
using namespace std;
class Staff
{
public:
int code;
void get_staff_info()
{
cout<<"\nEnter Staff Code:";
cin>>code;
}
void disp_staff_info()
{
cout<<"\nStaff Code:"<<code;
}
};
class Teacher:public Staff
{
public:
char subject[20];
void get_teacher_info()
{
cout<<"\nEnter Teacher Subject:";
cin>>subject;
}
void disp_teacher_info()
{
cout<<"\nTeacher Subject:"<<subject;
}
};
class Officer:public Staff
{
public:
char grade;
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 6
OOP Important Questions
void get_officer_info()
{
cout<<"\nEnter Officer Grade:";
cin>>grade;
}
void disp_officer_info()
{
cout<<"\nofficer Grade:"<<grade;
}
};
int main()
{
cout<<"\n****Teacher Class****";
Teacher t1;
t1.get_staff_info();
t1.get_teacher_info();
t1.disp_staff_info();
t1.disp_teacher_info();
cout<<"\n****Officer Class****";
Officer s1;
s1.get_staff_info();
s1.get_officer_info();
s1.disp_staff_info();
s1.disp_officer_info();
return 0;
}
OUTPUT:
****Teacher Class****
Enter Staff Code:1010
Enter Teacher Subject:C++
Staff Code:1010
Teacher Subject:C++
****Officer Class****
Enter Staff Code:2020
Enter Officer Grade:A
Staff Code:2020
officer Grade:A
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 7
OOP Important Questions
#include <iostream>
using namespace std;
class Student
{
protected:
int rollno;
char name[20];
public:
void get_stud_info()
{
cout<<"\nEnter Student rollno:";
cin>>rollno;
cout<<"\nEnter Student Name:";
cin>>name;
}
void disp_stud_info()
{
cout<<"\nStudent RollNo:"<<rollno;
cout<<"\nStudent Name:"<<name;
}
};
class Test:public Student
{
protected:
int marks1,marks2;
public:
void get_marks()
{
cout<<"\nEnter Class Test-1 Marks:";
cin>>marks1;
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 8
OOP Important Questions
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 9
OOP Important Questions
OUTPUT:
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 10
OOP Important Questions
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 11
OOP Important Questions
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 12
OOP Important Questions
#include <iostream>
using namespace std;
class A
{
public:
void display_A()
{
cout<<"\nDisplay Method of class A";
}
};
class B:virtual public A
{
public:
void display_B()
{
cout<<"\nDisplay Method of class B";
}
};
class C:virtual public A
{
public:
void display_C()
{
cout<<"\nDisplay Method of class C";
}
};
class D:public B,public C
{
public:
void display_D()
{
cout<<"\nDisplay Method of class D";
}
};
int main()
{
D d1;
d1.display_A();
d1.display_B();
d1.display_C();
d1.display_D();
return 0;
}
OUTPUT
Display Method of class A
Display Method of class B
Display Method of class C
Display Method of class D
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 13
OOP Important Questions
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 14
OOP Important Questions
Program:
#include<iostream>
using namespace std;
class Alpha
{
private:
int x;
public:
Alpha(int i)
{
x=i;
}
void show_x()
{
cout<<"\nValue of x="<<x;
}
};
class Beta
{
private:
int y;
public:
Beta(int j)
{
y=j;
}
void show_y()
{
cout<<"\nValue of y="<<y;
}
};
class Gamma:public Alpha,public Beta
{
private:
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 15
OOP Important Questions
int z;
public:
Gamma(int m,int n,int p):Alpha(m),Beta(n)
{
z=p;
}
void show_z()
{
cout<<"\nValue of z="<<z;
}
};
int main()
{
Gamma g1(100,200,300);
g1.show_x();
g1.show_y();
g1.show_z();
return 0;
}
OUTPUT
Value of x=100
Value of y=200
Value of z=300
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 16
OOP Important Questions
Program
#include <iostream>
using namespace std;
class Student
{
protected:
int rollno;
char name[20];
public:
void get_stud_info()
{
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 17
OOP Important Questions
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 18
OOP Important Questions
public:
void disp_total_marks()
{
total=marks1+marks2;
cout<<"\nTotal Marks:"<<total;
}
};
int main()
{
Result r1;
r1.get_stud_info();
r1.get_marks();
r1.get_sport_info();
r1.disp_stud_info();
r1.disp_marks();
r1.disp_total_marks();
r1.disp_sport_info();
return 0;
}
OUTPUT:
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 19
OOP Important Questions
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 20
OOP Important Questions
Program
#include <iostream>
using namespace std;
class Student
{
protected:
int rollno;
char name[20];
public:
void get_stud_info()
{
cout<<"\nEnter Student roll no:";
cin>>rollno;
cout<<"\nEnter Student Name:";
cin>>name;
}
void disp_stud_info()
{
cout<<"\nStudent Roll NO:"<<rollno;
cout<<"\nStudent Name:"<<name;
}
};
class Test:public Student
{
protected:
int marks1,marks2;
public:
void get_marks()
{
cout<<"\nEnter Class Test-1 Marks:";
cin>>marks1;
cout<<"\nEnter Class Test-2 Marks:";
cin>>marks2;
}
void disp_marks()
{
cout<<"\nClass Test-1 Marks:"<<marks1;
cout<<"\nClass Test-2 Marks:"<<marks2;
}
};
class Result:public Test
{
protected:
int total;
public:
void disp_total_marks()
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 21
OOP Important Questions
{
total=marks1+marks2;
cout<<"\nTotal Marks:"<<total;
}
};
int main()
{
Result r1;
r1.get_stud_info();
r1.get_marks();
r1.disp_stud_info();
r1.disp_marks();
r1.disp_total_marks();
return 0;
}
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 22
OOP Important Questions
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 23
OOP Important Questions
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 24
OOP Important Questions
Program
#include <iostream>
using namespace std;
class Student
{
protected:
int rollno;
char name[20];
public:
void get_stud_info()
{
cout<<"\nEnter Student roll no:";
cin>>rollno;
cout<<"\nEnter Student Name:";
cin>>name;
}
void disp_stud_info()
{
cout<<"\nStudent Roll NO:"<<rollno;
cout<<"\nStudent Name:"<<name;
}
};
class Test
{
protected:
int marks1,marks2;
public:
void get_marks()
{
cout<<"\nEnter Class Test-1 Marks:";
cin>>marks1;
cout<<"\nEnter Class Test-2 Marks:";
cin>>marks2;
}
void disp_marks()
{
cout<<"\nClass Test-1 Marks:"<<marks1;
cout<<"\nClass Test-2 Marks:"<<marks2;
}
};
class Result:public Student,public Test
{
protected:
int total;
public:
void disp_total_marks()
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 25
OOP Important Questions
{
total=marks1+marks2;
cout<<"\nTotal Marks:"<<total;
}
};
int main()
{
Result r1;
r1.get_stud_info();
r1.get_marks();
r1.disp_stud_info();
r1.disp_marks();
r1.disp_total_marks();
return 0;
}
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 26
OOP Important Questions
Author:
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 1
OOP Important Questions
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 2
OOP Important Questions
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 3
OOP Important Questions
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 4
OOP Important Questions
#include <iostream>
using namespace std;
int main()
{
char str[10],*p,key;
int flag=0;
cout<<"\nEnter String:";
cin>>str;
cout<<"\nEnter character for searching:";
cin>>key;
p=&str[0];
while(*p!='\0')
{
if(key==*p)
{
flag=1;
break;
}
p++;
}
if(flag==1)
cout<<"\nCharacter is found!!!";
else
cout<<"\nCharacter is not found!!!";
return 0;
}
OUTPUT
Enter String:VJTech
Enter character for searching:k
Character is not found!!!
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 5
OOP Important Questions
9. Write a C++ program to add two complex numbers overloading “+” operator.
#include <iostream>
using namespace std;
class Complex
{
private:
int real,imag;
public:
void getdata(int r,int i)
{
real = r;
imag = i;
}
void display()
{
cout<<real<<"+i"<<imag;
}
Complex operator +(Complex m)
{
Complex temp;
temp.real = real + m.real;
temp.imag = imag + m.imag;
return temp;
}
};
int main()
{
Complex c1,c2,c3;
c1.getdata(10,-20);
c2.getdata(2,4);
c3=c1+c2;
cout<<"\nObject c1:";
c1.display();
cout<<"\nObject c2:";
c2.display();
cout<<"\nObject c3:";
c3.display();
return 0;
}
OUTPUT
Object c1:10+i-20
Object c2:2+i4
Object c3:12+i-16
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 6
OOP Important Questions
10. State any four points of differentiation between compile time polymorphism and run time
polymorphism
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 7
OOP Important Questions
11. Write a C++ program to overload area() function to calculate area of shapes like triangle
,square, circle.
#include<iostream>
using namespace std;
void area(int s);
void area(float r);
void area(float base,float height);
int main()
{
int side;
float radius,bs,ht;
cout<<"Enter side of a square:";
cin>>side;
cout<<"Enter radius of circle:";
cin>>radius;
cout<<"Enter base and height of triangle:";
cin>>bs>>ht;
area(side);
area(radius);
area(bs,ht);
return 0;
}
void area(int s)
{
cout<<"\nArea of square="<<(s*s);
}
void area(float r)
{
cout<<"\nArea of Circle="<<(3.14*r*r);
}
void area(float base,float height)
{
cout<<"\nArea of triangle="<<((base*height)/2);
}
OUTPUT
Enter side of a square:2
Enter radius of circle:3
Enter base and height of triangle:12 9
Area of square=4
Area of Circle=28.26
Area of triangle=54
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 8
OOP Important Questions
#include <iostream>
using namespace std;
class Base
{
public:
void display()
{
cout<<"\ndisplay method of Base class";
}
};
class Derived:public Base
{
public:
void display()
{
cout<<"\ndisplay method of Derived class";
}
};
int main()
{
Derived d1;
d1.display();
return 0;
}
- In the above example, base class and derived class both contains a same name function
“display”. The derived class overrides the “display‟ function of base class.
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 9
OOP Important Questions
13. Write a program for overloading of ++ unary operator for inch to feet conversion. 12 inches =
1 feet.
#include <iostream>
using namespace std;
class UnayOPOverload
{
private:
int inch;
public:
void getdata()
{
cout<<"\nEnter value of inch:";
cin>>inch;
}
void operator ++()
{
float feet=(float)inch/12;
cout<<"\nInches="<<inch;
cout<<"\nFeet="<<feet;
}
};
int main()
{
UnayOPOverload v1;
v1.getdata();
++v1;
return 0;
}
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 10
OOP Important Questions
14. Write a program to copy content of one string to another string using pointer to string.
#include <iostream>
using namespace std;
int main()
{
char str1[10],str2[10],*p1,*p2;
cout<<"\nEnter Any String:";
cin>>str1;
p1=&str1[0];
p2=&str2[0];
while(*p1!='\0')
{
*p2=*p1;
p1++;
p2++;
}
*p2='\0';
cout<<"\nCopied String="<<str2;
return 0;
}
OUTPUT:
Enter Any String:Pune
Copied String=Pune
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 11
OOP Important Questions
#include <iostream>
using namespace std;
int main()
{
char str1[10],str2[10],*p1,*p2;
int len=0;
cout<<"\nEnter any string:";
cin>>str1;
p1=&str1[0];
p2=&str2[0];
while(*p1!='\0')
{
len++;
p1++;
}
p1--;
while(len>0)
{
*p2=*p1;
p1--;
p2++;
len--;
}
*p2='\0';
cout<<"\nReverse String : "<<str2;
return 0;
}
OUTPUT
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 12
OOP Important Questions
#include <iostream>
using namespace std;
int main()
{
int a=120,*p;
p=&a;
cout<<"\nAddress stored in p="<<p; //p=1000
p=p+2;
cout<<"\nAddress stored in p="<<p; //p=1004
p=p-3;
cout<<"\nAddress stored in p="<<p; //p=998
p++;
cout<<"\nAddress stored in p="<<p; //p=1000
p--;
cout<<"\nAddress stored in p="<<p; //p=998
return 0;
}
OUTPUT:
Address stored in p=1000
Address stored in p=1004
Address stored in p=998
Address stored in p=1000
Address stored in p=998
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 13
OOP Important Questions
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 14
OOP Important Questions
#include <iostream>
using namespace std;
class Base
{
public:
virtual void display()
{
cout<<"\ndisplay method of base class";
}
};
class Derived:public Base
{
public:
void display()
{
cout<<"\ndisplay method of derived class";
}
};
int main()
{
Base *bptr;
Base b1;
Derived d1;
cout<<"\nBase class pointer points to Base class";
bptr=&b1;
bptr->display();
cout<<"\nBase class pointer points to Derived class";
bptr=&d1;
bptr->display();
return 0;
}
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 15
OOP Important Questions
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 16
OOP Important Questions
#include <iostream>
using namespace std;
class Base
{
public:
virtual void display()=0;
};
class Derived:public Base
{
public:
void display()
{
cout<<"\ndisplay method of derived class";
}
};
int main()
{
Base *bptr;
Derived d1;
bptr=&d1;
bptr->display();
return 0;
}
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 17
OOP Important Questions
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 18
OOP Important Questions
21. Write a program to overload the ‘–’ operator to negate value of variable.
#include<iostream>
using namespace std;
class Space
{
private:
int x,y,z;
public:
void getdata(int m,int n,int p)
{
x=m;
y=n;
z=p;
}
void display()
{
cout<<"\nx="<<x<<"\ty="<<y<<"\tz="<<z;
}
void operator -()
{
x=-x;
y=-y;
z=-z;
}
};
int main()
{
Space s;
s.getdata(10,-20,30);
cout<<"\n***Before calling Operator overloaded function***";
s.display();
-s; //calling operator overloaded function
cout<<"\n***After calling Operator overloaded function***";
s.display();
return 0;
}
OUTPUT:
***Before calling Operator overloaded function***
x=10 y=-20 z=30
***After calling Operator overloaded function***
x=-10 y=20 z=-30
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 19
OOP Important Questions
22. Write a program to overload ‘– –‘ unary operator to decrement the data members of object by
one. Assume suitable data.
#include <iostream>
using namespace std;
class UnayOPOverload
{
private:
int a,b;
public:
void getdata(int x,int y)
{
a=x;
b=y;
}
void display()
{
cout<<"\nValue of a="<<a;
cout<<"\nValue of b="<<b;
}
void operator --()
{
a--;
b--;
}
};
int main()
{
UnayOPOverload v1;
v1.getdata(100,200);
cout<<"\n***Before calling Operator Overload function***";
v1.display();
--v1;
cout<<"\n***After calling Operator Overload function***";
v1.display();
return 0;
}
OUTPUT
***Before calling Operator Overload function***
Value of a=100
Value of b=200
***After calling Operator Overload function***
Value of a=99
Value of b=199
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 20
OOP Important Questions
ptr=&a[0];
for(i=0;i<5;i++)
{
if(key==*ptr)
{
flag=1;
break;
}
ptr++;
}
if(flag==1)
{
cout<<"\nElement is Found!!!";
}
else
{
cout<<"\nElement is not Found!!!";
}
return 0;
}
OUTPUT
Enter Five Array Elements:10 20 30 40 50
Enter key element for searching:40
Element is Found!!!
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 21
OOP Important Questions
#include <iostream>
using namespace std;
int main()
{
char str[10],*ptr;
int len=0;
cout<<"\nEnter Any String:";
cin>>str;
ptr=&str[0];
while(*ptr!='\0')
{
len++;
ptr++;
}
cout<<"\nLength of string="<<len;
return 0;
}
OUTPUT
Enter Any String:Latur
Length of string=5
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 22
OOP Important Questions
Author:
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 1
OOP Important Questions
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 2
OOP Important Questions
while(!fin.eof())
{
fin.get(ch);
fout<<ch;
}
cout<<"\nContents of ABC.txt file copied into XYZ.txt file successfully";
fin.close();
fout.close();
getch();
}
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 3
OOP Important Questions
4. Write a program that will create data file containing the list of telephone numbers as:
John 34567
Hari 56788
……
Use a class object to store each set of data.
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
class Telephone
{
private:
char cust_name[20];
char mobile_no[10];
public:
void getdata()
{
cout<<"\nEnter Customer Name:";
cin>>cust_name;
cout<<"\nEnter Mobile No:";
cin>>mobile_no;
}
void putdata()
{
ofstream fout;
fout.open("telphone.txt",ios::app);
fout<<"\n"<<cust_name<<"\t"<<mobile_no;
cout<<"\nNew customer entry added";
}
};
void main()
{
Telephone t1;
clrscr();
t1.getdata();
t1.putdata();
getch();
}
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 4
OOP Important Questions
5. Give syntax of and explain various functions related to ifstream and ofstream classes: seekp(),
getline(),
Seekp
The seekp method of ofstream in C++ is used to set the position of the file pointer in
the output sequence with the specified position.
This method takes the new position to be set.
Syntax: seekp(position);
getline:
The cin is an object which is used to take input from the user but does not allow to take
the input in multiple lines.
To accept the multiple lines input, we use the getline() function.
It is a pre-defined function defined in a <string.h> header file used to accept a line or a
string from the input stream until the delimiting character is encountered.
Syntax of getline() function:
There are two ways of representing a function:
The first way of declaring is to pass three parameters.
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 5
OOP Important Questions
2)Seekp() Function:
3)tellg() Function:
4)tellp() Function:
OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 6