Object Oriented Programming - Unit Wise Questions
1. What is object oriented programming? Explain objects, class, encapsulation, data hiding, inheritance, and polymorphism.
AI is thinking...
1. Explain in detail the following principles of Object-Oriented Programming.
i. Data encapsulation and data hiding. ii. Inheritance and polymorphism. iii. Abstraction
AI is thinking...
1. Discuss the feature of the Object-Oriented Programming. Differentiate between Object Oriented Programming and Procedural Oriented Programming.
AI is thinking...
1. What are the main features of the Object-Oriented Programming? Explain with suitable practical examples.
AI is thinking...
1. Why do we need object oriented programming? How can we use inheritance to reuse already written and tested code in programs? Discuss with suitable example. (3+3+4)
AI is thinking...
1. Differentiate between structural programming approach and object oriented programming approach. Explain the inheritance, polymorphism with example.
AI is thinking...
1. Explain the object oriented programming with its advantages. What are the features of object oriented languages? Explain.
AI is thinking...
1. Discuss the feature of Object-Oriented Programming. Differentiate between Object Oriented Programming and any other programming language that you know.
AI is thinking...
1. What is object-oriented approach? How is it different from structured programming approach? Discuss the features of object-oriented languages in detail.
AI is thinking...
1. Write down the features of object oriented programming language and explain.
AI is thinking...
1. Write any four features of object-oriented programming. Differentiate between operator overloading and function overloading.
AI is thinking...
4. Explain abstraction with example.
AI is thinking...
4. What is structured programming? Discuss characteristics and problems associated with structured programming.
AI is thinking...
4. How object oriented programming differs from object based programming language? Discuss benefits of OOP.
AI is thinking...
11. How can we define our functions inside the namespace and use them outside?
AI is thinking...
2. How can you convert the user defined data type into primitive data type and vice versa? Explain both conversion routine with suitable example.
AI is thinking...
2. Why do we need the preprocessor directive # include < io stream >? Describe the major parts of a C++ program.
AI is thinking...
3. What do you mean by overloading of a function? When do we use this concept? Explain with example.
AI is thinking...
3. What is function overloading? How is it different from function overriding? Write a program that gives an example of function overriding.
AI is thinking...
3. Why data conversion is needed? Write a program to convert kilogram into gram using user define to user define data conversion.(1 kg = 1000 gm).
AI is thinking...
4. Explain the purpose of a namespace with suitable example.
AI is thinking...
5. What is the principle reason for passing arguments by reference? Explain with suitable code.
AI is thinking...
4. Explain the do while structure.
AI is thinking...
4. What is type casting? Explain with suitable example.
AI is thinking...
5. What is function overloading? Explain with suitable example.
AI is thinking...
5. Explain the inline function with example.
AI is thinking...
5. Explain with example of an inline function.
AI is thinking...
5. What is library function? How is it different from user defined function?
AI is thinking...
5. Explain do/while structure with example.
AI is thinking...
5. What is function overloading ? Explain with example.
AI is thinking...
6. What is library function? How is it different from user defined function?
AI is thinking...
AI is thinking...
6. Why type conversion is necessary in OOP? Explain with example, the type conversion routine.
AI is thinking...
6. Discuss relationship between pointers and arrays.
AI is thinking...
6. Explain the Inline function with example.
AI is thinking...
7. Explain about this pointer with suitable example.
AI is thinking...
5. What is the use of new and delete operators? Illustrate with example. What are advantages of new malloc.
AI is thinking...
6. What is meant by pass by reference? How can we pass arguments by reference by using reference variable? Illustrate with example.
AI is thinking...
7. Differentiate between structure and class in terms of access modifier
AI is thinking...
7. Explain the use of inline function with example.
AI is thinking...
7. Discuss the use of inline function with example.
AI is thinking...
5. What is the principle reason for using default arguments in the function? Explain how missing arguments and default arguments are handled by the function simultaneously?
A function can be called without specifying all its arguments. But it does not work on any general function. The function declaration must provide default values for those arguments that are not specified. When the arguments are missing from function call , default value will be used for calculation.
Example:
#include <iostream>
using namespace std;
int sum(int a, int b=10, int c=20);
int main(){
cout<<sum(11)<<endl; /* In this case a value is passed as 11 and b and c values are taken from default arguments.*/
cout<<sum(11, 12)<<endl; /* In this case a value is passed as 11 and b value as 12, value of c values is taken from default arguments.*/
cout<<sum(11, 12, 13)<<endl; /* In this case all the three values are passed during function call, hence no default arguments have been used.*/
return 0;
}
int sum(int a, int b, int c){
int z;
z = a+b+c;
return z;
}
AI is thinking...
6. What is meant by return by reference? How can we return values by reference by using reference variable? Illustrate with examples.
AI is thinking...
6."An overloaded function appears to perform different activities depending the kind of data send to it" Justify the statement with appropriate example.
AI is thinking...
9. Differentiate between overriding vs overloading.
AI is thinking...
9. Differentiate between macro and function.
AI is thinking...
9. Explain with example, how you create space for array of object using pointers?
AI is thinking...
8. What is this pointer? How can we use it for name conflict resolution?Illustrate with example.
We can use keyword "this" to refer to this instance inside a class definition. One of the main usage of keyword this is to resolve ambiguity between the names of data member and function parameter. For example,
class Circle {
private:
double radius; // Member variable called "radius"
......
public:
void setRadius(double radius) // Function's argument also called "radius"
{
this->radius = radius;
// "this.radius" refers to this instance's member variable
// "radius" resolved to the function's argument.
}
......
}
In the above codes, there are two identifiers called radius - a data member and the function parameter. This causes naming conflict. To resolve the naming conflict, we could name the function parameter r instead of radius. However, radius is more approximate and meaningful in this context. We can use keyword this to resolve this naming conflict. "this->radius" refers to the data member; while "radius" resolves to the function parameter. "this" is actually a pointer to this object.
AI is thinking...
10. Explain the use of break and continue statements in switch case statements in C++.
AI is thinking...
11. Write a program to demonstrate the use of default argument in functions.
AI is thinking...
11. Explain the different storage classes in C++.
AI is thinking...
12. How can you differentiate a macro with an inline function? Are they same or different? Justify.
AI is thinking...
13. What are the major differences between overriding and overloading?
AI is thinking...
1. Write a program according to the specification given below:
- Create a class Account with data members acc no, balance, and min_balance(static)
- Include methods for reading and displaying values of objects
- Define static member function to display min_balance
-Create array of objects to store data of 5 accounts and read and display values of each object
AI is thinking...
2. Explain the role of constructor and destructor in Object-Oriented Programming. Discuss user defined parameterized constructor with suitable example.
AI is thinking...
2. Why constructor and destructor are required on Object Oriented Programming? Explain with suitable example.
AI is thinking...
2. Discuss features of class and object. Design a class to represent a bank account with data members name, account-number, account-type, and balance and functions to assign initial values, to deposit an amount, to withdraw an amount after checking balance, and to display the name and balance. (4+6)
Class
- Class is a user defined data type, which holds its own data members and member functions, which can be accessed and used by creating instance of that class.
- The variables inside class definition are called as data members and the functions are called member functions.
- Class name must start with an uppercase letter. If class name is made of more than one word, then first letter of each word must be in uppercase.
- Classes contain, data members and member functions, and the access of these data members and variable depends on the access specifiers.
- Class's member functions can be defined inside the class definition or outside the class definition.
- Objects of class holds separate copies of data members. We can create as many objects of a class as we need.
Object
- Objects are instances of class, which holds the data variables declared in class and the member functions work on these class objects.
- Each object has different data variables.
- Objects are initialized using special class functions called Constructors.
- And whenever the object is out of its scope, another special class member function called Destructor is called, to release the memory reserved by the object.
Program:
#include <iostream>
#include<stdio.h>
#include<conio.h>
using namespace std;
class Bank
{
public:
char name[20];
char account_type[20];
int account_number;
int balance;
void initialize()
{
cout<<"\\nEnter Account Holders Name:";
cin>>name;
cout<<"\\nEnter Account type:";
cin>>account_type;
cout<<"\\nEnter account number:";
cin>>account_number;
cout<<"\\nEnter balance to deposit:";
cin>>balance;
}
void deposit()
{
int bal;
cout<<"\\nEnter the amout to deposit:";
cin>>bal;
balance=balance+bal;
cout<<"\\nAmount deposited successfuly!!\\nYour New Balance:"<<balance;
}
void withdraw()
{
int bal;
cout<<"\\nYour balance :"<<balance<<"\\nEnter amount to withdraw:";
cin>>bal;
if(bal<=balance)
{
balance=balance-bal;
cout<<"\\nRemaining Balance:"<<balance;
}
else
{
cout<<"Cannot withdraw amount!!";
}
}
void display()
{
cout<<"\\nName :"<<name;
cout<<"\\nAccout Type:"<<account_type;
cout<<"\\nAccount No."<<account_number;
cout<<"\\nBalance :"<<balance;
}
};
int main()
{
int i;
Bank bk;
bk.initialize();
cout<<"\\n1. Your Information\\n2. Deposit\\n3. Withdraw\\nEnter your choice:\\n";
cin>>i;
if(i==1)
{
bk.display();
}
else if(i==2)
{
bk.deposit();
}
else if(i==3)
{
bk.withdraw();
}
getch();
return 0;
}
AI is thinking...
2. How is a member function of a class defined? Define friend function. What are the merits and demerits of using friend function? Explain.
AI is thinking...
2. What is constructor? Explain their types? Discuss user defined parameterized constructor with suitable example.
AI is thinking...
1. Explain the concept of user-defined to user-defined data conversion rotine located in the destination class.
AI is thinking...
3. Create a class Stack with suitable data members and member functions to push and pop the elements of the stack. Add the exception when user tries to add item while the stack is full and when user tries to delete item while the stack is empty. Throw exception in both of the cases and handle these exception.
# include<iostream>
using namespace std;
class Stack
{
int top;
public:
int a[5]; //Maximum size of Stack
Stack() //constructor
{
top = -1;
}
void push(int x)
{
if(top<5)
{
a[++top] = x;
cout << "Element Inserted \\n";
}
else
{
throw "Stack is full. You cannot insert the item!!!";
}
}
int pop()
{
if(top>=0)
{
int d = a[top--];
return d;
}
else
{
throw "Stack is empty. You cannot pop the item!!!";
}
}
};
int main()
{
int item;
Stack s1;
int ch=1;
cout<<"\\n1.PUSH\\n2.POP\\n3.EXIT";
cout<<"\\nEnter your choice:";
cin>>ch;
do
{
switch(ch)
{
case 1:
cout<<"\\nEnter the item to push:";
cin>>item;
try
{
s1.push(item);
}
catch(const char* e)
{
cout<<e<<endl;
}
break;
case 2:
try
{
cout<<"\\nPoped Item is:"<<s1.pop();
}
catch( const char* e)
{
cout<<e<<endl;
}
break;
case 3:
exit(0);
}
cout<<"\\nEnter your choice:";
cin>>ch;
}while(ch<4);
return 0;
}
AI is thinking...
3. Define constructor, list some of the special properties of the constructor functions
AI is thinking...
6. Why constructor is needed? Explain different types of constructors with example.
AI is thinking...
4. How can you classify objects? Why dynamic objects are needed?
AI is thinking...
4. Why dynamic object is needed? Explain with suitable example.
AI is thinking...
4. What do you mean by dynamic initialization of variables?
AI is thinking...
4. What is constructor ? Write a program to demonstrate constructor overloading.
AI is thinking...
5. Explain about the importance of constructors and destructors with their execution sequence.
Constructors are special class functions which performs initialization of every object. The Compiler calls the Constructor whenever an object is created. Constructors initialize values to object members after storage is allocated to the object.
Destructor is a special class function which destroys the class object as soon as the scope of object ends. The destructor is called automatically by the compiler when the object goes out of scope.Importance of Constructor:
- Constructors are used to initialize the objects of the class with initial values.
- Constructors are invoked automatically when the objects are created.
- Constructors can have default parameters.
- If constructor is not declared for a class , the C++ compiler generates a default constructor.
- A constructor can be used explicitly to create new objects of its class type.
- The constructor is executed automatically.
- Constructor function can be overloaded.
Importance of Destructor:
- Destructors are invoked automatically when the objects are destroyed.
- Destructors can not have any parameter and will not return any value.
- Destructors are declared in a program to release memory space for future utilization.
Example
#include <iostream>
using namespace std;
class MyClass
{
private:
int m, n;
public:
MyClass( ) // constructor
{
m = 10;
n = 5;
}
~MyClass( ) //destructor
{
cout<<"Destructor called!!";
}
void display()
{
cout<<"The sum is:"<<(m+n)<<endl;
}
};
int main()
{
MyClass obj;
obj.display();
return 0;
}
AI is thinking...
6. How is dynamic initialization of objects achieved?
AI is thinking...
7. What are the importance of destructors?
AI is thinking...
8. Explain the static class members with example.
AI is thinking...
7. What is constructor? Explain the concept of default and default copy with suitable example.
AI is thinking...
8. What is class? Differentiate it with object.
AI is thinking...
8. What are the characteristics of constructor?
AI is thinking...
8. What is constructor? Differentiate it with destructor.
AI is thinking...
7. Explain the default action of the copy constructor. Write a suitable program that demonstrates the technique of overloading the copy constructor.
AI is thinking...
7. What is destructor? Write a program to show the destructor call such that it prints the message "memory is released".
Destructor is a special class function which destroys the object as soon as the scope of object ends. The destructor is called automatically by the compiler when the object goes out of scope. Destructors are declared in a program to release memory space for future utilization.
Program:
#include <iostream>
using namespace std;
class MyClass
{
private:
int m, n;
public:
MyClass( ) // constructor
{
m = 10;
n = 5;
}
~MyClass( ) //destructor
{
cout<<"Memory is released!!";
}
void display()
{
cout<<"The sum is:"<<(m+n)<<endl;
}
};
int main()
{
MyClass obj;
obj.display();
return 0;
}
Output
When an object is created the constructor of that class is called. The object reference is destroyed when its scope ends, which is generally after the closing curly bracket }
for the code block in which it is created.
AI is thinking...
9. How can you define a member function outside a class ? Explain with suitable example.
AI is thinking...
9. Explain about accessing a member function outside a class with example.
AI is thinking...
9. Create a real scenario where static data members are useful. Explain with suitable example.
AI is thinking...
2. Explain operator overloading. Write a program that overloads insertion and extraction operators.
AI is thinking...
2. Write a program to overload the unary minus operator using friend function.
AI is thinking...
2. What is meant by type conversion? Define two way of converting one user defined data type (object) to another user defined object? Write a program that converts object of another distance class with data members feet and inch.(Assume 1m = 3.3 feet and 1cm = 0.4 inch)
AI is thinking...
2. Explain the concept of operator overloading? List the operators that cannot be overloaded. Write programs to add two object of distance class with data members feet and inch by using by using member function and friend function.
In C++, we can make operators to work for user defined classes. This means C++ has the ability to provide the operators with a special meaning for a user-defined data type, this ability is known as operator overloading. For example, we can overload an operator ‘+’ in a class like String so that we can concatenate two strings by just using +. Other example classes where arithmetic operators may be overloaded are Distance which has data member feet and inch, Complex Number etc.
Operator that cannot be overloaded are as follows:
- Scope operator (::)
- Sizeof
- member selector(.)
- member pointer selector(*)
- ternary operator(?:)
Program
#include <iostream>
using namespace std;
class Distance
{
private:
int feet, inch;
public:
void readDistance(void)
{
cout << "Enter feet: ";
cin >>feet;
cout << "Enter inch: ";
cin >>inch;
}
void displayDistance(void)
{
cout << "Feet:" << feet << "\\t" << "Inches:" << inch << endl;
}
friend Distance operator+(Distance, Distance);
};
Distance operator+(Distance d1, Distance d2)
{
Distance temp;
temp.inch = d1.inch + d2.inch;
temp.feet = d1.feet + d2.feet + (temp.inch/12);
temp.inch = temp.inch%12;
return temp;
}
int main()
{
Distance d1,d2,d3;
cout << "Enter first distance:" << endl;
d1.readDistance();
cout << endl;
cout << "Enter second distance:" << endl;
d2.readDistance();
cout << endl;
d3=d1+d2;
cout << "The sum of two distance is:" << endl;
d3.displayDistance();
return 0;
}
AI is thinking...
3. What is operator overloading? What are the benefits of operator overloading? How is operator overloading different from function overloading. Write a program that shows an example of function overloading. (2+2+2+4)
AI is thinking...
7. Write a program that illustrates the conversions between objects of different classes having conversion function in source object.
AI is thinking...
5. Write a program to compute subtraction of two complex numbers using operator overloading.
AI is thinking...
5. What is operator overloading? Explain their types with suitable examples.
AI is thinking...
8. What is an operator function? Explain with syntax.
AI is thinking...
10. Write a program that increases an integer value by 1 (one) overloading + + operator.
AI is thinking...
10. Explain the role of operator overloading with example.
AI is thinking...
2. How can we use inheritance for code reusability? Discuss multiple inheritance with suitable example.
AI is thinking...
1. Write a program according to the specification given below:
- Create a class Teacher with data members tid & subject and ember functions for reading and displaying data members.
- Create another class Staff with data members sid & position, and member function for reading and displaying data members.
- Derive a class Coordinator from Teacher and Staff and the class must have its own data member department and member functions for reading and displaying data members.
- Create two object of Coordinator class and read and display their details.
#include <iostream>
using namespace std;
class Teacher
{
int tid;
string subject;
public:
void TeacherRead()
{
cout<<"Enter teacher's id:"<<endl;
cin>>tid;
cout<<"Enter teacher's subject:"<<endl;
cin>>subject;
}
void TeacherDisplay()
{
cout<<"Teacher's Id:"<<tid<<endl;
cout<<"Teacher's subject:"<<subject<<endl;
}
};
class Staff
{
int sid;
string position;
public:
void StaffRead()
{
cout<<"Enter staff's id:"<<endl;
cin>>sid;
cout<<"Enter staff's position:"<<endl;
cin>>position;
}
void StaffDisplay()
{
cout<<"Staff's id:"<<sid<<endl;
cout<<"Staff's position:"<<position<<endl;
}
};
class Coordinator: public Teacher, public Staff
{
string department;
public:
void CoordinatorRead()
{
cout<<"Enter department:"<<endl;
cin>>department;
}
void CoordinatorDisplay()
{
cout<<"Department:"<<department<<endl;
}
};
int main()
{
Coordinator c1, c2;
c1.TeacherRead();
c1.CoordinatorRead();
c1.TeacherDisplay();
c1.CoordinatorDisplay();
c2.StaffRead();
c2.CoordinatorRead();
c2. StaffDisplay();
c2.CoordinatorDisplay();
return 0;
}
Output:
AI is thinking...
2. Differentiate between single inheritance and multiple inheritance? Imagine a college hires some lectures. Some lectures are paid in period basic, while others are paid in month basic. Create a class called lecture that stores ID and name of lectures. From this class derive two classes: part time, which adds payperhr(type float): and full time, which adds paypermonth(type float). Each of these three classes should have a readdata() function to get its data from user at the key board and printdata() function to display the data.
Write a main() program to test the Full time and Part time classes by creating instance of them asking the user to fill their data with readdata () and display the data with printdata().
Program:
#include <iostream>
using namespace std;
class Lecture
{
public:
int id;
string name;
void readdata()
{
cout<<"Enter the Id of lecture:";
cin>>id;
cout<<"Enter the name of leture:";
cin>>name;
}
void printdata()
{
cout<<"Id of lecture : "<<id<<endl;
cout<<"Name of lecture: "<<name<<endl;
}
};
class PartTime:public Lecture
{
public:
float payperhr;
void readdatapt()
{
cout<<"Enter the salary paid per hour: ";
cin>>payperhr;
}
void printdatapt()
{
cout<<"Salary paid per hour:"<<payperhr<<endl;
}
};
class FullTime:public Lecture
{
public:
float paypermonth;
void readdataft()
{
cout<<"Enter the salary paid per month: ";
cin>>paypermonth;
}
void printdataft()
{
cout<<"Salary paid per month:"<<paypermonth<<endl;
}
};
int main()
{
PartTime p1;
FullTime f1;
cout<<"***Part Time***"<<endl;
p1.readdata();
p1.readdatapt();
cout<<"***Displaying Data***"<<endl;
p1.printdata();
p1.printdatapt();
cout<<"***Full Time***"<<endl;
f1.readdata();
f1.readdataft();
cout<<"***Displaying Data***"<<endl;
f1.printdata();
f1.printdataft();
return 0;
}
AI is thinking...
3. What is inheritance? Explain the ambiguities associated with multiple inheritance with suitable example programs.
AI is thinking...
2. Depict the difference between private and public derivation. Explain derived class constructor with suitable program.
AI is thinking...
3. How ambiguity arises in multipath inheritance? How can you remove this type of ambiguity? Explain with suitable example.
AI is thinking...
3. Define a student class (with necessary constructors and member functions) in Object Oriented Programming (abstract necessary attributes and their types). (Write a complete code in C++ programming language).
• Derive a computer Science and Mathematics class from student class adding necessary attributes (at least three subjects).
• Use these classes in a main function and display the average marks of computer science and mathematics students.
AI is thinking...
3. Define a clock class (with necessary constructors and member functions) in Object Oriented Programming (abstract necessary attributes and their types). (Write a complete code in C++ programming language).
- Derive a wall_clock class from clock class adding necessary attributes.
- Create two objects of wall_clock class with all initial state to 0 or NULL.
AI is thinking...
3. Define a Shape class (with necessary constructors and member functions) in Object-Oriented Programming (abstract necessary attributes and their types). (Write a complete code in C++ programming language)
• Derive Triangle and Rectangle classes from Shape class adding necessary attributes.
• Use these classes in a main function and display the area of triangle and rectangle.
AI is thinking...
3. Explain the role of inheritance in object oriented programming. What is public, private and protected dentation? Explain.
AI is thinking...
4. Explain the syntax and rules of multiple inheritance in C++ with example.
When a class is derived from two or more base classes, such inheritance is called Multiple Inheritance. Multiple Inheritance in C++ allow us to combine the features of several existing classes into a single class.
Syntax:
class base_class1
{
// body of the class
};
class base_class2
{
//body of the class
};
class derived_classname : visibility_mode base_class1, visibility_mode base_class2
{
//body of the class
};
Example:
#include <iostream>
using namespace std;
class A
{
protected:
int a;
public:
void get_a(int n)
{
a = n;
}
};
class B
{
protected:
int b;
public:
void get_b(int n)
{
b = n;
}
};
class C : public A, public B
{
public:
void display()
{
cout << "The value of a is : " <<a<<endl;
cout << "The value of b is : " <<b<<endl;
cout <<"Addition of a and b is : "<<a+b;
}
};
int main()
{
C obj;
obj.get_a(10);
obj.get_b(20);
obj.display();
return 0;
}
AI is thinking...
8. Explain the difference between private and public inheritance with suitable diagram.
AI is thinking...
7. Differentiate between super class and sub class with suitable examples.
AI is thinking...
7. What is Inheritance? Explain their types with their suitable examples.
AI is thinking...
7. What is multiple inheritance? Explain with example.
AI is thinking...
7. What is the role of protected access specifies in inheritance ? Explain with example.
AI is thinking...
7. Differentiate between base class and derived class with suitable examples.
AI is thinking...
8. Differntiate between private, public and protected variable with suitable example.
AI is thinking...
8. Briefly explain types of inheritance used in object oriented Programming.
AI is thinking...
10. What is container class? Differentiate container class from inheritance.
AI is thinking...
12. Explain the multilevel inheritance. How is it different from multiple inheritance?
AI is thinking...
12. Write short notes on:
a. Manipulators
b. Protected Access Specifier
AI is thinking...
AI is thinking...
3. Explain types of polymorphism briefly. Write down roles of polymorphism. How can we achieve dynamic polymorphism briefly. Write down foles of polymorphism. How can we achieve dynamic polymorphism? Explain with example.
AI is thinking...
4. Display polymorphism with example.
AI is thinking...
4. "Concept of friend in against the philosophy of Object Oriented Programming". Explain.
A friend function is a function that can access private members of a class even though it is not a member of that class.
A class can also be declared as friend of some other class. When a class ABC declares another class XYZ as its friend, then friend class XYZ can access to all data and function members of class ABC.
The philosophy of object oriented programming is that we can't access private members of a class from outside the class. But in C++, we can access private members of a class even from non-member function using concept of friend function and friend class. So, it is against the philosophy of Object Oriented Programming.
AI is thinking...
6. What is virtual function? Explain.
AI is thinking...
9. Why friend function is required? Discuss with example.
AI is thinking...
10. How late binding is different from early binding. Write a program that explains late binding using virtual function.
AI is thinking...
8. What is friend function? Why it is used in OOP? Explain with an example.
AI is thinking...
8. Differentiate between virtual function and pure virtual function.
AI is thinking...
9. Differentiate between compile time polymorphism and run time polymorphism.
AI is thinking...
9. What is container class? Differentiate container class from inheritance.
AI is thinking...
8. What is the concept of friend function? How it violates the data hiding principle? Justify with example.
AI is thinking...
9. Differentiate container class from inheritance. Explain with suitable example.
AI is thinking...
9. What is abstract base class? Give an example.
AI is thinking...
9. Differentiate between function overriding and function overloading. Explain with suitable example.
AI is thinking...
10. Explain the role of polymorphism in Object Oriented Programming.
AI is thinking...
10. Explain the role of polymorphism in Object Oriented Programming.
AI is thinking...
11. Explain about “this” pointer with suitable example.
AI is thinking...
11. What is friend function? Write a program to multiply any two private numbers of two different classes using friend function.
A friend function is a function that can access private members of a class even though it is not a member of that class.
Program:
#include <iostream>
using namespace std;
class XYZ;
class ABC
{
private:
int value;
public:
ABC()
{
value = 6;
}
friend int multiply(ABC, XYZ);
};
class XYZ
{
private:
int value;
public:
XYZ()
{
value = 4;
}
friend int multiply(ABC, XYZ);
};
int multiply( ABC v1, XYZ v2 )
{
return (v1.value * v2.value);
}
int main()
{
ABC a;
XYZ x;
cout << "Product of two number is : " << multiply( a, x ) << endl;
return 0;
}
AI is thinking...
13. Explain the friend function with its syntax.
AI is thinking...
6. What is template? How can you differentiate a function template from a class template? Explain.
Templates is defined as a blueprint or formula for creating a generic class or a function. To simply put, we can create a single function or single class to work with different data types using templates.
Function Template
It is used to define a function to handle arguments of different types in different times. Using function template, we can use same function for arguments of one data type in some cases and arguments of other types in other cases.
Syntax:
template
<
class
type>
ret_type func_name(parameter list)
{
//body of the function
}
Example:
#include<iostream.h>
using namespace std;
template<class X>
X func( X a, X b)
{
return a;
}
int main()
count<<func(15,8); //func(int,int);
count,,func('p','q'); //func(char,char);
count<<func(7.5,9.2); //func(double,double)
return();
}
Class Template
Class Templates Like function templates, class templates are useful when a class defines something that is independent of the data type.
Syntax:
template
<
class
Ttype>
class
class_name
{
//class body;
}
Example:
#include <iostream> using namespace std; template <class T> class Calculator { private: T num1, num2; public: Calculator(T n1, T n2) { num1 = n1; num2 = n2; } void displayResult() { cout << "Numbers are: " << num1 << " and " << num2 << "." << endl; cout << "Addition is: " << add() << endl; cout << "Subtraction is: " << subtract() << endl; cout << "Product is: " << multiply() << endl; cout << "Division is: " << divide() << endl; } T add() { return num1 + num2; } T subtract() { return num1 - num2; } T multiply() { return num1 * num2; } T divide() { return num1 / num2; } }; int main() { Calculator<int> intCalc(2, 1); Calculator<float> floatCalc(2.4, 1.2); cout << "Int results:" << endl; intCalc.displayResult(); cout << endl << "Float results:" << endl; floatCalc.displayResult(); return 0; }
AI is thinking...
6. Write a C++ program containing a possible exception. Use a try block to throw it and a catch block to handle it properly.
AI is thinking...
6. Why exception handling is required? Explain with suitable example.
AI is thinking...
8. Write a C++ program containing a possible exception. Use a try block to throw it and a catch block to handle it.
AI is thinking...
11. Why do we need exceptions? Explain “exceptions with arguments” with suitable program.
AI is thinking...
9. What is exception? Why exception handling is better to use? Explain exception handling with try..... catch by using suitable example.
AI is thinking...
10. Explain the function templates with example.
AI is thinking...
11. Differentiate between overloaded functions and function templates.
AI is thinking...
11. Discuss importance of template. Write syntax of function template.
AI is thinking...
9. How can you define catch statement that can catch any type of exception? Illustrate the use of multiple catch statement with example.
The catch block defines the action to be taken, when an exception occur. We can use the catch statement with three dots as parameter (...) so that it can catch any types of exceptions. For example:
#include <iostream>
using namespace std;
void func(int a) {
try {
if(a==0) throw 23.33;
if(a==1) throw 's';
} catch(...) {
cout << "Caught Exception!\\n";
}
}
int main() {
func(0);
func(1);
return 0;
}
Multiple Catch Statements
Multiple catch statements are used when we have to catch a specific type of exception out of many possible type of exceptions i.e. an exception of type char or int or short or long etc. For example:
#include<iostream.h>
#include<conio.h>
void main()
{
int a=2;
try
{
if(a==1)
throw a; //throwing integer exception
else if(a==2)
throw 'A'; //throwing character exception
else if(a==3)
throw 4.5; //throwing float exception
}
catch(int a)
{
cout<<"\\nInteger exception caught.";
}
catch(char ch)
{
cout<<"\\nCharacter exception caught.";
}
catch(double d)
{
cout<<"\\nDouble exception caught.";
}
cout<<"\\nEnd of program.";
}
Output :
Character exception caught.
End of program.
AI is thinking...
10. When class templates are useful? How can you define a class that can implement stack with integer as well as sack of strings? Illustrate with example.
AI is thinking...
12. Discuss different keywords used in exception handling.
AI is thinking...
10. Create a function called swaps() that interchanges the values of the two arguments sent to it (pass these arguments by reference). Make the function into a template, so it can be used with all numerical data types (char, int, float, and so on). Write a main() program to exercise the function with several types.
#include<iostream>
using namespace std;
template <class T>
void swaps(T &a,T &b) //Function Template
{
T temp=a;
a=b;
b=temp;
}
int main()
{
int x1=5,y1=9;
float x2=3.5,y2=6.5;
cout<<"Before Swap:"<<endl;
cout<<"x1="<<x1<<"y1="<<y1<<endl;
cout<<"x2="<<x2<<"y2="<<y2<<endl;
swaps(x1,y1);
swaps(x2,y2);
cout<<"After Swap:"<<endl;
cout<<"x1="<<x1<<"y1="<<y1<<endl;
cout<<"x2="<<x2<<"y2="<<y2<<endl;
return 0;
}
AI is thinking...
12. What are the main advantages of using exception handling mechanism in a program?
AI is thinking...
12. Define try, throw and catch statement in C++ with example.
AI is thinking...
11. Explain how exceptions are used for handling C++ error in a systematic and OOP-oriented way with the design that includes multiple exceptions.
Exceptions are run-time anomalies or abnormal conditions that a program encounters during its execution. Exception Handling in C++ is a process to handle runtime errors. We perform exception handling so the normal flow of the application can be maintained even after runtime errors. In C++, we use 3 keywords to perform exception handling: try, catch, and throw
- The
try
statement allows us to define a block of code to be tested for errors while it is being executed. - The
throw
keyword throws an exception when a problem is detected, which lets us create a custom error. - The
catch
statement allows us to define a block of code to be executed, if an error occurs in the try block.
Syntax to handle multiple exceptions:
try
{
//protected code
} catch(exceptionName e1)
{
// catch block
} catch(exceptionName e2)
{
// catch block
} catch(exceptionName en)
{
// catch block
}
Example:
#include<iostream.h>
#include<conio.h>
void main()
{
int a=2;
try
{
if(a==1)
throw a; //throwing integer exception
else if(a==2)
throw 'A'; //throwing character exception
else if(a==3)
throw 4.5; //throwing float exception
}
catch(int a)
{
cout<<"\\nInteger exception caught.";
}
catch(char ch)
{
cout<<"\\nCharacter exception caught.";
}
catch(double d)
{
cout<<"\\nDouble exception caught.";
}
cout<<"\\nEnd of program.";
}
Output :
Character exception caught.
End of program.
AI is thinking...
13. Differentiate between class template and function template.
AI is thinking...
13. Explain the exceptional handling with example.
AI is thinking...
3. Briefly explain the hierarchy of stream classes. Write a program that overloads extraction and insertion operators.
AI is thinking...
5. Discuss input and output with C in and C out respectively.
AI is thinking...
4. Write a member function called reverseit() that reverses a string ( an array of character). Use a for loop that swaps the first and last characters, then the second and next-to last characters and so on. The string should be passed to reverseit() as an argument.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void reverseit(char *p)
{
int j;
int len=strlen(p);
j=len-1;
for(int i=0;i<len/2;i++)
{
char a=p[i];
p[i]=p[j];
p[j]=a;
j--;
}
cout<<"\\n\\nReverse Of String :";
puts(p);
}
void main()
{
char str[50];
char ch;
cout<<"\\nEnter The String : ";
gets(str);
reverseit(str);
getch();
}
AI is thinking...
5. What is the use of get and getline functions? Explain with suitable example.
AI is thinking...
8. Write a program in C++ to count a number of words in a line of text.
AI is thinking...
12. What are the advantages of using the stream classes for I/O? Write a program that writes object to a file.
AI is thinking...
10. Explain the features of I/O system supported by C++.
AI is thinking...
10. Which functions can be used for reading and writing object? Describe briefly. Write a program that read values of two objects of student class(assume data members are sid , sname, and level) and display the data in monitor.
AI is thinking...
11. What is meant by stream? Write a program that reads content of file data.txt and displays the content in monitor.
AI is thinking...
12. Write the syntax and use of get line () and write () functions.
AI is thinking...
12. Write short notes on:
- Cascading of IO operators
- Pure Virtual Function
AI is thinking...
12. How is character I/O different from Binary I/O? Explain with examples.
AI is thinking...