Java Point
Java Point
A list of top frequently asked C++ interview questions and answers are given below.
1) What is C++?
C++ is an object-oriented programming language created by Bjarne Stroustrup. It was
released in 1985.
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
++.
o C++ is a highly portable language means that the software developed using C++
language can run on any platform.
o C++ is an object-oriented programming language which includes the concepts
such as classes, objects, inheritance, polymorphism, abstraction.
o C++ has the concept of inheritance. Through inheritance, one can eliminate the
redundant code and can reuse the existing classes.
o Data hiding helps the programmer to build secure programs so that the program
cannot be attacked by the invaders.
o Message passing is a technique used for communication between the objects.
o C++ contains a rich function library.
C C++
In C language, data and functions are the free entities. In the C++ language, both da
together in the form of a proj
C does not support the data hiding. Therefore, the data can be C++ supports data hiding. Th
used by the outside world. the outside world.
C supports neither function nor operator overloading. C++ supports both function a
In C, the function cannot be implemented inside the structures. In the C++, the function can
Reference variables are not supported in C language. C++ supports the reference v
C language does not support the virtual and friend functions. C++ supports both virtual an
In C, scanf() and printf() are mainly used for input/output. C++ mainly uses stream cin a
operations.
Reference Pointer
Reference behaves like an alias for an existing variable, i.e., it is a The pointer is a va
temporary variable. variable.
Reference variable does not require any indirection operator to access the Pointer variable req
value. A reference variable can be used directly to access the value. the value of a varia
Once the reference variable is assigned, then it cannot be reassigned with The pointer variabl
different address values. it can be reassigne
A null value cannot be assigned to the reference variable. A null value can be
o 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.
1. class student
2. {
3. //data members;
4. //Member functions
5. }
o 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.
1. class Student
2. {
3. //data members;
4. //Member functions
5. }
o 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
o Encapsulation:
o Abstraction:
o 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.
o 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:
o Runtime polymorphism
1. #include <iostream>
2. using namespace std;
3. class Base
4. {
5. public:
6. virtual void show()
7. {
8. cout<<"javaTpoint";
9. }
10. };
11. class Derived:public Base
12. {
13. public:
14. void show()
15. {
16. cout<<"javaTpoint tutorial";
17. }
18. };
19.
20. int main()
21. {
22. Base* b;
23. Derived d;
24. b=&d;
25. b->show();
26. return 0;
27. }
Output:
javaTpoint tutorial
o Compile time polymorphism
Method overloading: Method overloading is a technique which allows you to have more
than one function with the same function name but with different functionality.
1. #include <iostream>
2. using namespace std;
3. class Multiply
4. {
5. public:
6. int mul(int a,int b)
7. {
8. return(a*b);
9. }
10. int mul(int a,int b,int c)
11. {
12. return(a*b*c);
13. }
14. };
15. int main()
16. {
17. Multiply multi;
18. int res1,res2;
19. res1=multi.mul(2,3);
20. res2=multi.mul(2,3,4);
21. cout<<"\n";
22. cout<<res1;
23. cout<<"\n";
24. cout<<res2;
25. return 0;
26. }
Output:
6
24
o In the above example, mul() is an overloaded function with the different number
of parameters.
1. namespace namespace_name
2. {
3. //body of namespace;
4. }
1. namespace_name::member_name;
1. #include <iostream>
2. using namespace std;
3. namespace addition
4. {
5. int a=5;
6. int b=5;
7. int add()
8. {
9. return(a+b);
10. }
11. }
12.
13. int main() {
14. int result;
15. result=addition::add();
16. cout<<result;
17. return 0;
18. }
Output:
10
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int a[5]={1,2,3,4,5};
6. int *ptr;
7. ptr=&a[0];
8. cout<<"Value of *ptr is : "<<*ptr<<"\n";
9. cout<<"Value of *++ptr : "<<*++ptr;
10. return 0;
11. }
Output:
Value of *ptr is : 1
Value of *++ptr : 2
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int a[5]={1,2,3,4,5};
6. int *ptr;
7. ptr=&a[0];
8. cout<<"Value of *ptr is : "<<*ptr<<"\n";
9. cout<<"Value of *ptr++ : "<<*ptr++;
10. return 0;
11. }
Output:
Value of *ptr is : 1
Value of *ptr++ : 1
o Subtracting a pointer from another pointer: When two pointers pointing to
the members of an array are subtracted, then the number of elements present
between the two members are returned.
o 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.
o Public: Functions and variables declared under public can be accessed from
anywhere.
o Protected: Functions and variables declared as protected cannot be accessed
outside the class except a child class. This specifier is generally used in
inheritance.
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.
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.
1. class sample
2. {
3. // data members;
4. public:
5. friend void abc(void);
6. };
o The friend function is not in the scope of the class in which it has been declared.
o 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.
o A friend function cannot access the private members directly, it has to use an
object name and dot operator with each member name.
o Friend function uses objects as arguments.
1. #include <iostream>
2. using namespace std;
3. class Addition
4. {
5. int a=5;
6. int b=6;
7. public:
8. friend int add(Addition a1)
9. {
10. return(a1.a+a1.b);
11. }
12. };
13. int main()
14. {
15. int result;
16. Addition a1;
17. result=add(a1);
18. cout<<result;
19. return 0;
20. }
Output:
11
1. Never
2. Rarely
3. If you find that the problem domain cannot be accurately modeled any other way.
Rules of destructor:
o Destructors have the same name as class name and it is preceded by tilde.
o It does not contain any argument and no return type.
26) What is an overflow error?
It is a type of arithmetical error. It happens when the result of an arithmetical operation
been greater than the actual space provided by the system.
o Member function
o Non-member function
o Friend function
1. #include<iostream>
2. using namespace std;
3. class Base
4. {
5. public:
6. virtual void show()=0;
7. };
8.
9. class Derived:public Base
10. {
11. public:
12. void show()
13. {
14. cout<<"javaTpoint";
15. }
16. };
17. int main()
18. {
19. Base* b;
20. Derived d;
21. b=&d;
22. b->show();
23. return 0;
24. }
Output:
javaTpoint
36) What is the difference between struct and class?
Structures class
A structure is a user-defined data type which contains variables of The class is a user-defined
dissimilar data types. variables and member fun
The variables of a structure are stored in the stack memory. The variables of a class ar
If access specifier is not specified, then by default the access If access specifier is not s
specifier of the variable is "public". specifier of a variable is "p
The structure does not support the inheritance. The class supports the con
1. template<class T>
2. class classname
3. {
4. // body of class;
5. };
1. #include <iostream>
2. using namespace std;
3. class Base
4. {
5. public:
6. Base()
7. {
8. cout<<"Base constructor is called"<<"\n";
9. }
10. ~Base()
11. {
12. cout<<"Base class object is destroyed"<<"\n";
13. }
14. };
15. class Derived:public Base
16. {
17. public:
18. Derived()
19. {
20. cout<<"Derived class constructor is called"<<"\n";
21. }
22. ~Derived()
23. {
24. cout<<"Derived class object is destroyed"<<"\n";
25. }
26. };
27. int main()
28. {
29. Base* b= new Derived;
30. delete b;
31. return 0;
32.
33. }
Output:
In the above example, delete b will only call the base class destructor due to which
derived class destructor remains undestroyed. This leads to the memory leak.
1. #include <iostream>
2. using namespace std;
3. class Base
4. {
5. public:
6. Base()
7. {
8. cout<<"Base constructor is called"<<"\n";
9. }
10. virtual ~Base()
11. {
12. cout<<"Base class object is destroyed"<<"\n";
13. }
14. };
15. class Derived:public Base
16. {
17. public:
18. Derived()
19. {
20. cout<<"Derived class constructor is called"<<"\n";
21. }
22. ~Derived()
23. {
24. cout<<"Derived class object is destroyed"<<"\n";
25. }
26. };
27. int main()
28. {
29. Base* b= new Derived;
30. delete b;
31. return 0;
32.
33. }
Output:
When we use the virtual destructor, then the derived class destructor is called first, and
then the base class destructor is called.