Logo/

C Plus Plus Object Oriented Concepts

Log in

Made with ScholarlyMake flashcards like these from your own notes

C Plus Plus Object Oriented Concepts

Questions and answers

86 cards
  1. Question:What is a class in C++?

    Answer:A class is a user-defined data type that defines the properties and behaviors of a set of objects. It acts as a blueprint for creating objects.

  2. Question:What is an object in C++?

    Answer:An object is an instance of a class, a concrete entity created from a class that can have a color and shape, for example.

  3. Question:What are data members and member functions in a class?

    Answer:Data members are variables that store the properties of the class, while member functions are functions that define the behaviors of the class.

  4. Question:What is encapsulation in C++?

    Answer:Encapsulation is the mechanism of hiding the internal details of an object and providing access only to what's necessary through public interfaces.

  5. Question:What is polymorphism in C++?

    Answer:Polymorphism allows objects of different classes to be treated as objects of a common superclass, enabling functions to operate on objects without knowing their exact class.

  6. Question:Explain the concept of a Student class with data members like name, id, and gpa.

    Answer:The Student class encapsulates data members such as name (string), id (int), and gpa (float). Each object of the Student class will copy these data members.

  7. Question:What is an abstract data type?

    Answer:An abstract data type is a self-contained entity composed of both data attributes stored in data members and functions defined by member functions that operate on the data.

  8. Question:What is a normal member function in C++?

    Answer:A normal member function is defined outside the class and can only access private members of the class it is defined for.

  9. Question:What is an inline member function?

    Answer:An inline member function is defined inside the class declaration and can directly access the data members.

  10. Question:What are the primitive data types in C++?

    Answer:int, short, long, long long, float, double, char, bool.

  11. Question:What is data hiding?

    Answer:Data hiding is the practice of making data members private to protect them from external access and modification.

  12. Question:What is encapsulation?

    Answer:Encapsulation is the bundling of data members and member functions that operate on that data within a class.

  13. Question:What is a static data member?

    Answer:A static data member is a class variable that is shared among all objects of the class and belongs to the class itself, not to any individual object.

  14. Question:What are the different ways of defining data members?

    Answer:Data members can be defined as private, protected, or public based on access specifiers.

  15. Question:What is the syntax for declaring a static data member?

    Answer:Static data members are declared inside the class with the static keyword and defined outside with the class scope resolution operator.

  16. Question:What is the difference between static and non-static data members?

    Answer:Static members are shared across all objects and belong to the class, while non-static (instance) members are unique to each object.

  17. Question:What is a constructor?

    Answer:A constructor is a special member function that initializes the data members of an object when it is created.

  18. Question:What is a constant data member?

    Answer:A constant data member is a data member declared with const that cannot be modified after initialization.

  19. Question:What are access specifiers?

    Answer:Access specifiers are keywords (public, private, protected) that control the visibility and access to class members.

  20. Question:What is private access?

    Answer:Private members are accessible only by member functions of the same class.

  21. Question:What is protected access?

    Answer:Protected members are accessible by member functions of the same class and derived classes.

  22. Question:What is public access?

    Answer:Public members are accessible from anywhere.

  23. Question:What is mutability for objects?

    Answer:Mutability refers to whether an object can be modified after creation; const objects cannot.

  24. Question:How do you declare a class with a constructor?

    Answer:Use the class keyword, declare members, and define the constructor inside or outside the class.

  25. Question:What is the purpose of a constructor?

    Answer:To initialize the object's data members upon creation.

  26. Question:What are the types of constructors?

    Answer:Default constructor (no parameters), parameterized constructor (with parameters), copy constructor (copies another object).

  27. Question:What is a default constructor?

    Answer:A constructor that takes no arguments and provides default initialization.

  28. Question:What is a parameterized constructor?

    Answer:A constructor that accepts arguments to initialize specific values for data members.

  29. Question:What is a copy constructor?

    Answer:A constructor that initializes an object using another object of the same class.

  30. Question:How would you define a Triangle class with width and height parameters?

    Answer:class Triangle { public: Triangle(float w, float h) : width(w), height(h) {} private: float width, height; };

  31. Question:Explain how to implement a parameterized constructor.

    Answer:Define the constructor with parameters and use them to set data members, either in the body or initializer list.

  32. Question:What is the difference between a constructor and a destructor?

    Answer:Constructor initializes the object when created; destructor cleans up resources when the object is destroyed.

  33. Question:What is the purpose of a destructor?

    Answer:To release resources like memory allocated by the object before it is destroyed.

  34. Question:What is the name of a destructor?

    Answer:The class name preceded by a tilde (~).

  35. Question:What are new and delete operators?

    Answer:New allocates memory for objects dynamically; delete deallocates it.

  36. Question:What is dynamic memory allocation?

    Answer:Allocating memory at runtime using new, as opposed to static allocation at compile time.

  37. Question:What is the delete operator?

    Answer:Used to deallocate memory that was dynamically allocated with new.

  38. Question:What is dynamic memory in C++?

    Answer:Memory allocated at runtime using new and delete, managed manually unlike automatic variables.

  39. Question:What is memory leak?

    Answer:When dynamically allocated memory is not deallocated, leading to wasted memory.

  40. Question:What is a pointer?

    Answer:A variable that stores the memory address of another variable.

  41. Question:What is the address of operator (&)?

    Answer:Returns the memory address of its operand.

  42. Question:What is the dereference operator (*)?

    Answer:Accesses the value at the address stored in a pointer.

  43. Question:How do you use the dot operator with an object?

    Answer:obj.member accesses members directly with .

  44. Question:How do you use the arrow operator with a pointer to an object?

    Answer:ptr->member accesses members through the pointer.

  45. Question:What is this pointer?

    Answer:A pointer that points to the current object inside a member function.

  46. Question:What is an implicit call?

    Answer:When the compiler automatically calls a function, like a default constructor.

  47. Question:What is an explicit call?

    Answer:When you directly call a function in your code.

  48. Question:What happens when you pass an object by value?

    Answer:A copy of the object is made and passed, invoking the copy constructor if defined.

  49. Question:What is an implicit call to the copy constructor?

    Answer:When the compiler calls it automatically, like when passing by value or initializing.

  50. Question:What is a default constructor?

    Answer:A constructor with no parameters that initializes objects with default values.

  51. Question:What are the initialization lists?

    Answer:A way to initialize data members in the constructor header using : member(value).

  52. Question:What is a delegating constructor?

    Answer:A constructor that calls another constructor in the same class to handle initialization.

  53. Question:What are data members?

    Answer:Variables declared inside a class that hold the state of objects.

  54. Question:What is an initializer list?

    Answer:A list after the constructor parameters that initializes members before the body executes.

  55. Question:What is the new operator?

    Answer:Used to dynamically allocate memory for objects or arrays.

  56. Question:What is the unary + operator?

    Answer:Performs no operation but can be overloaded; for example, +x for a number.

  57. Question:What is the unary - operator?

    Answer:Negates the value; for example, -x for a number.

  58. Question:What is the logical NOT operator (!)?

    Answer:Returns true if the operand is false, and vice versa.

  59. Question:What are binary operators like + , - , * , / ?

    Answer:Perform arithmetic operations on two operands.

  60. Question:What is the [] operator?

    Answer:Used for array indexing, overloaded for custom containers.

  61. Question:What is the () operator?

    Answer:Used for function calls, overloaded for functors.

  62. Question:What is operator overloading?

    Answer:Defining how operators work with user-defined types, like + for strings.

  63. Question:What is the >> operator overloading?

    Answer:Overloaded for input streams to read into objects.

  64. Question:What is the << operator overloading?

    Answer:Overloaded for output streams to print objects.

  65. Question:What is single inheritance?

    Answer:A class inherits from one base class.

  66. Question:What is multiple inheritance?

    Answer:A class inherits from more than one base class.

  67. Question:What is multilevel inheritance?

    Answer:Inheritance in a chain, like A -> B -> C.

  68. Question:What is hierarchical inheritance?

    Answer:Multiple classes inherit from one base class.

  69. Question:What is hybrid inheritance?

    Answer:A combination of two or more types of inheritance.

  70. Question:What is virtual inheritance?

    Answer:Used to avoid multiple copies of base class in diamond inheritance problems.

  71. Question:What is an exception?

    Answer:An event that occurs during program execution that disrupts normal flow.

  72. Question:What is exception handling in C++?

    Answer:Using try, catch, and throw to manage errors gracefully.

  73. Question:What is a throw keyword?

    Answer:Used to raise an exception when an error occurs.

  74. Question:What is a catch block?

    Answer:Catches and handles the thrown exception.

  75. Question:What is a try block?

    Answer:Encloses code that might generate an exception.

  76. Question:What is a catch block?

    Answer:Handles the exception thrown from the try block.

  77. Question:What are compile-time errors?

    Answer:Errors detected by the compiler before runtime, like syntax errors.

  78. Question:What are runtime errors?

    Answer:Errors that occur during program execution, like division by zero.

  79. Question:What are logical errors?

    Answer:Errors where the program runs but produces incorrect results.

  80. Question:What is generic programming?

    Answer:Writing code that works with any data type using templates.

  81. Question:What is a class template?

    Answer:A blueprint for a generic class that can work with different data types.

  82. Question:What is a function template?

    Answer:A blueprint for a generic function that can operate on different types.

  83. Question:What are the advantages of templates?

    Answer:Code reusability, type safety, and efficiency without runtime overhead.

  84. Question:How do you write a function template to add two values?

    Answer:template T add(T a, T b) { return a + b; }

  85. Question:What is a class template?

    Answer:A class definition with type parameters, instantiated with specific types.

  86. Question:What is a function template?

    Answer:A function definition with type parameters for generic behavior.