top of page
  • Facebook
  • Twitter
  • Instagram

C plus plus Notes

mobiprep (6).png

Basics of C plus plus

mobiprep (6).png

Basics Of OOPs

mobiprep (6).png

Classes and Objects

mobiprep (6).png

Polymorphism and Inheritence

mobiprep (6).png

Exception Handling

mobiprep (6).png

File Handling and templates

Heading

Q

1

Explain different types of polymorphism.

LRM_EXPORT_207556595493866_20190724_1939

Ans

There are two types of Polymorphism-
1. Run time Polymorphism-
If the appropriate member function could be selected while the program is running. This is called run time polymorphism.
2. Compile time Polymorphism-
The compiler uses the information from functions and operators at compile time, and, hence, is able to select the appropriate function for a particular call at compile time. This is called early or static binding or static linking or compile time polymorphism.

LRM_EXPORT_207556595493866_20190724_1939

Q

2

Explain Pure Virtual Functions.

LRM_EXPORT_207556595493866_20190724_1939

Ans

A pure virtual function is a member function of base class whose only declaration is provided in base class and should be defined in derived class otherwise derived class also becomes abstract. Base class having pure virtual function becomes abstract i.e. it cannot be instantiated.
Syntax:
virtual return_type function_name() = 0;
Example:
class Base
{
public:
virtual void show() = 0;
};
class Derived:public Base
{
void show()
{cout<<”I love TV shows”);}
};
int main()
{
Derived d;
d.show();
return 0;
}

Output: I love TV shows

LRM_EXPORT_207556595493866_20190724_1939

Q

3

What is the need of overloading operators and functions?

LRM_EXPORT_207556595493866_20190724_1939

Ans

Overloading is needed as it increases the readability of the program as you don’t need to use different names for the same action.
Note that All except three operators: ‘.’, ’?:’ and ‘sizeof” can be overloaded in C.

LRM_EXPORT_207556595493866_20190724_1939

Q

4

Write down the example to overload unary and binary operators in C++.

LRM_EXPORT_207556595493866_20190724_1939

Ans

Unary- (++)
#include <iostream>
using namespace std;
class Count
{
private:
int value;
public:
// Constructor to initialize count to 5
Count() : value(5) {}
// Overload ++ when used as prefix
void operator ++ ()
{
++value;
}
void display() {
cout <<"Count" << value << endl;
}
};
int main()
{
Count count1;
// Call the "void operator ++ ()" function
++count1;
count1.display();
return 0;
}
Output- count: 6
count: 7
Binary-
#include <iostream>
using namespace std;
class Count {
private:
int value;
public Count() : value(5) {}
Count operator ++ () {
Count temp;
temp.value = ++value;
return temp; }
Count operator ++ (int) {
Count temp;
temp.value = ++value;
return temp; }
void display() {
cout<<"Count:"<<value <<endl; }
};
int main()
{
Count count1, result;
result = ++count1;
result.display();
result = count1++;
result.display();
return 0;
}
Output- count: 6
count: 7

LRM_EXPORT_207556595493866_20190724_1939

Q

5

Write down a C++ program to implement function overloading.

LRM_EXPORT_207556595493866_20190724_1939

Ans

#include <iostream>
using namespace std;
/ / function with float type parameter
float absolute(float var)
{
if (var &< 0.0)
var = -var;
return var;
}
// function with int type parameter
int absolute(int var)
{
if (var < 0)
var = -var;
return var;
}
int main()
{
cout <<"Absolute value of -5 = "<< absolute(-5) <<endl;
cout << "Absolute value of 5.5 ="<<absolute(5.5f) << endl;
return 0;
}
Output- Absolute value of -5 = 5
Absolute value of 5.5 = 5.5

LRM_EXPORT_207556595493866_20190724_1939

Q

6

What is the need of inheritance?

LRM_EXPORT_207556595493866_20190724_1939

Ans

Inheritance is the process by which one object acquires the properties of another object. This is important because it supports the concept of hierarchical classification. most knowledge is made manageable by hierarchical (that is, top-down) classifications.

LRM_EXPORT_207556595493866_20190724_1939

Q

7

Draw a diagram to represent the forms of inheritance.

LRM_EXPORT_207556595493866_20190724_1939

Ans

LRM_EXPORT_207556595493866_20190724_1939

Q

8

Discuss the role of access specifiers in inheritance and show their visibility when they are inherited as public, private and protected.

LRM_EXPORT_207556595493866_20190724_1939

Ans

In inheritance, it is important to know when a member function in the base class can be used by the objects of the derived class.
A public member is accessible from anywhere outside the class but within a program.
A private member variable or function cannot be accessed, or even viewed from outside the class.
A protected member variable or function is very similar to a private member but it provided one additional benefit that they can be accessed in child classes which are called derived classes.

LRM_EXPORT_207556595493866_20190724_1939

Q

9

Discuss the concept of generalization and aggregation.

LRM_EXPORT_207556595493866_20190724_1939

Ans

Generalization is the process of extracting common properties from a set of entities and creating a generalized entity from it. In specialization, an entity is divided into sub-entities based on their characteristics. It is a top-down approach where a higher level entity is specialized into two or more lower level entities. A relationship with its corresponding entities is aggregated into a higher level entity.

LRM_EXPORT_207556595493866_20190724_1939

Q

10

How overriding is different from the overloading?

LRM_EXPORT_207556595493866_20190724_1939

Ans

Overriding happens at runtime. Binding of overridden method call to its definition happens at runtime.

LRM_EXPORT_207556595493866_20190724_1939
bottom of page