logo

Question-Bank

  • Home
  • About_us

  • Explain the concepts of class and object with suitable example
    Class in C++ is the building block that leads to Object-Oriented programming. It is a user-defined data type, which holds its
    own data members and member functions, which can be accessed and used by creating an instance of that class. A C++ class is like a
    blueprint for an object. For Example: Consider the Class of Cars. There may be many cars with different names and brands but all
    of them will share some common properties like all of them will have 4 wheels, Speed Limit, Mileage range, etc. So here, Car is the
    class, and wheels, speed limits, and mileage are their properties.
    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 used to manipulate these variables together,
    these data members and member functions define the properties and behavior of the objects in a Class.
    In the above example of class Car, the data member will be speed limit, mileage, etc, and member functions can be applying brakes,increasing speed, etc.
    An Object is an instance of a Class. When a class is defined, no memory is allocated but when it is instantiated (i.e. an object is created) memory is allocated.
    Defining Class and Declaring Objects
    A class is defined in C++ using the keyword class followed by the name of the class. The body of the class is defined inside the curly brackets and terminated by a semicolon at the end.
    Declaring Objects
    When a class is defined, only the specification for the object is defined; no memory or storage is allocated. To use the data and access functions defined in the class, you need to create objects.
    Accessing Data Members
    The public data members are also accessed in the same way given however the private data members are not allowed to be accessed directly by the object. Accessing a data member depends solely on the access control of that data member.
    This access control is given by Access modifiers in C++. There are three access modifiers: public, private, and protected.
  • Discuss the principles of oops
    Object-oriented programming as the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc. in programming.
    The main aim of OOP is to bind together the data and the functions that operate on them so that no other part of the code can access this data except that function.
    There are some basic concepts that act as the building blocks of OOPs i.e.
    Class,Objects,Encapsulation,Abstraction,Polymorphism,Inheritance
    Encapsulation
    In normal terms, Encapsulation is defined as wrapping up data and information under a single unit. In Object-Oriented Programming, Encapsulation is defined as binding together the data and the functions that manipulate them.
    Consider a real-life example of encapsulation, in a company, there are different sections like the accounts section, finance section, sales section, etc. The finance section handles all the financial transactions and keeps records of all the data related to finance.
    Abstraction
    Data abstraction is one of the most essential and important features of object-oriented programming in C++. Abstraction means displaying only essential information and hiding the details. Data abstraction refers to providing only essential information
    about the data to the outside world, hiding the background details or implementation.
    Polymorphism
    The word polymorphism means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form. A person at the same time can have different characteristics. A man at the same time is a father, a husband, and an employee.
    So the same person possesses different behavior in different situations. This is called polymorphism.
    Inheritance
    The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object-Oriented Programming.
    Sub Class: The class that inherits properties from another class is called Sub class or Derived Class.
    Super Class: The class whose properties are inherited by a sub-class is called Base Class or Superclass.
  • Explain constructor and destructor
    The process of creating and deleting objects is a vital task in C++. Every time an instance of a class is created, the constructor method is called. A constructor is a class member function used to initialize the objects of the class. It is treated as a special member function because it has the same name as the class name.
    The Constructor is called whenever an object of the respective class is created. It is named "constructor" because it constructs the value of the data members of a class. Initial values ​​can be passed as arguments to the constructor function when the object is declared.
    A constructor is a member function of a class that has the same name as the class name. It helps to initialize the object of a class. It can either accept the arguments or not. It is used to allocate the memory to an object of the class. It is called whenever an instance of the class is created.
    It can be defined manually with arguments or without arguments. There can be many constructors in a class. It can be overloaded but it can not be inherited or virtual. There is a concept of copy constructor which is used to initialize an object from another object.
    Syntax:
    ClassName()
    {
    //Constructor's Body
    }
    Like a constructor, Destructor is also a member function of a class that has the same name as the class name preceded by a tilde(~) operator. It helps to deallocate the memory of an object. It is called while the object of the class is freed or deleted. In a class,
    there is always a single destructor without any parameters so it can’t be overloaded. It is always called in the reverse order of the constructor. if a class is inherited by another class and both the classes have a destructor then the destructor of the child class is called first, followed by the destructor of the parent or base class.
    Syntax:
    ClassName()
    {
    //Destructor's Body
    }
  • write about exception handling mechanism
    One of the advantages of C++ over C is Exception Handling. Exceptions are runtime anomalies or abnormal conditions that a program encounters during its execution. There are two types of exceptions: a)Synchronous, b)Asynchronous
    (i.e., exceptions which are beyond the program’s control, such as disc failure, keyboard interrupts etc.). C++ provides the following specialized keywords for this purpose:
    try: Represents a block of code that can throw an exception.
    catch: Represents a block of code that is executed when a particular exception is thrown.
    throw: Used to throw an exception. Also used to list the exceptions that a function throws but doesn’t handle itself.
    Why Exception Handling?
    The following are the main advantages of exception handling over traditional error handling:
    1) Separation of Error Handling code from Normal Code: In traditional error handling codes, there are always if-else conditions to handle errors. These conditions and the code to handle errors get mixed up with the normal flow.
    This makes the code less readable and maintainable. With try/catch blocks, the code for error handling becomes separate from the normal flow.
    2) Functions/Methods can handle only the exceptions they choose: A function can throw many exceptions, but may choose to handle some of them. The other exceptions, which are thrown but not caught, can be handled by the caller. If the caller chooses not to catch them, then the exceptions are handled by the caller of the caller.
    In C++, a function can specify the exceptions that it throws using the throw keyword. The caller of this function must handle the exception in some way (either by specifying it again or catching it).
    C++ Exceptions:
    When executing C++ code, different errors can occur: coding errors made by the programmer, errors due to wrong input, or other unforeseeable things.
    When an error occurs, C++ will normally stop and generate an error message. The technical term for this is: C++ will throw an exception (error).
    C++ try and catch:
    Exception handling in C++ consists of three keywords: try, throw and catch:
    The try statement allows you to define a block of code to be tested for errors while it is being executed.
    The throw keyword throws an exception when a problem is detected, which lets us create a custom error.
    The catch statement allows you to define a block of code to be executed if an error occurs in the try block.
  • Describe various control statement in c++
    In C++, Control Statements are usually jumped from one part of the C++ code to another depending on whether a particular condition is satisfied or not. There are three types of C++ Control Statements are given as follows:
    Sequence Statement,Selection Statement,Loop Statement
    if-else statement is used to execute a statement block. It has the following syntax :
    if(expression is true
    {
    action 1;
    }
    else
    {
    action 2;
    }
    for is an entry controlled loop. It is used when an action is to be repeated for a predetermined time. It has the following syntax:
    for(initial value; test condition; increment or decrement)
    {
    action 1;
    }
    while is also a loop control structure but it’s an entry-controlled loop.It has the following syntax:
    while(condition is true)
    {
    action 1;
    }
    action 2;
    do-while is an exit-controlled loop based on a condition, the control is transferred back to a particular point in the program. It has the following syntax:
    do
    {
    action;
    }
    while(condition is true);
    action 2;
  • Discuss function overloading
    Two or more functions can have the same name but different parameters; such functions are called function overloading in c++.
    The function overloading in the c++ feature is used to improve the readability of the code. It is used so that the programmer does not have to remember various function names.
    If any class has multiple functions with different parameters having the same name, they are said to be overloaded.
    If we have to perform a single operation with different numbers or types of arguments, we need to overload the function. If I say parameter list, it means the data type and sequence of the parameters. For example, the parameters list of a function myfunction (int a, double b) is (int, double),
    which is different from the function myfunction (double a, int b) parameter list (double, int). Function overloading is a compile-time polymorphism.
    As we already know what a parameter list is, so let’s see the rules of overloading: we can have the following functions in the same scope. sum(int a, int b)
    sum(int a, int b, int c)
    The easiest way to remember this rule is that the parameters should qualify any one or more than one of the following conditions:
    They should have a different type
    They should have a different number
    They should have a different sequence of parameters.
    To put it simply,
    Function overloading in c++ can have the same name but different parameters
    C++ has many features, and one of the most important features is function overloading. It is a code with more than one function with the same name having various types of argument lists. This argument list includes the data type of arguments and the sequence of the arguments.
    Function overloading is similar to polymorphism that helps us to get different behaviour, with the same name of the function. Function overloading in c++ is used for code reusability and to save memory. The C++ programming language offers an overloading feature that enables an overload of two or more methods with the same name but distinct parameters in order to create compile-time polymorphism. Function and operator overloading can be used to carry it out.
    While operator overloading overloads operators to provide user-defined data types with particular meaning, function overloading overloads two or more functions with the same name but distinct parameters.
  • Explain in detail about various types of inheritance
    The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object-Oriented Programming.
    Inheritance is a feature or a process in which, new classes are created from the existing classes. The new class created is called “derived class” or “child class” and the existing class is known as the “base class” or “parent class”.
    The derived class now is said to be inherited from the base class.
    When we say derived class inherits the base class, it means, the derived class inherits all the properties of the base class, without changing the properties of base class and may add new features to its own. These new features in the derived class will not affect the base class.
    The derived class is the specialized class for the base class. Sub Class: The class that inherits properties from another class is called Subclass or Derived Class.
    Super Class: The class whose properties are inherited by a subclass is called Base Class or Superclass.
    Using inheritance, we have to write the functions only one time instead of three times as we have inherited the rest of the three classes from the base class (Vehicle).
    Implementing inheritance in C++: For creating a sub-class that is inherited from the base class we have to follow the below syntax.
  • Explain virtual functions and polymorphism.
    A virtual function is a member function that is declared in the base class using the keyword virtual and is re-defined (Overridden) in the derived class. It tells the compiler to perform late binding where the compiler matches the object with the right called function and executes it during the runtime.
    This technique falls under Runtime Polymorphism. The term Polymorphism means the ability to take many forms. It occurs if there is a hierarchy of classes that are all related to each other by inheritance. In simple words, when we break down Polymorphism into ‘Poly – Many’ and ‘morphism – Forms’ it means showing different characteristics in different situations.
    Consider the following simple program as an example of runtime polymorphism. The main thing to note about the program is that the derived class’s function is called using a base class pointer.
    The idea is that virtual functions are called according to the type of the object instance pointed to or referenced, not according to the type of the pointer or reference.
    In other words, virtual functions are resolved late, at runtime.
    Virtual functions allow us to create a list of base class pointers and call methods of any of the derived classes without even knowing the kind of derived class object.