Lecture 9 - C++ Classes Flashcards
What are compound data types in C++?
Compound data types group multiple variables under a single identifier. In C++, they are implemented via struct
or class
, and members can be of different types, accessed by name.
What is a struct
in C++?
A struct
in C++ is similar to a struct
in C but can also contain member functions. It is a way to group related variables together under a single name, allowing easier data organization.
What is the key difference between struct
and class
in C++?
The primary difference is the default access modifier. In a struct
, members are public by default, whereas, in a class
, members are private by default.
What are visibility modifiers in C++?
Visibility modifiers control access to class or struct members. They include public
, private
, and protected
, determining whether class members are accessible outside the class.
How do constructors work in C++ classes?
Constructors are special member functions that initialize objects of a class. They have the same name as the class and do not have a return type. They can be overloaded to provide different ways to initialize objects.
What is encapsulation in C++?
Encapsulation refers to bundling the data (variables) and methods (functions) operating on that data within a class, and restricting direct access to some of the object’s components using visibility modifiers.
What is procedural abstraction in C++?
Procedural abstraction in C++ refers to breaking down complex operations into simpler functions or methods. It hides implementation details and exposes only essential functionalities.
What are accessor and mutator functions?
Accessor functions (getters) retrieve the value of private data members, while mutator functions (setters) modify the value of private data members, providing controlled access to the class’s private data.
What is a default constructor in C++?
A default constructor is a constructor that takes no parameters. If no constructor is defined for a class, the compiler provides a default constructor.
What is constructor overloading in C++?
Constructor overloading allows a class to have multiple constructors with different signatures. This provides flexibility in object initialization.
What is the purpose of a copy constructor in C++?
A copy constructor initializes a new object as a copy of an existing object. It is automatically called when an object is passed by value, returned from a function, or explicitly copied.
What is the difference between deep copy and shallow copy?
A shallow copy copies an object’s pointer but not the data it points to, leading to shared memory. A deep copy duplicates both the object and the data it points to, creating independent copies.
What is the role of the this
pointer in C++?
The this
pointer is an implicit parameter to all non-static member functions. It points to the object that invoked the function and allows access to its members within the class.
What are destructors in C++?
Destructors are special member functions that clean up when an object is destroyed. They have the same name as the class but are preceded by a tilde (~
) and have no return type.
What is function overloading in C++?
Function overloading allows multiple functions to have the same name but different parameter types or numbers. The appropriate function is selected based on the argument types passed during the call.