Imp C++ WithAnswers

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Q1:Give the Difference between POP and OOP.

Ans:

Q2: Structure of C++ program.

Ans:
Q3: What is class?

Ans:
Ans: A class can be declared as a collection of data members along
with members function which allows association of data and
functions into a single unit called encapsulation.

Q4: Explain constructor? List the types and properties of


constructor.
Ans: Constructors is a member function having the same name as
that of its class and is executed automatically when the class is
instantiated (object is created).
Types of Constructor:
1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor
4. Dynamic
Constructor Properties of
Constructor:
• They should be declared in the public section of the
class declaration.
• They are invoked automatically when objects are created.
• They cannot be virtual.
• We cannot refer their addresses.
• They cannot return values.
• They can have default arguments.

Q5: What is a reference variable and give example of


reference variable?
Ans: A reference variable is an alias, that is, another name for an
already existing variable.
Either the variable name or the reference name may be used to
refer to the variable.
Eg int i = 17; // variable is declared
int& r = i; // reference variable r is declared

Q6: What is a function?


Ans: A function is a block of code which executes the statements
when we call it.
It consists of three entities:
1) the function name.this is simply a unique identifier.
2) The function parameters.this is a set of zero or more
typed identifier.
3) The function return type this specifies the type of value
function returns.

Q7: Explain Inline function?


Ans: Inline functions are those function whose function body is
inserted in place of the function call.

Q8: What is function overloading? Explain with an example


Ans: Function overloading is the concept where multiple functions
can have the same name with different parameters.
Example
int myFunction(int x)
float myFunction(float x)
double myFunction(double x, double y)

Q9: What is implicit and explicit type conversion?


Ans: In implicit casting in c++ compile automatically handle the
type conversion, the final result expressed in the highest precision
possible.

Explicit: the conversion of data type of two operands is not


automatic but forced.we can force an expression to be a specific
type by using a cast.

Q10: What is friend function?


Ans: A friend function is a function which is use to access the
private data member of different class.

Q11: What is inheritance in C++ and name the different


types of inheritance?
Ans: The mechanism of deriving a new class from an old class is
called as inheritance.
The class which we are inheriting from is called as the base class
and the class which inherits called as derived class.
Different types of inheritance are as follows-:
1) Single level
2) Multiple
3) Hierarchical
4) Multi-level
5) Hybrid
Q12: Explain default argument with an example.
Ans: A default argument is a value provided in a function
declaration that is automatically assigned by the compiler if the
caller of the function doesn’t provide a value for the argument
with a default value.
Example:
#include <iostream.h>
int sum(int x, int y, int z=0, int w=0)
{
return (x + y + z + w);
}
int main()
{
cout << sum(10, 15) << endl;
cout << sum(10, 15, 25) << endl;
cout << sum(10, 15, 25, 30) << endl;
return 0;
}

Q13: Explain static data member.


Ans: Static data members are class members that are declared
using the static keyword. There is only one copy of the static data
member in the class, even if there are many class objects. All the
objects of a particular class share the common static data member.
The static data member is always initialized to zero when the first
class object is created.

Syntax: static data_type data_member_name;

Q14: Explain virtual function.


Ans:
• The same function name is created in both the base and
derived classes, the function in base class is declared as
virtual using the keyword virtual preceding its normal
declaration.
• Requirements:
– It must be member of some class.
– It can not be a static member.
– It can only accessed by pointers.
– It can’t be friend of another class.
– It must be defined in the base class.
– There is no virtual constructor, but we have
virtual destructor.

Q15: Explain Input stream and Output stream in brief.


Ans: A stream is a sequence of bytes or character.

INPUT STREAMS

• cin is the default object connected to keyboard


• cin can be used to receive the input from keyboard eg cin>>a;

OUTPUT STREAMS

• cout is default object connected to monitor


• cout is used to print output on monitor
Q17:- Write a C++ program to demonstrate use of constructor overloading.

Ans:- Prerequisites: Constructors in C++


In C++, We can have more than one constructor in a class with
same name, as long as each has a different list of arguments.This
concept is known as Constructor Overloading and is quite similar
to function overloading.

Overloaded constructors essentially have the same name (exact


name of the class) and different by number and type of
arguments.
A constructor is called depending upon the number and type of
arguments passed.
While creating the object, arguments must be passed to let
compiler know, which constructor needs to be called .

Q18: Write a C++ program to demonstrate constructor call in the derived

Class.

Certainly! Below is an example of a C++ program


And:-
demonstrating constructor calls in a derived class. In this
example, there is a base class Shape with a parameterized
constructor, and a derived class Rectangle that inherits from
Shape. Both classes have their own constructors.

