🌍 C++ Programming · subject

C++ Programming Object-Oriented Programming Syllabus

The Object-Oriented Programming syllabus for C++ Programming in full: 5 chapters and 17 topics, every one listed below, plus 50 Object-Oriented Programming flashcards with answers.

5Chapters
17Topics
50Flashcards

Object-Oriented Programming syllabus: all 5 chapters

Expand any chapter to see its topics and sub-topics.

  1. Classes and Objects

    3 topics
    • Class Definition
    • Creating Objects
    • Access Specifiers
  2. Constructors and Destructors

    4 topics
    • Default Constructor
    • Parameterized Constructor
    • Copy Constructor
    • Destructor
  3. Inheritance

    3 topics
    • Base and Derived Classes
    • Types of Inheritance
    • Virtual Base Class
  4. Polymorphism

    4 topics
    • Compile-time Polymorphism
    • Runtime Polymorphism
    • Virtual Functions
    • Abstract Classes
  5. Operator Overloading

    3 topics
    • Unary Operator Overloading
    • Binary Operator Overloading
    • Function Call Operator Overloading

C++ Programming Object-Oriented Programming flashcards — 50 questions with answers

  1. In C++, what is a class and what is the syntax to define one?

    A class is a user-defined data type that binds data members and member functions into a single unit (a blueprint for objects). Syntax: $$\texttt{class ClassName \{ ... \};}$$ The definition must end with a semicolon.

  2. What are the two main components (members) inside a class definition?

    1. Data members (attributes/variables that hold state). 2. Member functions (methods that define behavior). Together they encapsulate data and the operations on that data.

  3. How do you create an object of a class in C++, both on the stack and on the heap?

    Stack (automatic): $\texttt{ClassName obj;}$ Heap (dynamic, using new): $\texttt{ClassName* p = new ClassName;}$ which returns a pointer and must later be freed with $\texttt{delete p;}$

  4. How are members accessed through an object versus through a pointer to an object?

    Through an object, use the dot operator: $\texttt{obj.member}$. Through a pointer, use the arrow operator: $\texttt{ptr->member}$, which is shorthand for $\texttt{(*ptr).member}$.

  5. What are the three access specifiers in C++ and what does each control?

    public: members accessible from anywhere. private: members accessible only within the class and its friends. protected: members accessible within the class, its friends, and its derived classes.

  6. What is the default access specifier for members of a class versus a struct in C++?

    For a class, the default access is private. For a struct, the default access is public. This is the primary difference between class and struct in C++.

  7. What is a constructor in C++ and how is it distinguished from ordinary member functions?

    A constructor is a special member function automatically invoked when an object is created, used to initialize it. It has the same name as the class and no return type (not even void).

  8. What is a default constructor?

    A default constructor is a constructor that takes no arguments (or has all parameters with default values). It initializes objects when no arguments are supplied, e.g. $\texttt{ClassName();}$. The compiler provides an implicit one if no constructor is declared.

  9. When does the compiler NOT provide an implicit default constructor?

    If you define any constructor of your own (e.g., a parameterized constructor), the compiler does not automatically generate a default constructor. You must define one explicitly if you still need it.

  10. What is a parameterized constructor and give its purpose?

    A parameterized constructor takes one or more arguments, allowing objects to be initialized with specific values at creation time, e.g. $\texttt{Point(int x, int y);}$. It enables different objects to start with different states.

  11. What is a member initializer list and why is it preferred over assignment in the constructor body?

    It initializes members before the constructor body runs, using the syntax $\texttt{Ctor(args) : mem1(v1), mem2(v2) \{ \}}$. It is required for const members, references, and base-class subobjects, and it avoids a redundant default-construct-then-assign step.

  12. What is a copy constructor and what is its standard signature?

    A copy constructor creates a new object as a copy of an existing object. Standard signature: $$\texttt{ClassName(const ClassName\& other);}$$ The parameter is passed by const reference to avoid infinite recursion and unnecessary copying.

  13. Why must the copy constructor's parameter be passed by reference and not by value?

    Passing by value would itself require a copy to be made, which would call the copy constructor again, leading to infinite recursion. Passing by reference avoids this.

  14. Name three situations in which the copy constructor is invoked.

    1. When an object is initialized from another object of the same class. 2. When an object is passed by value to a function. 3. When an object is returned by value from a function. (Also during certain copy-initializations.)

  15. What is the difference between a shallow copy and a deep copy?

    A shallow copy copies member values directly, so pointer members share the same memory. A deep copy allocates new memory and duplicates the pointed-to data, so each object owns independent resources. The default copy constructor performs a shallow copy.

  16. What is a destructor and what is its syntax?

    A destructor is a special member function automatically called when an object goes out of scope or is deleted, used to release resources. Syntax: $\texttt{\~ClassName();}$ — same name as the class prefixed with a tilde, no arguments, no return type.

  17. In what order are constructors and destructors called for an object hierarchy?

    Constructors: base class first, then derived class. Destructors: reverse order — derived class first, then base class. For member objects, they are constructed in declaration order and destructed in reverse.

  18. What is the Rule of Three in C++?

    If a class needs a user-defined destructor, copy constructor, or copy assignment operator, it almost certainly needs all three. This is because such classes typically manage a resource (like dynamic memory) requiring consistent copy and cleanup semantics.

  19. What is inheritance and what are the base class and derived class?

    Inheritance lets a new class acquire the members of an existing class. The base class (parent) is the one being inherited from; the derived class (child) inherits from it and can add or override members. Syntax: $\texttt{class Derived : public Base \{ \};}$

  20. How does the inheritance access mode (public, protected, private) affect inherited members?

    public inheritance: base's public stays public, protected stays protected. protected inheritance: base's public and protected both become protected. private inheritance: base's public and protected both become private. In all cases, base's private members are never directly accessible.

