[#10] – Object and Classes – Questions

C-plus-plus-programming-questions-techhyme

C++ is an Object Oriented Programming language. The key concepts of OOPs include classes, objects, data hiding, encapsulation, abstraction, polymorphism and inheritance. They key concept of Object Oriented Programming language C++ is a class. A class is a fundamental language construct for creating the complex user-defined data types. It distinguishes C++ from C language.

Index:

Its relative importance can be seen from the fact that before the name C++ was coined, the language was called C with classes. Classes with C++ evolved from the concept of structures in C.

Key Points To Remember –

  1. A class is a logical way of grouping together data and associated functions into a single unit. The body of a class consists of data members and associated member functions which are collectively known as members.
  2. The access specifiers private and public defines the scope of associated members. The members which are private in a class can only be accessed by member functions of the same class and not from outside the class. On the other hand, public members are not only accessible are private by default.
  3. An object is a real world entity which is an instance of a class. It can uniquely be identified by its name and it defines a state which is represented by the value(s) of its data member(s) at a particular point of time.
  4. The private members of a class can only be accessed through its member functions simply by referring their name in the class. Private members cannot be accessed directly from outside the class. However only public members can be accessed from outside using the dot operator(.) which is identical to accessing members of a structure.
  5. The member function(s) of a class can be either defined inside or outside the class body. They are defined inside as ordinary functions. However, if they are defined outside the class then they are defined using the scope resolution operator.
  6. The private member function(s) are usually used to hide certain operations of a class from the outsider and serve as utility functions to other functions of the class.
  7. Data Hiding is a mechanism that prevents direct access of class’s data from the outside world. This is achieved by declaring the data members of a class as private.
  8. Encapsulation binds data and its associated function into a single unit. In C++, this achieved using a class.
  9. Data Abstraction is a process of separating the interfaces of a class from its implementation details. It is implemented by declaring the member function inside a class and defining those member functions outside.
  10. Each object of a class has its own copy of the data members whereas member function gets memory only once corresponding to the class specification.
  11. An array of objects is a collection of objects of the same class type that are stored in the contiguous memory locations.
  12. Objects can be passed as an argument by value, by reference or by address.
  13. A static data member(s) is the data member(s) that gets memory only once for the whole class no matter how many objects(s)of class are created. This common copy is shared by all objects of its class.
  14. The static member function can access only static members(s) declared in the class.
  15. When a member function doesn’t modify any any data members of its class then such a member function are declared as const functions.

Viva Voce Questions – 

Here are commonly asked questions with answers:

  1. What are the two major parts of a class ?
    • Data and its associated functions.
  2. How does a structure in C++ differs from that of classes ?
    • Both structure and classes in C++ combine data and its associated functions together into a single unit. The basic difference is that structures are specified using struct keyword and all its members are public by default whereas classes are specified using class keyword and all its members are private by default. Moreover, advanced object oriented concepts are not implemented using structures.
  3. What is an access specifier ?
    • It controls which part of the program can access a member of the class.
  4. What is a difference between public and private access specifier ?
    • The member which are public are not only accessed inside the class but also from outside the class. However private member can only be accessed from inside the class.
  5. What is class instantiation ?
    • It is a process of creating objects of a class.
  6. Is it always necessary to specify access specifier in a class?
    • No, if you don’t specify any access specifier then it is private by default.
  7. Can you define member function outside the class body ?
    • Yes, it is possible by declaring the member function inside and defining outside the class body using scope resolution operator.
  8. Are the member functions defined inside the class inline by default ?
    • Yes, provided it contains few statements and doesn’t have control structures like loops, switch, if.
  9. How much memory is allocated when we create an object of a class ?
    • Memory allocated to an object is equal to sum of the size of the its data members.
  10. Should the data members of a class must always be private ?
    • No, it can be public as well as protected.
  11. How is data hiding implemented in C++ ?
    • Data Hiding is implemented by declaring the data members of class as private.
  12. How is encapsulation implemented in C++ ?
    • Encapsulation is implemented using class which binds the data and its associated functions into a single unit.
  13. How is abstraction implemented in C++ ?
    • Abstraction is implemented by declaring the member functions inside the class and defining outside which separate the interfaces of the class from its implementations. The header file (such as math.h) that contains declaration of built-in functions (sin(), sqrt() etc.) provide interfaces to the actual implementations present in the library file.
  14. What happens when we pass an object by reference ?
    • When an object is passed by reference, an alias of the original passed object is created in the called function so any modification to it in the called function are automatically reflected back to the original object.
  15. Why passing an object by reference is more efficient than by passing a value ?
    • While passing an object by reference, only the address of the object is passed and not its entire copy as in case of passing object by value. So, lot of memory is saved especially in case of large objects.
  16. What is the difference between passing an object by reference to that of pointer ?
    • The main difference is that in case of passing object by reference, the address of the object is passed implicitly while passing an object by pointer, the address of the object is passed explicitly to the function.
  17. Why data members of a class cannot be initialized at the point of declaration ?
    • Since memory is allocated to the data members only when an object of a class is created and not during the class specification, so there is no sense to initialize data members at the point of their declaration in the class.
  18. How are static data members different from non-static data members ?
    • Static data members gets memory only once no matter how many objects of a class are created, whereas non-static data members gets separate memory corresponding to each object. Moreover, unlike non-static data members, a static data member can appear as a default argument to the member function of the class.
  19. Can a data member of a class be an object of the same class ?
    • No, because class is just a blue-print and blueprint does not create any object. Objects of a class are created only when the class is defined.
  20. Can a data member of a class be a static object of the same class ?
    • Yes, because static object as a data member of the same class is only declared within the class and is defined (allocated memory) outside the class.
  21. How will you specify a member function which cannot modify class’s object data member ?
    • It is possible by declaring the member function as const. E.g. int avg() const;
  22. What is a mutuable data member ?
    • A data member which is not const even when it is a member of a const member function.
  23. Can a non-const member function be overloaded with a const member function ?
    • Yes, For example,
      int display() const,
      int display ();

Other Similar Questions – 

  1. What are structures ? How do structures in C differs from C++ ?
  2. Why do we use class instead of structures ?
  3. What is the difference between structures in C++ and a class ?
  4. What is a class ? How is it specified ?
  5. What do you understand by data members and member functions of a class ?
  6. Explain access specifiers and why do we need it ?
  7. What is difference between public and private member functions ?
  8. What is an object ? How objects can be defined and accessed in C++ ?
  9. ‘An object is an instance of a class’ . Explain.
  10. Describe how data members and member function are accessed inside
    • main() program
    • member function of same class
    • inside member function of another class
  11. What is a scope resolution operator ? How is it useful for defining the members of a class ?
  12. How will you define a member function inside and outside the class ?
  13. Create the following classes
    • Class – Employee
      Data members – employee identification number, employee name, basic pay, dearness allowance, house rent allowance.
      Member functions – Input the data , calculate the total salary and display it along with the employee details.
    • Class – Student
      Data members – Roll number, marks of three subjects (100 each).
      Member Functions – Input the data, compute average marks in three subjects, compute and display grade of the student.
  14. Explain the concept of inline member functions.
  15. How does a class promote data hiding, abstraction and encapsulation ?
  16. How is memory allocated to the data members and member functions of the class ?
  17. Explain the difference between the interface and the implementation of a class ?
  18. How and why the scope resolution operator used in classes ?
  19. What is an array of class objects ? How are they defined in C++ ?
  20. What are difference ways of passing objects as arguments to the function ?
  21. Explain the following statements:
    • t3. addtime(t1,t2); where t1, t2, t3 are objects of class time having data members hours and minutes. The member function addtime () calculates the sum of two times.
    • t3=t1.addtime (t2);
  22. What do you understand by static date members ? Why we need it ? explain using example.
  23. What is need of static member functions ?
  24. When do we use ‘const’ member functions ?
You may also like:

Related Posts

Leave a Reply