[go: up one dir, main page]

0% found this document useful (0 votes)
31 views28 pages

C++ Imp

C++
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views28 pages

C++ Imp

C++
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 28

1) What is Object Oriented Programming (OOPS)?

&
Explain characteristics of OOP?

Ans:- Object-Oriented Programming (OOP) is a programming


paradigm that organizes software design around objects and data
rather than actions and logic. It revolves around the concept of
"objects," which can contain data, in the form of fields or attributes,
and code, in the form of procedures or methods.

Characteristics of OOP include:

Encapsulation: Bundling data (attributes) and methods that operate


on the data within a single unit (an object), thus hiding the internal
workings of an object and only exposing necessary information.

Inheritance: The ability of one class (a blueprint for an object) to


inherit properties and behavior from another class, allowing for
the creation of hierarchical relationships between classes.

Polymorphism: The ability for objects to be processed in various ways


depending on their data type or class, allowing methods to be written
that can work with objects of multiple types.

Abstraction: Simplifying complex reality by modeling classes


appropriate to the problem, focusing on essential
characteristics while hiding unnecessary details.
2) Difference between Structured Vs. OOPS?
Ans:- Structured programming and Object-Oriented Programming
(OOP) are two different paradigms used in software development.

Structured Programming focuses on organizing code into small,


manageable, and reusable blocks called functions or procedures. It
emphasizes clear, step-by-step procedures, usually using functions
or routines to perform specific tasks. It doesn't have the concept of
objects and doesn't allow data to be encapsulated with the methods
that operate on them.

Object-Oriented Programming (OOP), on the other hand, revolves


around the concept of objects, which bundle data and the methods
that manipulate that data into a single unit. It promotes concepts like
inheritance, encapsulation, and polymorphism. Encapsulation hides
the internal state of an object and only exposes the necessary
functionalities through methods, while inheritance allows new
classes to inherit properties and behaviors from existing ones,
promoting code reuse
3) Explain all data types?
Ans:- Sure, data types define the kind of values that can be stored
and manipulated in a programming language. They include:

Integer: Whole numbers without a decimal point.


Float/Double: Numbers with a decimal point.
String: Sequence of characters, like text.
Boolean: Represents true or false.
Array: Collection of similar data types.
Object: Stores key-value pairs.
Null: Represents the absence of a value.
Undefined: Default value for uninitialized variables.
Symbol: Unique and immutable data type introduced in ES6.
4) what is Operator & Explain their types?
Ans:- Operators in programming are symbols or keywords that perform
operations on one or more operands. They are used to manipulate data
and perform computations.

The types of operators in programming include:

1.Arithmetic Operators: These perform arithmetic operations like addition (+),


subtraction (-), multiplication (*), division (/), modulus (%), and
exponentiation (**).

2.Assignment Operators: They assign values to variables. Examples include


(=, +=, -=, *=, /=, %=).

3.Comparison Operators: These are used to compare values and return a


boolean result. Examples include equal to (==), not equal to (!=), greater than
(>), less than (<), greater than or equal to (>=), less than or equal to (<=).

4.Logical Operators: These operators perform logical operations.


Examples include AND (&&), OR (||), and NOT (!).

5.Bitwise Operators: These operate on bits and perform bit-level operations.


Examples include bitwise AND (&), bitwise OR (|), bitwise XOR (^), left shift
(<<), and right shift (>>).

6.Ternary Operator: It's a conditional operator that evaluates a condition and


returns a value based on whether the condition is true or false.

7.Unary Operators: These operate on a single operand. Examples include


unary plus (+), unary minus (-), increment (++), and decrement (--).

8.String Concatenation Operator: Used to concatenate strings together. In