In this example:

The ‘Shape’ class has a parameterized constructor that initializes


‘width’ and ‘height’.
The ‘Rectangle’ class is derived from ‘Shape’ and has its own
parameterized constructor.
When you create an object of the ‘Rectangle’ class, it invokes both
the base class (‘Shape’) and derived class (‘Rectangle’)
constructors.

This program outputs: Shape constructor called


Rectangle constructor called
Area of the rectangle: 50
Q19: Explain virtual base class with example. / Why we need to
define

Virtual base class in Hybrid inheritance? Explain with example.

Ans:- વર્ચ્યુઅલ બેઝ ક્લાસ અને હાઇબ્રિડ ઇન્હેરિટન્સ


વર્ચ્યુઅલ બેઝ ક્લાસ શું છે?
વર્ચ્યુઅલ બેઝ ક્લાસ એ એક ક્લાસ છે જેને ડેરિવ્ડ ક્લાસ ડેક્લરેશનમાં તેના નામ
પહેલા virtual કીવર્ડ સાથે ડિક્લેર કરવામાં આવે છે. આ કીવર્ડ કમ્પાઇલરને બેઝ
ક્લાસ ઑબ્જેક્ટની ફક્ત એક જ કૉપી બનાવવાની સૂચના આપે છે, ભલે તેને કેટલી
વાર inherit કરવામાં આવે.

વર્ચ્યુઅલ બેઝ ક્લાસનો ઉપયોગ કેમ કરવો?


વર્ચ્યુઅલ બેઝ ક્લાસ વિના, મલ્ટીપલ ઇન્હેરિટન્સથી સમસ્યાઓ ઊભી થઈ શકે છે,
જેમ કે:

ડાયમંડ પ્રોબ્લેમ: જો બે ક્લાસ એક સામાન્ય બેઝ ક્લાસથી inherit કરે છે, અને ત્રીજો
ક્લાસ બંનેમાંથી inherit કરે છે, તો બેઝ ક્લાસ ત્રીજા ક્લાસના મેમરી લેઆઉટમાં
duplicate થશે. આ બીજા-બીજાને અવરોધે છે અને મેમરી વેડફે છે.
મેમ્બર ઍક્સેસમાં અસ્પષ્ટતા: જો બે બેઝ ક્લાસમાં સમાન નામના મેમ્બર હોય, તો
ડેરિવ્ડ ક્લાસમાં તે મેમ્બરને ઍક્સેસ કરવાથી અસ્પષ્ટતા ઊભી થઈ શકે છે.
વર્ચ્યુઅલ બેઝ ક્લાસ આ સમસ્યાઓનું સમાધાન કરે છે, તે ફક્ત એક જ બેઝ ક્લાસ
ઑબ્જેક્ટની નકલ બનાવે છે, ભલે તેને કેટલી વાર inherit કરવામાં આવે. આ
ડાયમંડ પ્રોબ્લેમ અને મેમ્બર ઍક્સેસ અસ્પષ્ટતાની શક્યતાને દૂર કરે છે.

હાઇબ્રિડ ઇન્હેરિટન્સમાં વર્ચ્યુઅલ બેઝ ક્લાસ


હાઇબ્રિડ ઇન્હેરિટન્સ એ મલ્ટીપલ અને સિંગલ ઇન્હેરિટન્સનું સંયોજન છે. એક ક્લાસ
મલ્ટીપલ બેઝ ક્લાસ (મલ્ટીપલ ઇન્હેરિટન્સ)માંથી inherit કરી શકે છે, પરંતુ તે બેઝ
ક્લાસમાંથી એકને વર્ચ્યુઅલ ઇન્હેરિટન્સનો ઉપયોગ કરીને inherit કરી શકાય છે.
હાઇબ્રિડ ઇન્હેરિટન્સમાં વર્ચ્યુઅલ બેઝ ક્લાસનો ઉપયોગ કેમ કરવો?
હાઇબ્રિડ ઇન્હેરિટન્સમાં વર્ચ્યુઅલ બેઝ ક્લાસનો ઉપયોગ કરવો ખૂબ જ મહત્વપૂર્ણ છે
કારણ કે તે બેઝ ક્લાસ ઑબ્જેક્ટ અને તેની સાથે સંકળાયેલી સમસ્યાઓની duplicate
રોકે છે.

ઉદાહરણ:

માની લો કે આપણી પાસે ત્રણ ક્લાસ છે: Shape, Color, અને Square. Shape
ક્લાસમાં width અને height જેવા ગુણધ

You might also like