What Is C++? What Are The Advantages of C++?
What Is C++? What Are The Advantages of C++?
Advantages of C++:
3. Define ‘std’?
Syntax:
// reference variable
Call by Value:
The changes made in the function are never reflected outside the function on
the variable. In short, the original value is never altered in Call by Value.
Call by Reference:
The changes made in the functions can be seen outside the function on the
passed function. In short, the original value is altered in Call by reference.
Passed actual and formal parameters are stored in the same memory
location. Therefore, making Call by Reference a little more memory efficient.
Special Symbols – They have some special meaning and cannot be used for
another purpose; eg: [] () {}, ; * = #
Struct:
Often used for Plain Old Data (POD) structures, or simple data grouping.
Class:
Suitable for complex objects that may include methods, constructors, and
destructors.
Reference:
It can hold or point at a null value and be termed as a nullptr or null pointer
15. What is the difference between virtual functions and pure virtual
functions?
16. What are classes and objects in C++?
A class is a user-defined data type where all the member functions and data
members are tailor-made according to demands and requirements in
addition to which these all can be accessed with the help of an object. To
declare a user-defined data type we use a keyword class.
Multiple inheritances mean that a derived class can inherit two or more
base/parent classes. It is useful when a derived class needs to combine
numerous attributes/contracts and inherit some, or all, of the implementation
from these attributes/contracts. To take a real-life example consider your
Parents where Parent A is your DAD Parent B is your MOM and Chid C is you.
In other words, we can also say that a man can be an employee to someone,
a son of someone, a father of someone, and a husband of someone; this is
how polymorphism can be projected in real life.
Function Overloading
Operator Overloading
Function Overriding
Virtual Function
Function Overloading: When there are multiple functions with the same
name but different parameters then this is known as function overloading.
Operator Overloading: It is basically giving practice of giving a special
meaning to the existing meaning of an operator or in simple terms redefining
the pre-redefined meaning
Run-time polymorphism takes place when functions are invoked during run
time.
Function Overriding: Function overriding occurs when a base class
member function is redefined in a derived class with the same arguments
and return type.
Output:
Example:
Output
Syntax:
~constructor_name(); // tilde sign signifies that it is a destructor
Virtual destructor guarantees that first the derived class’ destructor is called.
Then the base class’s destructor is called to release the space occupied by
both destructors in the inheritance class which saves us from the memory
leak. It is advised to make your destructor virtual whenever your class is
polymorphic.
Pointers are the variables that are used to store the address location of
another variable. Operations that are permitted to a pointer are:
1. Increment/Decrement of a Pointer
32. What do you know about friend class and friend function?
A friend class is a class that can access both the protected and private
variables of the classes where it is declared as a friend.
Overflow Error occurs when the number is too large for the data type to
handle. In simple terms, it is a type of error that is valid for the defined but
exceeds used the defined range where it should coincide/lie.
A scope resolution operator is denoted by a ‘::‘ symbol. Just like its name this
operator resolves the barrier of scope in a program. A scope resolution
operator is used to reference a member function or a global variable out of
their scope furthermore to which it can also access the concealed variable or
function in a program.
1. To access a global variable when there is a local variable with the same
name
4. For namespace
35. What are the C++ access modifiers?
Syntax:
inline data_type function_name()
{
Body;
}
You cannot use an abstract class as a parameter type, a function return type,
or the type of an explicit conversion, nor can you declare an object of an
abstract class. However, it can be used to declare pointers and references to
an abstract class.
40. What are the static data members and static member functions?
The static data member of a class is a normal data member but preceded
with a static keyword. It executes before main() in a program and is
initialized to 0 when the first object of the class is created. It is only visible to
a defined class but its scope is of a lifetime.
Syntax:
The static member function is the member function that is used to access
other static data members or other static member functions. It is also
defined with a static keyword. We can access the static member function
using the class name or class objects.
Syntax:
classname::function name(parameter);
Just like its name, things can change suddenly and unexpectantly; So it is
used to inform the compiler that the value may change anytime. Also, the
volatile keyword prevents the compiler from performing optimization on the
code. It was intended to be used when interfacing with memory-mapped
hardware, signal handlers, and machine code instruction.
Syntax:
Just like its name, the mutable storage class specifier is used only on a class
data member to make it modifiable even though the member is part of an
object declared as const. Static or const, or reference members cannot use
the mutable specifier. When we declare a function as const, this pointer
passed to the function becomes const.
A block scope variable is also known as a local scope variable. A variable that
is defined inside a function (like main) or inside a block (like loops and if
blocks) is a local variable.
The auto keyword may be used to declare a variable with a complex type in a
straightforward fashion. You can use auto to declare a variable if the
initialization phrase contains templates, pointers to functions, references to
members, etc. With type inference capabilities, we can spend less time
having to write out things the compiler already knows. As all the types are
deduced in the compiler phase only, the time for compilation increases
slightly but it does not affect the runtime of the program.
The void keyword, when used as a function return type, indicates that the
function does not return a value. When used as a parameter list for a
function, void indicates that the function takes no parameters. Non-Value
Returning functions are also known as void functions.
48. What is the difference between shallow copy and deep copy?
Just like its name a void pointer is a pointer that is not associated with
anything or with any data type. Nevertheless, a void pointer can hold the
address value of any type and can be converted from one data type to
another.