Show the other 30 Object-Oriented Programming flashcards
  1. List the five types of inheritance in C++.

    1. Single inheritance (one base). 2. Multiple inheritance (multiple bases). 3. Multilevel inheritance (chain: A -> B -> C). 4. Hierarchical inheritance (one base, many derived). 5. Hybrid inheritance (combination of the above).

  2. What is multiple inheritance and what classic problem can it cause?

    Multiple inheritance is when a derived class inherits from more than one base class ($\texttt{class C : public A, public B}$). It can cause the diamond problem: ambiguity and duplicate base subobjects when two bases share a common ancestor.

  3. What is the diamond problem in C++?

    It occurs when a class D inherits from two classes B and C that both inherit from a common base A. D then contains two copies of A's members, causing ambiguity and duplication. It is resolved using virtual base classes.

  4. What is a virtual base class and how is it declared?

    A virtual base class ensures that only one shared copy of a common base is inherited, solving the diamond problem. Declared with the virtual keyword: $\texttt{class B : virtual public A \{ \};}$ and $\texttt{class C : virtual public A \{ \};}$

  5. In a diamond hierarchy using virtual inheritance, which class is responsible for initializing the virtual base?

    The most-derived class (the one at the bottom of the diamond) is responsible for directly initializing the virtual base class's constructor. Intermediate classes' calls to the virtual base constructor are ignored.

  6. What is polymorphism and what are its two broad categories in C++?

    Polymorphism means 'many forms' — the ability of one interface to behave differently based on context. The two categories are compile-time (static) polymorphism and runtime (dynamic) polymorphism.

  7. What is compile-time (static) polymorphism and how is it achieved?

    Compile-time polymorphism resolves the function call during compilation (early/static binding). It is achieved through function overloading and operator overloading (and templates).

  8. What is function overloading?

    Function overloading is defining multiple functions with the same name but different parameter lists (different number or types of parameters) in the same scope. The compiler selects the correct one based on the arguments. It is a form of compile-time polymorphism.

  9. Can two functions be overloaded if they differ only in their return type?

    No. Overloading requires differences in the parameter list (number, types, or order of parameters). Differing only by return type is not valid overloading and causes a compile error.

  10. What is runtime (dynamic) polymorphism and how is it achieved?

    Runtime polymorphism resolves the function call during execution (late/dynamic binding). It is achieved through virtual functions accessed via a base class pointer or reference, enabling function overriding.

  11. What is the difference between function overloading and function overriding?

    Overloading: same name, different parameters, same scope, resolved at compile time. Overriding: a derived class provides a new definition of a base class virtual function with the same signature, resolved at runtime via dynamic binding.

  12. What is a virtual function?

    A virtual function is a member function declared in a base class with the keyword virtual and redefined (overridden) in derived classes. It enables runtime polymorphism: the version called is determined by the actual object type, not the pointer/reference type.

  13. What is a vtable (virtual table) and how does it enable dynamic dispatch?

    A vtable is a per-class table of pointers to the class's virtual functions. Each polymorphic object holds a hidden vptr pointing to its class's vtable. At runtime, a virtual call is resolved by looking up the function address in the vtable via the vptr.

  14. What is a pure virtual function and what is its syntax?

    A pure virtual function is a virtual function with no implementation in the base class, declared by assigning $= 0$: $$\texttt{virtual void draw() = 0;}$$ It must be overridden by derived classes and makes its class abstract.

  15. What is an abstract class in C++?

    An abstract class is a class that contains at least one pure virtual function. It cannot be instantiated directly and serves as a base/interface; derived classes must override all pure virtual functions to become concrete (instantiable).

  16. Can you create an object of an abstract class? Can you create a pointer or reference to it?

    You cannot create (instantiate) an object of an abstract class. However, you can declare pointers and references to an abstract class, which are used to achieve polymorphism by pointing to derived-class objects.

  17. Why should a base class intended for polymorphic use declare a virtual destructor?

    So that deleting a derived object through a base-class pointer calls the derived destructor first, then the base destructor. Without a virtual destructor, only the base destructor runs, causing undefined behavior / resource leaks.

  18. What is operator overloading and what is the general syntax of an operator function?

    Operator overloading gives an operator a user-defined meaning for class operands. It uses a function named with the operator keyword: $$\texttt{returnType operator<op>(parameters);}$$ for example $\texttt{Complex operator+(const Complex\& rhs);}$

  19. Name at least three operators that CANNOT be overloaded in C++.

    The scope resolution operator $\texttt{::}$, member access operator $\texttt{.}$, pointer-to-member operator $\texttt{.*}$, the ternary conditional $\texttt{?:}$, and $\texttt{sizeof}$ cannot be overloaded.

  20. What is unary operator overloading? Give an example operator.

    Unary operator overloading redefines an operator that acts on a single operand, such as $\texttt{-}$ (negation), $\texttt{!}$, $\texttt{++}$, or $\texttt{--}$. Example: $\texttt{Point operator-();}$ negates the coordinates of a Point object.

  21. How many arguments does a unary operator function take as (a) a member function and (b) a friend/global function?

    (a) As a member function: zero explicit arguments (the operand is the invoking object, *this). (b) As a friend/global function: one argument (the single operand).

  22. How are the prefix and postfix increment operators distinguished when overloading?

    Prefix $\texttt{++obj}$ is overloaded as $\texttt{T operator++();}$. Postfix $\texttt{obj++}$ takes a dummy int parameter: $\texttt{T operator++(int);}$. The int argument is never used except to differentiate the two.

  23. What is binary operator overloading? Give an example.

    Binary operator overloading redefines an operator that works on two operands, such as $\texttt{+}$, $\texttt{-}$, $\texttt{==}$, $\texttt{<}$. Example: $\texttt{Complex operator+(const Complex\& b);}$ adds two Complex objects and returns their sum.

  24. How many arguments does a binary operator function take as (a) a member function and (b) a friend/global function?

    (a) As a member function: one argument (the right-hand operand; the left operand is *this). (b) As a friend/global function: two arguments (both operands explicitly).

  25. When must a binary operator be overloaded as a friend/global function rather than a member function?

    When the left-hand operand is not an object of the class (e.g., $\texttt{2 * obj}$ or $\texttt{cout << obj}$). A member operator requires the class object on the left, so a non-member (often friend) function is needed to allow a different left operand.

  26. What is the function call operator and how is it overloaded?

    The function call operator $\texttt{operator()}$ lets objects be called like functions (creating a functor). It is overloaded only as a member function and can take any number of arguments, e.g. $\texttt{int operator()(int x, int y);}$ enabling $\texttt{obj(a, b)}$.

  27. What is a functor (function object) in C++?

    A functor is an object of a class that overloads the function call operator $\texttt{operator()}$, so it can be invoked using function-call syntax like $\texttt{obj(args)}$. Functors can carry state and are widely used with STL algorithms.

  28. Compare compile-time and runtime polymorphism on binding time and mechanism.

    Compile-time: early/static binding, resolved at compilation, achieved via function/operator overloading; generally faster. Runtime: late/dynamic binding, resolved during execution via the vtable, achieved via virtual functions and overriding; more flexible but with slight overhead.

  29. Why can a base class pointer point to a derived class object, and what determines which overridden function it calls?

    Because a derived object 'is-a' base object (upcasting is implicit and safe). If the function is virtual, the actual (dynamic) type of the pointed-to object determines which override runs; if non-virtual, the static (pointer) type determines it.

  30. What does the override specifier do in C++11 and why use it?

    Placing override after a derived member function's signature (e.g. $\texttt{void draw() override;}$) tells the compiler the function is meant to override a base virtual function. If no matching virtual function exists, compilation fails, catching signature-mismatch bugs.

C++ Programming Object-Oriented Programming FAQ

What is in the C++ Programming Object-Oriented Programming syllabus?

C++ Programming Object-Oriented Programming is split into 5 chapters: Classes and Objects, Constructors and Destructors, Inheritance, Polymorphism and Operator Overloading. Together they contain 17 topics.

How many chapters are in C++ Programming Object-Oriented Programming?

5 chapters, containing 17 topics. The largest chapter is Constructors and Destructors, with 4 topics.

Are there flashcards for C++ Programming Object-Oriented Programming?

Yes — a 50-card Object-Oriented Programming deck, printed in full in the flashcards section of this page and available in the Examius app with spaced repetition. The free plan covers one exam of your choice, including its flashcards; Examius Pro, a one-time purchase, unlocks every other exam and deck.