1/26
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No study sessions yet.
describe classes
A class is a user-defined blueprint or prototype from which objects are created.
It represents the set of properties or methods that are common to all objects of one type.
Using classes, you can create multiple objects with the same behavior instead of writing their code multiple times.
Components of a class in C++:
Access Specifiers: A class can have members defined as public, private, or protected to control accessibility.
Class Name: The class name should follow naming conventions, usually starting with a capital letter.
Body: The class body is enclosed with braces {} and defines data members and member functions.
describe objects
An object is an instance of a class.
It contains the actual values of the properties defined in the class and can use the methods defined in the class.
Multiple objects can be created from the same class, each with its own set of data members.
compare and contrast classes and objects
Feature | Class | Object |
Definition | Blueprint for creating objects | Instance of a class |
Contains | Data members and member functions | Actual values of data members |
Example | class Rectangle { ... }; | Rectangle rect1; |
describe access modifiers
Access specifiers control how the members (attributes and methods) of a class can be accessed.
They help protect data and organize code so that only the right parts can be seen or changed.
Access Specifiers in C++:
public – members are accessible from outside the class.
private – members cannot be accessed (or viewed) from outside the class.
protected – members cannot be accessed from outside the class, but can be accessed in inherited classes.
Example:
class MyClass {
public:
// class members here
};
describe inheritance
Inheritance allows one class to reuse attributes and methods from another class.
Helps you write cleaner, more efficient code by avoiding duplication.
Categories:
Base class (parent): The class being inherited from
Derived class (child): The class that inherits from another class
Example:
class Animal { ... };
class Dog : public Animal { ... };
describe composition
Composition is a “has-a” relationship. One class owns the other class and is responsible for its creation and destruction.
Represented in C++ as an object of one class being contained within another class.
The contained object cannot exist independently; if the containing object is destroyed, the contained object is automatically destroyed.
Example: A Car class contains an Engine object. If the Car is destroyed, the Engine is destroyed too.
describe abstraction
Abstraction means displaying only essential information and ignoring internal details.
Real-life example:
A man driving a car only knows how to accelerate or brake but does not know the internal mechanism.
Types of Abstraction:
Data Abstraction: Shows only required information about data, hides unnecessary details.
Control Abstraction: Shows only required information about implementation, hides unnecessary details.
Abstraction using Access Specifiers:
Public members can be accessed from anywhere.
Private members can only be accessed from within the class.
describe encapsulation
Encapsulation bundles data members and functions inside a single class.
Helps in data hiding and keeping related code together.
Example explanation:
In a Rectangle class, the function getArea() calculates the area using length and breadth.
The data members (length and breadth) and the function getArea() are kept together, demonstrating encapsulation.
describe polymorphism
Polymorphism means having many forms.
A single function or operator can behave differently in different situations.
1. Compile-Time Polymorphism:
Resolved at compile time
Achieved via:
Function Overloading: Multiple functions with the same name but different parameters.
Operator Overloading: Operators behave differently for different types, e.g., + for strings concatenates, for integers adds.
2. Runtime Polymorphism:
Resolved at runtime
Achieved via:
Function Overriding
Virtual Functions
Compare and contrast encapsulation and abstraction
Feature | Encapsulation | Abstraction |
Definition | Bundles data and methods into a single unit | Hides complexity, shows only relevant info |
Focus | How data and methods are organized | What an object does, not how it does it |
Compare and contrast composition and inheritance
Feature | Inheritance | Composition |
Relationship | “is-a” | “has-a” |
Mechanism | Derived class acquires properties/behaviors from base class | Class contains objects of other classes |
describe default constructors
A constructor with no arguments.
Initializes an object with default values.
If not specified, the compiler generates a default constructor.
describe parameterized constructors
A constructor that accepts arguments to initialize an object with specific values.
Syntax:
ClassName(parameters) {
// Initialization code
}
describe copy contrsuctors
Creates a new object using an existing object of the same class.
Compiler generates a default copy constructor (shallow copy).
If a class has dynamic memory, a custom copy constructor is needed.
Compare and contrast the three constructor types
Feature | Default Constructor | Parameterized Constructor | Copy Constructor |
Arguments | None | One or more | One object of the same class |
Purpose | Initialize with default values | Initialize with specific values | Create a copy of an existing object |
describe destructors
Last function called before an object is destroyed.
Characteristics:
Same name as the class preceded by ~
Cannot be overloaded, static, or const
No arguments and no return value
Automatically called when an object goes out of scope
Called when:
Function ends
Program ends
delete operator is called
Describe the role of constructors in class definitions
Automatically called when an object is created
Same name as the class
No return type
Describe the role of destructors in class definitions
Called automatically when an object goes out of scope or is deleted
Used for cleaning up resources
Describe static members/methods
Belong to the class, not any specific object
Declared with static
Can be called without creating an object
Access only static data members or other static functions
Useful for utility functions or tracking shared data
Describe operator overloading and give an example
Gives new meaning to operators when used with objects
Allows objects to behave like basic types
Reduces extra function calls
Syntax:
returnType operator symbol(arguments) { ... }
Describe pure virtual functions. What considerations do we need to make when using them?
Declared in a base class using = 0
Must be implemented in all derived classes
Useful when a base class should define the interface but not the implementation
Describe friend functions. Give a scenario in which we might use one
Friend functions/classes can access private/protected members of another class
Friendship is not mutual
Use friend keyword to declare
What distinguishes a constant class method from other methods? When should we use them?
Declared with const after the method
Cannot modify object state
Useful for read-only operations
Describe the “this” pointer and a common use for it
Implicit pointer available in non-static member functions
Points to the current object
Used to resolve naming conflicts or for method chaining
We discussed five purposes of OOP in addition to its five principles. Choose one and explain
Class: Blueprint from which objects are created, containing access specifiers, data members, and member functions
Nowadays, fewer programmers use class implementation files, preferring to define methods in the class interface file instead. Why is this?
Templates must have full code visible in header files
Allows compiler to inline small functions for performance
Header-only libraries are easier to use and maintain
Describe a way in which we might facilitate polymorphism in our code
Compile-time Polymorphism: Function/Operator Overloading
Run-time Polymorphism: Function Overriding, Virtual Functions