- What is C++?
C++ is an object-oriented programming language created by Bjarne Stroustrup. It was released in 1985.
C++ is a superset of C with the major addition of classes in C language.
Initially, Stroustrup called the new language “C with classes”. However, after sometime the name was changed to C++. The idea of C++ comes from the C increment operator ++.
2. What are the advantages of C++?
- C++ doesn’t only maintains all aspects from C language, it also simplifies memory management and adds several features like:
- C++ is a highly portable language means that the software developed using C++ language can run on any platform.
- C++ is an object-oriented programming language which includes the concepts such as classes, objects, inheritance, polymorphism, abstraction.
- C++ has the concept of inheritance. Through inheritance, one can eliminate the redundant code and can reuse the existing classes.
- Data hiding helps the programmer to build secure programs so that the program cannot be attacked by the invaders.
- Message passing is a technique used for communication between the objects
C++ contains a rich function library.
3. What is a class?
The class is a user-defined data type. The class is declared with the keyword class. The class contains the data members, and member functions whose access is defined by the three modifiers are private, public and protected. The class defines the type definition of the category of things. It defines a datatype, but it does not define the data it just specifies the structure of data.
You can create N number of objects from a class.
4. What are the various OOPs concepts in C++?
The various OOPS concepts in C++ are:
Class:
The class is a user-defined data type which defines its properties and its functions. For example, Human being is a class. The body parts of a human being are its properties, and the actions performed by the body parts are known as functions. The class does not occupy any memory space. Therefore, we can say that the class is the only logical representation of the data.
The syntax of declaring the class:
__________________________
class student
{
//data members;
//Member functions
}
_____________________
Object:
An object is a run-time entity. An object is the instance of the class. An object can represent a person, place or any other item. An object can operate on both data members and member functions. The class does not occupy any memory space. When an object is created using a new keyword, then space is allocated for the variable in a heap, and the starting address is stored in the stack memory. When an object is created without a new keyword, then space is not allocated in the heap memory, and the object contains the null value in the stack.
______________________
class Student
{
//data members;
//Member functions
}
_________________
The syntax for declaring the object:
_____________________________
Student s = new Student();
______________________
Inheritance:
Inheritance provides reusability. Reusability means that one can use the functionalities of the existing class. It eliminates the redundancy of code. Inheritance is a technique of deriving a new class from the old class. The old class is known as the base class, and the new class is known as derived class.
Syntax:
__________________________________________
class derived_class :: visibility-mode base_class;
________________________________
Note: The visibility-mode can be public, private, protected.
Encapsulation:
Encapsulation is a technique of wrapping the data members and member functions in a single unit. It binds the data within a class, and no outside method can access the data. If the data member is private, then the member function can only access the data.
Abstraction:
Abstraction is a technique of showing only essential details without representing the implementation details. If the members are defined with a public keyword, then the members are accessible outside also. If the members are defined with a private keyword, then the members are not accessible by the outside methods.
Data binding:
Data binding is a process of binding the application UI and business logic. Any change made in the business logic will reflect directly to the application UI.
Polymorphism:
Polymorphism means multiple forms. Polymorphism means having more than one function with the same name but with different functionalities. Polymorphism is of two types:
- Static polymorphism is also known as early binding.
- Dynamic polymorphism is also known as late binding.
5. What is the difference between reference and pointer?
Following are the differences between reference and pointer:
Reference | Pointer |
Reference behaves like an alias for an existing variable, i.e., it is a temporary variable. | The pointer is a variable which stores the address of a variable. |
Reference variable does not require any indirection operator to access the value. A reference variable can be used directly to access the value. | Pointer variable requires an indirection operator to access the value of a variable. |
Once the reference variable is assigned, then it cannot be reassigned with different address values. | The pointer variable is an independent variable means that it can be reassigned to point to different objects. |
A null value cannot be assigned to the reference variable. | A null value can be assigned to the reference variable. |
It is necessary to initialize the variable at the time of declaration. | It is not necessary to initialize the variable at the time of declaration. |
6. Define namespace in C++?
- The namespace is a logical division of the code which is designed to stop the naming conflict.
- The namespace defines the scope where the identifiers such as variables, class, functions are declared.
- The main purpose of using namespace in C++ is to remove the ambiguity. Ambiquity occurs when the different task occurs with the same name.
- For example: if there are two functions exist with the same name such as add(). In order to prevent this ambiguity, the namespace is used. Functions are declared in different namespaces.
- C++ consists of a standard namespace, i.e., std which contains inbuilt classes and functions. So, by using the statement “using namespace std;” includes the namespace “std” in our program.
- Syntax of namespace:
_________________________
namespace namespace_name { //body of namespace; } ____________________
Syntax of accessing the namespace variable:
__________________________
namespace_name::member_name; ___________________
Let’s understand this through an example:
________________________________
#include <iostream>
using namespace std;
namespace addition
{
int a=5;
int b=5;
int add()
{
return(a+b);
}
}
int main() {
int result;
result=addition::add();
cout<<result;
return 0;
}
___________________________-
Output:
________
10 _____ 7. Define token in C++? A token in C++ can be a keyword, identifier, literal, constant and symbol.
8. Who was the creator of C++?
Bjarne Stroustrup.
9. Define ‘std’?
Std is the default namespace standard used in C++.
10. Which programming language’s unsatisfactory performance led to the discovery of C++?
C++was discovered in order to cope with the disadvantages of C.
11. How delete [] is different from delete?
Delete is used to release a unit of memory, delete[] is used to release an array.
12. What is the full form of STL in C++?
STL stands for Standard Template Library.
13. What are the C++ access specifiers?
The access specifiers are used to define how to functions and variables can be accessed outside the class.
There are three types of access specifiers:
- Private: Functions and variables declared as private can be accessed only within the same class, and they cannot be accessed outside the class they are declared.
- Public: Functions and variables declared under public can be accessed from anywhere.
- Protected: Functions and variables declared as protected cannot be accessed outside the class except a child class. This specifier is generally used in inheritance.
14. What is an object?
The Object is the instance of a class. A class provides a blueprint for objects. So you can create an object from a class. The objects of a class are declared with the same sort of declaration that we declare variables of basic types.
15. What is Object Oriented Programming (OOP)?
OOP is a methodology or paradigm that provides many concepts. The basic concepts of Object Oriented Programming are given below:
Classes and Objects: Classes are used to specify the structure of the data. They define the data type. You can create any number of objects from a class. Objects are the instances of classes.
Encapsulation: Encapsulation is a mechanism which binds the data and associated operations together and thus hides the data from the outside world. Encapsulation is also known as data hiding. In C++, It is achieved using the access specifiers, i.e., public, private and protected.
Abstraction: Abstraction is used to hide the internal implementations and show only the necessary details to the outer world. Data abstraction is implemented using interfaces and abstract classes in C++.
Some people confused about Encapsulation and abstraction, but they both are different.
Inheritance: Inheritance is used to inherit the property of one class into another class. It facilitates you to define one class in term of another class.
16. What is the difference between new() and malloc()?
- new() is a preprocessor while malloc() is a function.
- There is no need to allocate the memory while using “new” but in malloc() you have to use sizeof().
- “new” initializes the new memory to 0 while malloc() gives random value in the newly allotted memory location.
- The new() operator allocates the memory and calls the constructor for the object initialization and malloc() function allocates the memory but does not call the constructor for the object initialization.
- The new() operator is faster than the malloc() function as operator is faster than the function.
17. What is the difference between an array and a list?
- An Array is a collection of homogeneous elements while a list is a collection of heterogeneous elements.
- Array memory allocation is static and continuous while List memory allocation is dynamic and random.
- In Array, users don’t need to keep in track of next memory allocation while In the list, the user has to keep in track of next location where memory is allocated.
18. Define friend function?
Friend function acts as a friend of the class. It can access the private and protected members of the class. The friend function is not a member of the class, but it must be listed in the class definition. The non-member function cannot access the private data of the class. Sometimes, it is necessary for the non-member function to access the data. The friend function is a non-member function and has the ability to access the private data of the class.
To make an outside function friendly to the class, we need to declare the function as a friend of the class as shown below:
_____________________
class sample
{
// data members;
public:
friend void abc(void);
};
___________________
Following are the characteristics of a friend function:
- The friend function is not in the scope of the class in which it has been declared.
- Since it is not in the scope of the class, so it cannot be called by using the object of the class. Therefore, friend function can be invoked like a normal function.
- A friend function cannot access the private members directly, it has to use an object name and dot operator with each member name.
- Friend function uses objects as arguments.
Let’s understand this through an example:
_________________________
#include <iostream>
using namespace std;
class Addition
{
int a=5;
int b=6;
public:
friend int add(Addition a1)
{
return(a1.a+a1.b);
}
};
int main()
{
int result;
Addition a1;
result=add(a1);
cout<<result;
return 0;
}
____________________
Output:
______
11
______
19. What are the methods of exporting a function from a DLL?
There are two ways:
- By using the DLL’s type library.
- Taking a reference to the function from the DLL instance.
20. What is a virtual function?
- A virtual function is used to replace the implementation provided by the base class. The replacement is always called whenever the object in question is actually of the derived class, even if the object is accessed by a base pointer rather than a derived pointer.
- A virtual function is a member function which is present in the base class and redefined by the derived class.
- When we use the same function name in both base and derived class, the function in base class is declared with a keyword virtual.
- When the function is made virtual, then C++ determines at run-time which function is to be called based on the type of the object pointed by the base class pointer. Thus, by making the base class pointer to point different objects, we can execute different versions of the virtual functions.
Rules of a virtual function:
- The virtual functions should be a member of some class.
- The virtual function cannot be a static member.
- Virtual functions are called by using the object pointer.
- It can be a friend of another class.
- C++ does not contain virtual constructors but can have a virtual destructor.
21. What are the different data types present in C++?
The 4 data types in C++ are given below:
- Primitive Datatype(basic datatype). Example- char, short, int, float, long, double, bool, etc.
- Derived datatype. Example- array, pointer, etc.
- Enumeration. Example- enum
- User-defined data types. Example- structure, class, etc.
22. What is the difference between C and C++?
The main difference between C and C++ are provided in the table below:
C | C++ |
C is a procedure-oriented programming language. | C++ is an object-oriented programming language. |
C does not support data hiding. | Data is hidden by encapsulation to ensure that data structures and operators are used as intended. |
C is a subset of C++ | C++ is a superset of C. |
Function and operator overloading are not supported in C | Function and operator overloading is supported in C++ |
Namespace features are not present in C | Namespace is used by C++, which avoids name collisions. |
Functions can not be defined inside structures. | Functions can be defined inside structures. |
calloc() and malloc() functions are used for memory allocation and free() function is used for memory deallocation. | new operator is used for memory allocation and deletes operator is used for memory deallocation. |
23. What are class and object in C++?
A class is a user-defined data type that has data members and member functions. Data members are the data variables and member functions are the functions that are used to perform operations on these variables.
An object is an instance of a class. Since a class is a user-defined data type so an object can also be called a variable of that data type.
A class is defined as-
________________________
class A{
private:
int data;
public:
void fun(){
}
};
__________________
24. What is the difference between struct and class?
In C++ a structure is the same as a class except for a few differences like security. The difference between struct and class are given below:
Structure | Class |
Members of the structure are public by default. | Members of the class are private by default. |
When deriving a struct from a class/struct, default access specifiers for base class/struct are public. | When deriving a class, default access specifiers are private. |
25. What is operator overloading?
Operator Overloading is a very essential element to perform the operations on user-defined data types. By operator overloading we can modify the default meaning to the operators like +, -, *, /, <=, etc.
For example –
The following code is for adding two complex number using operator overloading-
_____________________________
class complex{
private:
float r, i;
public:
complex(float r, float i){
this->r=r;
this->i=i;
}
complex(){}
void displaydata(){
cout<<”real part = “<<r<<endl;
cout<<”imaginary part = “<<i<<endl;
}
complex operator+(complex c){
return complex(r+c.r, i+c.i);
}
};
int main(){
complex a(2,3);
complex b(3,4);
complex c=a+b;
c.displaydata();
return 0;
}
_________________________
26. Explain constructor in C++?
The constructor is a member function that is executed automatically whenever an object is created. Constructors have the same name as the class of which they are members so that compiler knows that the member function is a constructor. And no return type is used for constructors.
Example-
______________________
class A{
private:
int val;
public:
A(int x){ //one argument constructor
val=x;
}
A(){ //zero argument constructor
}
}
int main(){
A a(3);
return 0;
}
___________________
27. What is polymorphism in C++?
Polymorphism in simple means having many forms. Its behavior is different in different situations. And this occurs when we have multiple classes that are related to each other by inheritance.
For example, think of a base class called a car that has a method called car brand(). Derived classes of cars could be Mercedes, BMW, Audi – And they also have their own implementation of a cars
The two types of polymorphism in c++ are:
- Compile Time Polymorphism
- Runtime Polymorphism
28. What do you know about friend class and friend function?
A friend class can access private, protected, and public members of other classes in which it is declared as friends.
Like friend class, friend function can also access private, protected, and public members. But, Friend functions are not member functions.
For example –
___________________________________
class A{
private:
int data_a;
public:
A(int x){
data_a=x;
}
friend int fun(A, B);
}
class B{
private:
int data_b;
public:
A(int x){
data_b=x;
}
friend int fun(A, B);
}
int fun(A a, B b){
return a.data_a+b.data_b;
}
int main(){
A a(10);
B b(20);
cout<<fun(a,b)<<endl;
return 0;
}
_______________________________
Here we can access the private data of class A and class B.
29. What are the C++ access specifiers?
In C++ there are the following access specifiers:
Public: All data members and member functions are accessible outside the class.
Protected: All data members and member functions are accessible inside the class and to the derived class.
Private: All data members and member functions are not accessible outside the class.
30. Define inline function?
If a function is inline, the compiler places a copy of the code of that function at each point where the function is called at compile time. One of the important advantages of using an inline function is that it eliminates the function calling overhead of a traditional function.
31. What do you mean by abstraction in C++?
Abstraction is the process of showing the essential details to the user and hiding the details which we don’t want to show to the user or hiding the details which are irrelevant to a particular user.
32. What do you mean by call by value and call by reference?
In call by value method, we pass a copy of the parameter is passed to the functions. For these copied values a new memory is assigned and changes made to these values do not reflect the variable in the main function.
In call by reference method, we pass the address of the variable and the address is used to access the actual argument used in the function call. So changes made in the parameter alter the passing argument
33. Is deconstructor overloading possible? If yes then explain and if no then why?
No destructor overloading is not possible. Destructors take no arguments, so there’s only one way to destroy an object. That’s the reason destructor overloading is not possible.
34. What is an abstract class and when do you use it?
A class is called an abstract class whose objects can never be created. Such a class exists as a parent for the derived classes. We can make a class abstract by placing a pure virtual function in the class.
35. What are destructors in C++?
A constructor is automatically called when an object is first created. Similarly when an object is destroyed a function called destructor automatically gets called. A destructor has the same name as the constructor (which is the same as the class name) but is preceded by a tilde.
Example:
______________________________________
class A{
private:
int val;
public:
A(int x){
val=x;
}
A(){
}
~A(){ //destructor
}
}
int main(){
A a(3);
return 0;
}
_______________________________
36. What is a copy constructor?
A copy constructor is a member function that initializes an object using another object of the same class.
Example-
_________________________________
class A{
int x,y;
A(int x, int y){
this->x=x;
this->y=y;
}
};
int main(){
A a1(2,3);
A a2=a1; //default copy constructor is called
return 0;
}
__________________________
We can define our copy constructor. If we don’t define a copy constructor then the default copy constructor is called.
37. What is the difference between shallow copy and deep copy?
The difference between shallow copy and a deep copy is given below:
Shallow Copy | Deep Copy |
Shallow copy stores the references of objects to the original memory address. | Deep copy makes a new and separate copy of an entire object with its unique memory address. |
Shallow copy is faster. | Deep copy is comparatively slower |
Shallow copy reflects changes made to the new/copied object in the original object. | Deep copy doesn’t reflect changes made to the new/copied object in the original object |
38. Can we call a virtual function from a constructor?
Yes, we can call a virtual function from a constructor. But the behavior is a little different in this case. When a virtual function is called, the virtual call is resolved at runtime. It is always the member function of the current class that gets called. That is the virtual machine doesn’t work within the constructor.
For example-
_______________________________________
class base{
private:
int value;
public:
base(int x){
value=x;
}
virtual void fun(){
}
}
class derived{
private:
int a;
public:
derived(int x, int y):base(x){
base *b;
b=this;
b->fun(); //calls derived::fun()
}
void fun(){
cout<<”fun inside derived class”<<endl;
}
}
______________________________
39. What are void pointers?
A void pointer is a pointer which is having no datatype associated with it. It can hold addresses of any type.
For example-
_____________________________
void *ptr;
char *str;
p=str; // no error
str=p; // error because of type mismatch
_______________________
We can assign a pointer of any type to a void pointer but the reverse is not true unless you typecast it as
___________________________________________
str=(char*) ptr;
_____________________
40. What is this pointer in C++?
The member functions of every object have a pointer named this, which points to the object itself. The value of this is set to the address of the object for which it is called. It can be used to access the data in the object it points to.
Example
___________________________________
class A{
private:
int value;
public:
void setvalue(int x){
this->value=x;
}
};
int main(){
A a;
a.setvalue(5);
return 0;
}
____________________________
41. What are access modifiers?
You use access modifiers to define accessibility for the class members. It defines how to access the members of the class outside the class scope.
There are three types of access modifiers:
- Private
- Public
- Protected
42. Difference between equal to (==) and assignment operator(=)?
The equal to operator == checks whether two values are equal or not. If equal, then it’s true; otherwise, it will return false.
The assignment operator = allots the value of the right-side expression to the left operand.
43. Discuss the difference between prefix and postfix?
In prefix (++i), first, it increments the value, and then it assigns the value to the expression.
In postfix (i++), it assigns the value to the expression, and then it increments the variable’s value.
44. What is the C++ OOPs concept?
OOPs concept in C++:
- Object
- Class
- Inheritance
- Polymorphism
- Encapsulation
- Abstraction
Object: Anything that exists physically in the real world is called an object.
Class: The collection of objects is called class.
Inheritance: Properties of parent class inherited into child class is known as inheritance.
Polymorphism: It is the ability to exist in more than one form.
Encapsulation: Binding of code and data together into a single unit.
Abstraction: Hiding internal details and showing functionality to the user.
45. hat among these is used to return the number of characters in the string?
Size
Length
Both size and length
Name
3 – Both size and length are used to return the number of characters in the string
46. What is a friend function?
You can define a friend function as a function that can access private, public and protect members of the class. You declare the friend function with the help of the friend keyword. You declare this function inside the class.
47. Which of the following is not a member of a class?
- Static function
- Virtual function
- Const function
- Friend function
- 4 – Among the following, friend function is not a member of the class.
48. What should be the correct statement about string objects in C++?
- String objects should necessarily be terminated by a null character
- String objects have a static size
- String objects have a dynamic size
- String objects use extra memory than required
- 3 – String objects have a dynamic size.
49. Define storage class in C++? Name some?
Storage class in C++ specifically resemble life or even the scope of symbols, including the variables, functions, etc. Some of the storage class names in C++ include mutable, auto, static, extern, register, etc.
50. Can we have a recursive inline function in C++?
Even though it is possible to call an inline function from within itself in C++, the compiler may not generate the inline code. This is so because the compiler won’t determine the depth of the recursion at the compile time.
Nonetheless, a compiler with a good optimizer is able to inline recursive calls until some depth is fixed at compile-time and insert non-recursive calls at compile time for the cases when the actual depth exceeds run time.
- An Introduction of GitLab Duo - December 22, 2024
- Best Hospitals for affordable surgery for medical tourism - December 20, 2024
- Top Global Medical Tourism Companies in the World - December 20, 2024