some languages, it's represented by the plus (+) symbol.
5) Explain any one control statement? (if-else, switch, else-if
ladder etc?
Ans:- , let's delve into the 'if-else' control statement:
The 'if-else' statement is a fundamental part of programming
languages. It allows you to create conditional branches in your
code. Here's how it works:

'if' statement: It evaluates a condition and executes a block of code


if that condition is true. For example:

x = 10
if x > 5:
print("x is greater than 5")
'else' statement: If the condition in the 'if' statement is false, the
code within the 'else' block gets executed. For instance:

x=3
if x > 5:
print("x is greater than 5")
else:
print("x is less than or equal to 5")
6) Explain Concept of Pointer variables?
Ans:- Pointer variables in programming languages like C, C++, and
others are variables that store memory addresses as their values.
Instead of directly storing data, they store the location (address) of
where data is stored in memory. They allow you to indirectly access
and manipulate data by referring to its memory address. This can be
particularly powerful for tasks like dynamic memory allocation,
passing functions as arguments, and working with complex data
structures. However, if not used carefully, they can also lead to bugs
like segmentation faults or memory leaks.
7) What is Array of Pointer?
Ans:- Pointer variables in programming languages like C, C++, and
others are variables that store memory addresses as their values.
Instead of directly storing data, they store the location (address) of
where data is stored in memory. They allow you to indirectly access
and manipulate data by referring to its memory address. This can be
particularly powerful for tasks like dynamic memory allocation,
passing functions as arguments, and working with complex data
structures. However, if not used carefully, they can also lead to bugs
like segmentation faults or memory leaks.
8) Explain Recursive Function in details?
Ans:- A recursive function is a function that calls itself within its
definition. This technique allows a function to solve a problem by
breaking it down into smaller instances of the same problem. A
recursive function typically has two parts: a base case that specifies
when the recursion should stop, and a recursive case that defines
how the function calls itself with modified arguments to move
towards the base case.

When a recursive function is called, it keeps creating new instances


of itself until it reaches the base case. Once the base case is met, the
function starts returning values back through the chain of recursive
calls, eventually providing the final result when all the recursive calls
are resolved.
9) Exphin Function overloading Method?
Ans:- Function overloading refers to the ability to create multiple
functions in a class with the same name but different parameters.
This allows you to perform different actions based on the number or
type of arguments provided when calling the function. In Python,
function overloading isn't directly supported due to its dynamic
typing nature, but you can achieve similar behavior using default
parameter values or variable argument lists.
10) what is Inline Function with example?
Ans:- An inline function is a function that the compiler treats as a
suggestion to insert the function's complete code in place of the
function call, instead of executing a separate function call.
Ex:- #include <iostream>
/ Inline function definition
inline int add(int a, int b) {
return a + b;

int main() {

int x = 5, y = 10;

/ Inline function call


int result = add(x, y);
std::cout << "Result: " << result <<
std::endl; return 0;

}
11) Different Between class and object?
Ans:- Class:- In object-oriented programming, a class is a blueprint or
template for creating objects. It defines the properties (attributes)
and behaviors (methods) that objects of that class will have. Think of
a class as a general description or a set of instructions for creating
objects with specific characteristics and behaviors.

Object:- An object, on the other hand, is an instance of a class. When


you create an object, you're creating a specific instance based on the
blueprint provided by the class. Each object has its own unique data
and can perform actions according to the methods defined in its class.
Essentially, a class is the plan, and an object is the actual thing
created based on that plan
12) what is constructor? & Explain any one type
(Default, @py, Parameterized Constructor
Ans:- An "constructor" in programming is a special type of
method/function in object-oriented languages like Python, Java, C+
+, etc., used to initialize objects. It's called when an object of a class
is created and allocates memory for the object.

Default Constructor: This constructor doesn't take any parameters.


It's automatically called when an object is instantiated without
any arguments. It initializes the object with default values or
performs default actions.

Parameterized Constructor: This type of constructor accepts parameters


at the time of object creation. It allows you to initialize an object with
specific values passed as arguments during instantiation
13) what is Friend class with example?
Ans:- object-oriented programming, a friend class in C++ is a class
that is granted special access to the private and protected members
of another class. This allows the friend class to access and modify the
private or protected members of the class it's friends with.

Ex:- #include <iostream>


class A {
private:
int privateVar;
public:
A() : privateVar(0) {}
/ Declaring class B as a friend
class friend class B;

};
class B {
public:
void modifyPrivateVar(A &obj, int val) {
obj.privateVar = val; // Accessing privateVar in class
A } };

int main() {
return 0;
}
14) What is Inheritance? Explain their types
Ans:- Inheritance in programming refers to the mechanism by which
a new class can inherit properties, methods, and behaviors from an
existing class. This helps in creating a hierarchy and facilitates
reusability.

There are several types of inheritance:


1.Single Inheritance: A class inherits from only one base class.
2.Multiple Inheritance: A class inherits from more than one base
class. However, multiple inheritance can lead to complexities and
ambiguity.

3.Multilevel Inheritance: In this type, a derived class inherits from a


base class, and another class inherits from this derived class,
forming a hierarchical chain.

4.Hierarchical Inheritance: Multiple classes are derived from a


single base class.

5.Hybrid Inheritance: It's a combination of two or more types


