Object Oriented Programming Flashcards
What is an object?
An object is an instance of a class.
Explain abstraction
Abstraction is a good feature of OOPS, and it shows only the necessary details to the client of an object. Means, it shows only required details for an object, not the inner constructors, of an object. Example – When you want to switch On television, it not necessary to show all the functions of TV. Whatever is required to switch on TV will be showed by using abstract class.
What is the use of default constructor?
- It is a constructor that does not accept any parameters.
- If there is no user-defined constructor for a class, the compiler declares a default parameterless constructor called default constructor.
- It is an inline public member of its class.
- When the compiler uses this constructor to create an object – the constructor will have no constructor initializer and a null body.
Whether static method can use nonstatic members?
False. Static methods can only access the static part of a class.
What are the various types of constructors?
There are three various types of constructors, and they are as follows:
– Default Constructor – With no parameters.
– Parametric Constructor – With Parameters. Create a new instance of a class and also passing arguments simultaneously.
– Copy Constructor – Which creates a new object as a copy of an existing object.
Explain deep copy and a shallow copy.
a. Deep copy :
It involves using the contents of one object to create another instance of the same class. Here, the two objects may contain the same information but the target object will have its own buffers and resources. The destruction of either object will not affect the remaining objects.
b. Shallow copy :
It involves copying the contents of one object into another instance of the same class. This creates a mirror image. The two objects share the same externally contained contents of the other object to be unpredictable.This happens because of the straight copying of references and pointers.
What is an abstract class?
An abstract class is a class which cannot be instantiated. Creation of an object is not possible with an abstract class, but it can be inherited. An abstract class can contain only Abstract method.
What are all the operators that cannot be overloaded?
Following are the operators that cannot be overloaded -.
Scope Resolution (:: ) Member Selection (.) Member selection through a pointer to function (.*)
When do I need necessarily to use the initialization list to initialize some parameters?
When the class has static members
What is Inheritance? What is the purpose?
The idea of inheritance is simple, a class is based on another class and uses data and implementation of the other class. The purpose of inheritance is Code Reuse.
Inheritance: definition, use, advantages
The idea of inheritance is simple, a class is based on another class and uses data and implementation of the other class. The purpose of inheritance is Code Reuse.
What is encapsulation?
Encapsulation is referred to one of the following two notions.
1) Data hiding: A language feature to restrict access to members of an object. For example, private and protected members in C++.
2) Bundling of data and methods together: Data and methods that operate on that data are bundled together.
What is the type of “this” pointer? When does it get created?
It is a constant pointer type. It gets created when a non-static member function of a class is called.
Difference between overloading and overriding?
1) Overloading is static binding whereas Overriding is dynamic binding.
2) Overloading is nothing but the same method with different arguments, and it may or may not return the same value in the same class itself.
Overriding is the same method names with same arguments and return types associated with the class and its child class.
What is function overloading?
Function overloading an as a normal function, but it can perform different tasks. It allows the creation of several methods with the same name which differ from each other by the type of input and output of the function.
Example void add(int& a, int& b); void add(double& a, double& b); void add(struct bob& a, struct bob& b);
Difference between class and struct
- Structure default access type is public, but class access type is private.
- A structure is used for grouping data whereas class can be used for grouping data and methods. Structures are exclusively used for data, and it doesn’t require strict validation, but classes are used to encapsulates and inherit data which requires strict validation.
Define Destructor?
A destructor is a method which is automatically called when the object is made of scope or destroyed. Destructor name is also same as class name but with the tilde symbol before the name.
Explain the virtual inheritance in C++.
Virtual inheritance is used when a single base class is inherited with virtual methods. It can be achieved by the virtual keyword in the program. In this, the object that belongs to virtual class becomes common to the base class. It is used for multiple inheritance, as it creates multiple sub objects and gives the feature where a class can inherit from more than one classes.
What is ‘this’ pointer?
THIS pointer refers to the current object of a class. THIS keyword is used as a pointer which differentiates between the current object with the global object. Basically, it refers to the current object.
Which keyword can be used for overloading?
Operator keyword is used for overloading.