Question:- Is it possible for a class to inherit the constructor of its base class?
Answer:- No, a class cannot inherit the constructor of its base class.
Question:- Identify which OOPs concept should be used in the following scenario?
Answer:- A group of 5 friends, one boy never gives any contribution when the group goes for the outing. Suddenly a beautiful girl joins the same group. The boy who never contributes is now spending a lot of money for the group. Runtime Polymorphism
Question:- What is composition?
Answer:- Composition is one of the vital concepts in OOP. It describes a class that references one or more objects of other classes in instance variables. It allows us to model a has-a association between objects. We can find such relationships in the real world. For example, a car has an engine. the following figure depicts the same
Question:- What are the differences between copy constructor and assignment operator?
Answer:- The copy constructor and the assignment operator (=) both are used to initialize one object using another object. The main difference between the two is that the copy constructor allocates separate memory to both objects i.e. existing object and newly created object while the assignment operator does not allocate new memory for the newly created object. It uses the reference variable that points to the previous memory block (where an old object is located).
Question:- What is the difference between Composition and Inheritance?
Answer:- Inheritance means an object inheriting reusable properties of the base class. Compositions mean that an object holds other objects. In Inheritance, there is only one object in memory (derived object) whereas, in Composition, the parent object holds references of all composed objects. From a design perspective, inheritance is "is a" relationship among objects whereas Composition is "has a" relationship among objects.
Question:- What is constructor chaining?
Answer:- In OOPs, constructor chaining is a sequence of invoking constructors (of the same class) upon initializing an object. It is used when we want to invoke a number of constructors, one after another by using only an instance. In other words, if a class has more than one constructor (overloaded) and one of them tries to invoke another constructor, this process is known as constructor chaining. In C++, it is known as constructor delegation and it is present from C++ 11.
Question:- What are the limitations of inheritance?
Answer:- • The main disadvantage of using inheritance is two classes get tightly coupled. That means one cannot be used independently of the other. If a method or aggregate is deleted in the Super Class, we have to refactor using that method in SubClass. • Inherited functions work slower compared to normal functions. • Need careful implementation otherwise leads to improper solutions.
Question:- What is Coupling in OOP and why it is helpful?
Answer:- In programming, separation of concerns is known as coupling. It means that an object cannot directly change or modify the state or behavior of other objects. It defines how closely two objects are connected together. There are two types of coupling, loose coupling, and tight coupling. Objects that are independent of one another and do not directly modify the state of other objects is called loosely coupled. Loose coupling makes the code more flexible, changeable, and easier to work with. Objects that depend on other objects and can modify the states of other objects are called tightly coupled. It creates conditions where modifying the code of one object also requires changing the code of other objects. The reuse of code is difficult in tight coupling because we cannot separate the code. Since using loose coupling is always a good habit.
Question:- Name the operators that cannot be overload.
Answer:- 1. Scope Resolution Operator (::) 2. Ternary Operator (? :) 3. Member Access or Dot Operator (.) 4. Pointer to Member Operator (.*) 5. sizeof operator
Question:- What is the difference between new and override?
Answer:- The new modifier instructs the compiler to use the new implementation instead of the base class function. Whereas, Override modifier helps to override the base class function. • virtual: indicates that a method may be overridden by an inheritor • override: Overrides the functionality of a virtual method in a base class, providing different functionality. • new: Hides the original method (which doesnt have to be virtual), providing different functionality. This should only be used where it is absolutely necessary. When you hide a method, you can still access the original method by upcasting to the base class. This is useful in some scenarios, but dangerous.
Question:- Explain overloading and overriding with example?
Answer:- • Overloading Overloading is a concept in OOP when two or more methods in a class with the same name but the method signature is different. It is also known as compile-time polymorphism. For example, in the following code snippet, the method add() is an overloaded method. • Overriding If a method with the same method signature is presented in both child and parent class is known as method overriding. The methods must have the same number of parameters and the same type of parameter. It overrides the value of the parent class method. It is also known as runtime polymorphism. For example, consider the following program.
Question:- What is Cohesion in OOP?
Answer:- In OOP, cohesion refers to the degree to which the elements inside a module belong together. It measures the strength of the relationship between the module and data. In short, cohesion represents the clarity of the responsibilities of a module. It is often contrasted with coupling. It focuses on a how single module or class is intended. Higher the cohesiveness of the module or class, better is the object-oriented design. There are two types of cohesion, i.e. High and Low. • High cohesion is associated with several required qualities of software including robustness, reliability, and understandability. • Low cohesion is associated with unwanted qualities such as being difficult to maintain, test, reuse, or even understand. High cohesion often associates with loose coupling and vice versa.
Question:- Give a real-world example of polymorphism?
Answer:- The general meaning of Polymorphism is one that has different forms. The best real-world example of polymorphism is a person that plays different roles at different palaces or situations. • At home a person can play the role of father, husband, and son. • At the office the same person plays the role of boss or employee. • In public transport, he plays the role of passenger. • In the hospital, he can play the role of doctor or patient. • At the shop, he plays the role of customer.