of inheritance
15) What is Virtual Base class with exmple?
Ans:- In C++, a virtual base class is a class that's designed to be inherited
virtually by other classes. This means that if a class inherits from a virtual base
class, any further derived classes that also inherit from that base class will share
a single instance of the base class. This avoids the "diamond problem" where
multiple paths in a class hierarchy lead to multiple instances of the same base
class.

Ex:- class A {

public:

int dataA; };

class B : virtual public A {

public:

int dataB; };

class C : virtual public A {

public:

int dataC; };

class D : public B, public C {

public:

int dataD; };

int main() {

D obj;

obj.dataA = 10; // Accessing dataA through


obj return 0;

}
16) Explain Operator Overloading with example?
Ans:- Operator overloading allows operators like +, -, *, /, etc., to be
used with user-defined types. For instance, you can define how the '+'
operator works for your custom class objects.

Ex:- class Vector:


def _init_(self, x, y):
self.x = x
self.y = y
def _add_(self, other):
return Vector(self.x + other.x, self.y + other.y)
# Usage
v1 = Vector(1, 2)
v2 = Vector(3, 4)
result = v1 + v2 # This triggers the overloaded '+' operator
print(f"Resultant vector: ({result.x}, {result.y})")
17) Explain Operator Overloading using friend function.
Ans:- Operator overloading allows operators, like +, -, *, etc., to be
used with user-defined types. When using a friend function for
operator overloading, the function is declared as a friend inside the
class and implemented separately. This allows the function to access
the private members of the class directly, granting it special
privileges to work with those members. For instance, if you want to
overload the + operator for a custom class MyClass, you'd declare a
friend function inside the class:
18) What is Virtual Function with example.
Ans:- In object-oriented programming, a virtual function is a function
within a base class that's meant to be overridden in derived classes.
This allows for polymorphic behavior, where a function call is
resolved at runtime based on the actual object type.

For instance, consider a base class Shape with a virtual function


calculateArea(). Derived classes like Circle and Rectangle override this
function to provide their own implementations of area calculations
based on their specific shapes. This enables calling calculateArea() on
a Shape object, and the appropriate overridden method for the
specific shape will be executed
19) How to handled Exception in CPP
Ans:- In C++, exceptions are handled using try, catch, and throw
blocks. Use try to enclose the code that might throw an exception
and catch to handle specific types of exceptions. If an exception
occurs within the try block, it's caught by an appropriate catch block.
Use throw to raise an exception based on certain conditions or errors
encountered in your code.
20) what is File system ? & write a Read & write functions
Ans:- A file system is a way of organizing and storing files on a
computer's storage devices like hard drives, SSDs, or flash drives. It
includes structures and algorithms for managing

Ex:- # Read function


def read_file(file_name):
try:
with open(file_name, 'r') as file:
content = file.read()
return content
except FileNotFoundError:
return "File not found."
# Write function
def write_file(file_name, content):
try:
with open(file_name, 'w') as file:
file.write(content)
return "Write successful."
except Exception as e:
return f"Error writing to file: {e}"
Q21) Explain the call by reference and Call by vadue or Explain the
Pointer as Function argument.
Ans :-
call by value
The value of actual argument is copied into farmal argument if any
changes in the Formal argument does not effect actual argument it is
called Function call by value
Fott For Example: Swapping of two numbers using call by value
method.
#include <iostream.h>
#include <conio.h)
Void swapcint, int);
void main
int abi
cout<<"Enter 2 Number:
cin >> abi
cout<< in Before swapping assecake
"Itb"<bi
Cout swap (a,b);
getchca
void swap(int x, int y) ६
int temp;
temp=x;
Y=temp;
cout<<" in After swapping ox
<<4;

Call by reference:
Function call by reference meante while calling a of actual Function
the sailue argument is copled inte Formal argument and any changes
in Formal argument also changes andaal arguement because both are
seferes to same memory Location.

Q21)what is inline Function explain with exumple . is Inline Function


are used in program. Ans
to Ganve is this is appreciable wh appreciable when Function. is called
many times however every. Limes Function is called it take lot of extra
time For executing Sexies of operation such as jumping. to a Function
copy actual argument into Formal argument, saving the value of local
variables on stack and returning to the calling Function. iii) when
Function is small this extra time become ovenhade. C++ provide
solution to call such small function by giving the feature called inline
Function- Inline function are those whose function body is insented
inplace of the Function call statement during the compilation
statement.
Syntax:
inline acturatype Function name (Rimeters).
{
//Function code
}
There are some situations where inlines function cannot work.
1) For Function contain laoping Statement, switch and yoto statement
2) For Function not returning value.
3) if function contain static variabi then it cannot be declares. as
inline.
4) if function is recursive.
For example:
#include< lostream.h)
#include <conio.h)
{
inline int cube()
{
return(1*1*1);
}
Void main
{
clrscreen
cout<<" volumn of cube:";
couter koly cube (3);
}
getchchar ();
}
22) what is Function explain recursive Function.
Function is a subprogram. Function is a self-contained block of
Statement which perform a specific lask any type of function take
number of argument and after Processing return only one resut.
Recursive Function is the technique of making Function call itself. this
technique Provide a way to break complicated Problem down.
into simple program which are easier to solve Recursive Function is
required for Problem concerning data structure and advance
algorithm.
Disadvantages of Recursive function.
1)It take lot of stack spare compose to an iterative process.
2) It uses more processor time.
23)what is class 2. How to define class with example.
Ans. class is a single unit. class is a user define datatype which hold it's
own datamembers and member Functions, which can be access and
used by creating an instance of that class.
class is like a blueprint for an objed, and it has ability to supporting
the concept of inheritance class is defined using class keyword
followed by the name of class. body of the class is defined inside a
curly braces and terminate by semicolon at the end.
#syntax
class class name
{
llaccess specifiers, (can be Private Public Protected 11 Data member's
(variables used)
Il member Functions (methods, };
}
Q23) A what is constructor 9. Explain the tipes of constructor.
Ans. Constructor is a special member Function.
which is used to initialized object when it is created.
Constructor is a special because it's name is same as class name.
constructor call atomatically whenever the object of the class is
created. it is called constructor because it construct the value of data
members of the class.
There are three types of construcher
1) Default constructor:
Default constructor is that will either have no parameters. 07 all the
parameters have default Vadues
2) Parameterized construct... The constructor that can take argument
are called Panameterized constructor. By using parameterized
constructor to initialized obicet by different values.
3) copy constructor:
one object is initiated by another object value is called copy
constructor.
23) write a short note on Friend Function
Ans. Friend Function is declared by Friend Keyword. It is a nan
member Function that can be access and manipulate private Land
Protected member of a dass. The Friend Function is defined like
normal Function that is outside class it is a normal Function
Syntax:
class class name allan lai
{
__________
_______ ___
Friend return type Function name Caugin
}
Friend Function usefull in following Situations
Function operating on object of of two different classes. This is ideal
situation where friend Function can be used to bridge. between two
classes.
1)Friend function can be used to increase vessalality of overloaded
2) Sometimes a friend allows mare clear syntax for calling the function

24) what is inheritance ? Explain the types of Inheritance


Ans. Inheritance is the mechanism of driving new dass From an
existing. class, it means it is a process of created new dass, From an
old dass new class is known as derived dass and old or existing duas is
known as base class. Derived class inherits all the properties or somes
properties from base class depend on the inheritance level on of
Following are the types of inheritance:
1) Single Inheritance:
Derivation of new class from only one single base class it is known. as
single inheritance.
A Buse class.
Derived dass
2) Multiple Inheritance It is a when new class is derive From more
than one base classes. then it is called multiple inheritance.
3) Multilevel Inheritance In this type of inheritance derived class is
created fOr
Devived class
4) Hierarchical Inhentunce: Mechanism of driving several classes from
Single base class it is called hierarchical inheritance..
5) Hybrid Inheritance
Hybrid inheritance is implemented by combining more than one tipe
of inheritance, for example, combithing hitaenchical inheritance and
Multiple inheritance

25)Explain the visibility mode of innesitance and benefits of


inheritance
Ans Private visibility mode
class Brixate B
{
Il members of derived clasa
}
If we derive a subdass Faum Private base dass then both public
members. and protected members of base clas will become Private in
the derived class
2) Protected visibility made.
Class: Protected B
{
Ilmembers of derived class=
}
If we derive a subel subclass from Prateded base class then both
public members and protected members of the base class will become
Pistedad in the derived class
3) Public visibility Mode:-
class D: Public If we derive a subclass From public base class then
public members of the base class swill become perblic in the derived
class and protected members. of the base class will become
Protectcated in the base class.
Benefits of inheritance:
There are many important benefits that can be derived From the
proper use of inheritance.
They are cade reuse, easy of code maintainance, and extension and
reduction in the time to market
Benefits of inheritance in Following Situations:
code sharing can occure at several lev For example, At higher level
users. on projects con use the same class of the lawet lower level code
can be Share by two or more within a projed

You might also like