🔥 Simple Explanation:
- In Python, we use the
- There’s no keyword like
-
-
---
✅ A Simple Example:
🔹 Here:
-
-
And for both, the
---
🎯 Conclusion:
✅ So, always use
#Python #OOP #Class #Inheritance #PythonBasics #Programming #LearnPython
👨💻 From: https://t.iss.one/DataScienceQ
- In Python, we use the
class keyword to define any class (whether it's a base class or a child class).- There’s no keyword like
inherit, superclass, or parent in Python.-
inherit means "to inherit," but it's not a Python keyword.-
superclass and parent are just concepts, not keywords.---
✅ A Simple Example:
class Animal:
pass
class Dog(Animal):
pass
🔹 Here:
-
Animal is a base class (or parent class).-
Dog is a child class that inherits from Animal.And for both, the
class keyword is used! 🎯---
🎯 Conclusion:
✅ So, always use
class to define any class in Python (whether it's a parent or child class). #Python #OOP #Class #Inheritance #PythonBasics #Programming #LearnPython
👨💻 From: https://t.iss.one/DataScienceQ
👍3❤2
Question 7 (Intermediate):
What does the
A) Converts a method into a read-only attribute
B) Marks a function as a class method
C) Enforces type checking on variables
D) Makes a method private
#Python #OOP #Decorators #Intermediate
✅ By: https://t.iss.one/DataScienceQ
What does the
@property decorator do in Python? A) Converts a method into a read-only attribute
B) Marks a function as a class method
C) Enforces type checking on variables
D) Makes a method private
#Python #OOP #Decorators #Intermediate
✅ By: https://t.iss.one/DataScienceQ
Question 12 (Intermediate):
What is the key difference between
A) Classmethods can modify class state, staticmethods can't
B) Staticmethods are inherited, classmethods aren't
C) Classmethods receive implicit first argument (cls), staticmethods receive no special first argument
D) Classmethods are faster to execute
#Python #OOP #ClassMethod #StaticMethod
✅ By: https://t.iss.one/DataScienceQ
What is the key difference between
@classmethod and @staticmethod in Python OOP? A) Classmethods can modify class state, staticmethods can't
B) Staticmethods are inherited, classmethods aren't
C) Classmethods receive implicit first argument (cls), staticmethods receive no special first argument
D) Classmethods are faster to execute
#Python #OOP #ClassMethod #StaticMethod
✅ By: https://t.iss.one/DataScienceQ
❤3
Question 23 (Advanced):
How does Python's "Name Mangling" (double underscore prefix) work in class attribute names, and what's its practical purpose?
Options:
A) Completely hides the attribute
B) Renames it to
C) Makes it immutable
D) Converts it to a method
#Python #OOP #NameMangling #Advanced
✅ By: https://t.iss.one/DataScienceQ
How does Python's "Name Mangling" (double underscore prefix) work in class attribute names, and what's its practical purpose?
class Test:
def __init__(self):
self.public = 10
self._protected = 20
self.__private = 30 # Name mangling
obj = Test()
print(dir(obj)) # What happens to __private?
Options:
A) Completely hides the attribute
B) Renames it to
_Test__private C) Makes it immutable
D) Converts it to a method
#Python #OOP #NameMangling #Advanced
✅ By: https://t.iss.one/DataScienceQ
🚀 Comprehensive Guide: How to Prepare for a Python Job Interview – 200 Most Common Interview Questions
Are you ready: https://hackmd.io/@husseinsheikho/Python-interviews
Are you ready: https://hackmd.io/@husseinsheikho/Python-interviews
#PythonInterview #JobPrep #PythonQuestions #CodingInterview #DataStructures #Algorithms #OOP #WebDevelopment #MachineLearning #DevOps
✉️ Our Telegram channels: https://t.iss.one/addlist/0f6vfFbEMdAwODBk📱 Our WhatsApp channel: https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A
Please open Telegram to view this post
VIEW IN TELEGRAM
❤3
⁉️ Interview question
In Python, what happens when a class inherits from multiple classes that have a method with the same name, and how does the Method Resolution Order (MRO) determine which method gets called?
Simpson:
When a class inherits from multiple parent classes with a method of the same name, Python uses the **Method Resolution Order (MRO)** to decide which method is invoked. The MRO follows the **C3 linearization algorithm**, which ensures a consistent and deterministic order based on the inheritance hierarchy. This means that if you call the method, Python traverses the classes in a specific sequence defined by the MRO, starting from the child class and moving through parents in a depth-first, left-to-right order. If a method is found in one of the parent classes before others, it will be used, even if other parents also define the same method. The MRO can be inspected using `ClassName.mro()` or `help(ClassName)`. However, if there’s an ambiguity in the inheritance structure—such as a diamond pattern without proper resolution—the C3 algorithm still resolves it, but unexpected behavior may occur if not carefully designed. This makes understanding MRO crucial for complex inheritance scenarios.
#️⃣ tags: #Python #AdvancedPython #Inheritance #MethodResolutionOrder #MRO #OOP #ObjectOrientedProgramming #InterviewQuestion
By: t.iss.one/DataScienceQ 🚀
In Python, what happens when a class inherits from multiple classes that have a method with the same name, and how does the Method Resolution Order (MRO) determine which method gets called?
Simpson:
#️⃣ tags: #Python #AdvancedPython #Inheritance #MethodResolutionOrder #MRO #OOP #ObjectOrientedProgramming #InterviewQuestion
By: t.iss.one/DataScienceQ 🚀
What are the implications of using
Answer:
Using
For example:
While `__slots__` improves memory efficiency—especially in classes with many instances—it must be used carefully, particularly when dealing with inheritance or when dynamic attribute assignment is needed.
#Python #AdvancedPython #MemoryOptimization #Performance #OOP #PythonInternals
By: @DataScienceQ 🚀
__slots__ in Python classes, and how can it affect memory usage, performance, and inheritance? Answer:
Using
__slots__ in Python classes allows you to explicitly declare the attributes a class can have, which reduces memory usage by preventing the creation of a dict__dict__ for each instance. This results in faster attribute access since attributes are stored in a fixed layout rather than a dictionary. However, __slots__ restricts the ability to add new attributes dynamically, disables certain fedictike __dict__ and __weakref__, and complicates multiple inheritance because of potential conflicts between slot definitions in parent classes.For example:
class Point:
__slots__ = ['x', 'y']
def __init__(self, x, y):
self.x = x
self.y = y
p = Point(1, 2)
# p.z = 3 # This will raise an AttributeError
While `__slots__` improves memory efficiency—especially in classes with many instances—it must be used carefully, particularly when dealing with inheritance or when dynamic attribute assignment is needed.
#Python #AdvancedPython #MemoryOptimization #Performance #OOP #PythonInternals
By: @DataScienceQ 🚀
What is the difference between
Answer:
For example:
Use `@classmethod
#Python #AdvancedPython #OOP #ClassMethods #StaticMethods #PythonInternals
By: @DataScienceQ 🚀
@classmethod and @staticmethod in Python, and when should each be used? Answer:
@classmethod receives the class (cls) as its first argument and is used to define methods that operate on the class itself rather than instances. It can modify class state or create alternative constructors. @staticmethod, on the other hand, does not receive any implicit first argument (neither self nor cls) and behaves like a regular function bound to the class namespace. It cannot access or modify class or instance state.For example:
class MyClass:
count = 0
def __init__(self):
MyClass.count += 1
@classmethod
def get_count(cls):
return cls.count
@staticmethod
def helper_method(x):
return x * 2
print(MyClass.get_count()) # 0 initially
obj = MyClass()
print(MyClass.get_count()) # 1
print(MyClass.helper_method(5)) # 10
Use `@classmethod
for factory methods or operations affecting the class, and @staticmethod` for utility functions logically related to the class but independent of its state.#Python #AdvancedPython #OOP #ClassMethods #StaticMethods #PythonInternals
By: @DataScienceQ 🚀
What is the purpose of
Answer:
The
For example:
By overriding
#Python #AdvancedPython #Metaclasses #OOP #PythonInternals #CustomClassCreation
By: @DataScienceQ 🚀
__prepare__ in Python metaclasses, and how does it influence the creation of class dictionaries? Answer:
The
__prepare__ method is a class method defined in a metaclass that allows custom control over the namespace dictionary used when creating a new class. It is called before the class body executes and returns a dictionary-like object (e.g., dict, OrderedDict) that will serve as the class namespace. This enables metaclasses to define custom behaviors for attribute ordering, validation, or even use non-standard data structures.For example:
class OrderedMeta(type):
@classmethod
def __prepare__(cls, name, bases, **kwargs):
return OrderedDict()
class MyClass(metaclass=OrderedMeta):
a = 1
b = 2
print(list(MyClass.__dict__.keys())) # ['a', 'b'] - ordered
By overriding
__prepare__, you can ensure that class attributes are stored in a specific order or with additional constraints, making it powerful for frameworks requiring predictable attribute behavior.#Python #AdvancedPython #Metaclasses #OOP #PythonInternals #CustomClassCreation
By: @DataScienceQ 🚀
How can you design a Python class to represent a geometric shape (e.g., Circle, Rectangle) with inheritance and method overriding, ensuring each shape calculates its area and perimeter correctly? Implement a base class
Answer: The question explores object-oriented programming concepts in Python using inheritance and abstraction. The solution defines an abstract base class
#Python #OOP #Inheritance #Polymorphism #Abstraction #GeometricShapes #Programming #Academic #IntermediateLevel #ObjectOriented
By: @DataScienceQ 🚀
Shape with abstract methods for area and perimeter, then create derived classes for Circle and Rectangle. Include validation for input parameters and demonstrate polymorphism by storing multiple shapes in a list and iterating through them to calculate total area and perimeter.from abc import ABC, abstractmethod
import math
class Shape(ABC):
"""Abstract base class for geometric shapes."""
@abstractmethod
def area(self) -> float:
"""Calculate the area of the shape."""
pass
@abstractmethod
def perimeter(self) -> float:
"""Calculate the perimeter of the shape."""
pass
class Circle(Shape):
"""Represents a circle with a given radius."""
def __init__(self, radius: float):
if radius <= 0:
raise ValueError("Radius must be positive.")
self.radius = radius
def area(self) -> float:
return math.pi * self.radius ** 2
def perimeter(self) -> float:
return 2 * math.pi * self.radius
class Rectangle(Shape):
"""Represents a rectangle with width and height."""
def __init__(self, width: float, height: float):
if width <= 0 or height <= 0:
raise ValueError("Width and height must be positive.")
self.width = width
self.height = height
def area(self) -> float:
return self.width * self.height
def perimeter(self) -> float:
return 2 * (self.width + self.height)
# Example usage
shapes = [
Circle(5),
Rectangle(4, 6),
Circle(3),
Rectangle(7, 2)
]
total_area = 0
total_perimeter = 0
for shape in shapes:
total_area += shape.area()
total_perimeter += shape.perimeter()
print(f"Total Area: {total_area:.2f}")
print(f"Total Perimeter: {total_perimeter:.2f}")
# Demonstrate polymorphism
for shape in shapes:
print(f"{shape.__class__.__name__}: Area = {shape.area():.2f}, Perimeter = {shape.perimeter():.2f}")
Answer: The question explores object-oriented programming concepts in Python using inheritance and abstraction. The solution defines an abstract base class
Shape with two abstract methods (area and perimeter) that must be implemented by all derived classes. Two concrete classes, Circle and Rectangle, inherit from Shape and provide their own implementations of the required methods. Input validation is enforced through error checking in the constructors. The example demonstrates polymorphism by storing different shape types in a single list and processing them uniformly. This approach promotes code reusability, maintainability, and extensibility, making it ideal for academic and real-world applications involving geometric calculations.#Python #OOP #Inheritance #Polymorphism #Abstraction #GeometricShapes #Programming #Academic #IntermediateLevel #ObjectOriented
By: @DataScienceQ 🚀
❤2
#Python #InterviewQuestion #OOP #Inheritance #Polymorphism #Programming #CodingExample
Question:
How does method overriding work in Python, and can you demonstrate it using a real-world example involving a base class
Answer:
Method overriding in Python allows a subclass to provide a specific implementation of a method that is already defined in its superclass. This enables polymorphism, where objects of different classes can be treated as instances of the same class through a common interface.
Here’s an example demonstrating method overriding with
Explanation:
- The
- Both
- The
- When called with a
This demonstrates how method overriding supports flexible and extensible code design in object-oriented programming.
By: @DataScienceQ 🚀
Question:
How does method overriding work in Python, and can you demonstrate it using a real-world example involving a base class
Animal and derived classes Dog and Cat?Answer:
Method overriding in Python allows a subclass to provide a specific implementation of a method that is already defined in its superclass. This enables polymorphism, where objects of different classes can be treated as instances of the same class through a common interface.
Here’s an example demonstrating method overriding with
Animal, Dog, and Cat:class Animal:
def make_sound(self):
pass # Abstract method
class Dog(Animal):
def make_sound(self):
return "Woof!"
class Cat(Animal):
def make_sound(self):
return "Meow!"
# Function to demonstrate polymorphism
def animal_sound(animal):
print(animal.make_sound())
# Create instances
dog = Dog()
cat = Cat()
# Call the method
animal_sound(dog) # Output: Woof!
animal_sound(cat) # Output: Meow!
Explanation:
- The
Animal class defines an abstract make_sound() method.- Both
Dog and Cat inherit from Animal and override make_sound() with their own implementations.- The
animal_sound() function accepts any object that has a make_sound() method, showcasing polymorphism.- When called with a
Dog or Cat instance, the appropriate overridden method is executed based on the object type.This demonstrates how method overriding supports flexible and extensible code design in object-oriented programming.
By: @DataScienceQ 🚀
❤3
#Python #InterviewQuestion #OOP #Inheritance #Polymorphism #Programming #CodingChallenge
Question:
How does method resolution order (MRO) work in Python when multiple inheritance is involved, and can you provide a code example to demonstrate the diamond problem and how Python resolves it using C3 linearization?
Answer:
In Python, method resolution order (MRO) determines the sequence in which base classes are searched when executing a method. When multiple inheritance is used, especially in cases like the "diamond problem" (where a class inherits from two classes that both inherit from a common base), Python uses the C3 linearization algorithm to establish a consistent MRO.
The C3 linearization ensures that:
- The subclass appears before its parents.
- Parents appear in the order they are listed.
- A parent class appears before any of its ancestors.
Here’s an example demonstrating the diamond problem and how Python resolves it:
Output:
Explanation:
- The
- Without proper MRO, calling
- Python uses C3 linearization to compute MRO as:
- Since
- This avoids the diamond problem by ensuring a deterministic and predictable order.
This mechanism allows developers to write complex class hierarchies without runtime ambiguity, making Python's multiple inheritance safe and usable.
By: @DataScienceQ 🚀
Question:
How does method resolution order (MRO) work in Python when multiple inheritance is involved, and can you provide a code example to demonstrate the diamond problem and how Python resolves it using C3 linearization?
Answer:
In Python, method resolution order (MRO) determines the sequence in which base classes are searched when executing a method. When multiple inheritance is used, especially in cases like the "diamond problem" (where a class inherits from two classes that both inherit from a common base), Python uses the C3 linearization algorithm to establish a consistent MRO.
The C3 linearization ensures that:
- The subclass appears before its parents.
- Parents appear in the order they are listed.
- A parent class appears before any of its ancestors.
Here’s an example demonstrating the diamond problem and how Python resolves it:
class A:
def process(self):
print("A.process")
class B(A):
def process(self):
print("B.process")
class C(A):
def process(self):
print("C.process")
class D(B, C):
pass
# Check MRO
print("MRO of D:", [cls.__name__ for cls in D.mro()])
# Output: ['D', 'B', 'C', 'A', 'object']
# Call the method
d = D()
d.process()
Output:
MRO of D: ['D', 'B', 'C', 'A', 'object']
B.process
Explanation:
- The
D class inherits from B and C, both of which inherit from A.- Without proper MRO, calling
d.process() could lead to ambiguity (e.g., should it call B.process or C.process?).- Python uses C3 linearization to compute MRO as:
D -> B -> C -> A -> object.- Since
B comes before C in the inheritance list, B.process is called first.- This avoids the diamond problem by ensuring a deterministic and predictable order.
This mechanism allows developers to write complex class hierarchies without runtime ambiguity, making Python's multiple inheritance safe and usable.
By: @DataScienceQ 🚀
❤1
In Python, abstract base classes (ABCs) in the
#python #OOP #classes #abc #inheritance
👉 @DataScience4
abc module define interfaces for subclasses to implement, enforcing polymorphism and preventing instantiation of incomplete classes. Use them for designing robust class hierarchies where specific methods must be overridden.from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
# rect = Rectangle(5, 3)
# print(rect.area()) # 15
# Shape() # Error: Can't instantiate abstract class
#python #OOP #classes #abc #inheritance
👉 @DataScience4
❤3
In Python, Object-Oriented Programming (OOP) allows you to define classes and create objects with attributes and methods. Classes are blueprints for creating objects, and they support key concepts like inheritance, encapsulation, polymorphism, and abstraction.
#Python #OOP #Classes #Inheritance #Polymorphism #Encapsulation #Programming #ObjectOriented #PythonTips #CodeExamples
By: @DataScienceQ🚀
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return f"{self.name} makes a sound"
class Dog(Animal):
def speak(self):
return f"{self.name} says Woof!"
class Cat(Animal):
def speak(self):
return f"{self.name} says Meow!"
# Creating instances
dog = Dog("Buddy")
cat = Cat("Whiskers")
print(dog.speak()) # Output: Buddy says Woof!
print(cat.speak()) # Output: Whiskers says Meow!
#Python #OOP #Classes #Inheritance #Polymorphism #Encapsulation #Programming #ObjectOriented #PythonTips #CodeExamples
By: @DataScienceQ
Please open Telegram to view this post
VIEW IN TELEGRAM
👍1🔥1
In Python, "Magic Methods" (also known as Dunder methods, short for "double underscore") are special methods that allow you to define how objects of your class behave with built-in functions and operators. While
Output:
#Python #MagicMethods #DunderMethods #OOP #Classes #PythonTips #CodeExamples #StringRepresentation #ObjectOrientation #Programming
---
By: @DataScienceQ ✨
init handles object initialization, str and repr are crucial for defining an object's string representation.str: Returns a "user-friendly" string representation of an object, primarily for human readability (e.g., when print() is called).repr: Returns an "official" string representation of an object, primarily for developers, often aiming to be unambiguous and allow recreation of the object.class Book:
def init(self, title, author, year):
self.title = title
self.author = author
self.year = year
def str(self):
return f'"{self.title}" by {self.author} ({self.year})'
def repr(self):
return f"Book('{self.title}', '{self.author}', {self.year})"
Creating an instance
my_book = Book("The Hitchhiker's Guide to the Galaxy", "Douglas Adams", 1979)
str is used by print()
print(my_book)
repr is used by the interpreter or explicitly with repr()
print(repr(my_book))
In collections, repr is used by default
bookshelf = [my_book, Book("Pride and Prejudice", "Jane Austen", 1813)]
print(bookshelf)
Output:
"The Hitchhiker's Guide to the Galaxy" by Douglas Adams (1979)
Book('The Hitchhiker\'s Guide to the Galaxy', 'Douglas Adams', 1979)
[Book('The Hitchhiker\'s Guide to the Galaxy', 'Douglas Adams', 1979), Book('Pride and Prejudice', 'Jane Austen', 1813)]
#Python #MagicMethods #DunderMethods #OOP #Classes #PythonTips #CodeExamples #StringRepresentation #ObjectOrientation #Programming
---
By: @DataScienceQ ✨
Python Tip: Mastering
When defining a class in Python,
The
In the
Let's create some cars:
Output:
#PythonTip #OOP #Classes #InitMethod #SelfKeyword #ObjectOriented #PythonProgramming
---
By: @DataScienceQ ✨
init and self in OOP! 🐍When defining a class in Python,
init is a special method (often called the constructor) that gets called automatically every time a new object (instance) of the class is created. It's used to set up the initial state or attributes of that object.The
self parameter is a convention and the first parameter of any instance method. It always refers to the instance of the class itself, allowing you to access its attributes and other methods from within the class.class Car:
def init(self, make, model, year):
self.make = make # Assign 'make' to the instance's 'make' attribute
self.model = model # Assign 'model' to the instance's 'model' attribute
self.year = year # Assign 'year' to the instance's 'year' attribute
def get_description(self):
return f"This is a {self.year} {self.make} {self.model}."
In the
init method, self.make = make means "take the value passed in as make and assign it to the make attribute of this specific Car object."Let's create some cars:
my_car = Car("Toyota", "Camry", 2020)
your_car = Car("Honda", "Civic", 2022)
print(my_car.get_description())
print(your_car.get_description())Output:
This is a 2020 Toyota Camry.
This is a 2022 Honda Civic.
init ensures each object starts with its own data, and self connects you to that data!#PythonTip #OOP #Classes #InitMethod #SelfKeyword #ObjectOriented #PythonProgramming
---
By: @DataScienceQ ✨
Python OOP Tip: Inheritance Basics! 🚀
Inheritance allows a new class (child) to acquire properties and methods from an existing class (parent), promoting code reuse and establishing an "is-a" relationship.
Key Takeaway: Use
#Python #OOP #Inheritance #PythonTips #Programming
---
By: @DataScienceQ ✨
Inheritance allows a new class (child) to acquire properties and methods from an existing class (parent), promoting code reuse and establishing an "is-a" relationship.
class Vehicle:
def init(self, brand):
self.brand = brand
def description(self):
return f"This is a {self.brand} vehicle."
class Car(Vehicle): # Car inherits from Vehicle
def init(self, brand, model):
super().init(brand) # Call parent's constructor
self.model = model
def drive(self):
return f"The {self.brand} {self.model} is driving."
my_car = Car("Toyota", "Camry")
print(my_car.description())
print(my_car.drive())
Key Takeaway: Use
super().init() in a child class to properly initialize parent attributes when overriding the constructor.#Python #OOP #Inheritance #PythonTips #Programming
---
By: @DataScienceQ ✨
❤2
🧠 Quiz: Which keyword is used to implement inheritance between classes in Java?
A)
B)
C)
D)
✅ Correct answer:B
Explanation:In Java, the keyword is used to indicate that a class is inheriting from another class, forming an "is-a" relationship. The implements keyword is used for interfaces.
#Java #OOP #Inheritance
━━━━━━━━━━━━━━━
By: @DataScienceQ ✨
A)
implementsB)
extendsC)
inheritsD)
uses✅ Correct answer:
Explanation:
extends#Java #OOP #Inheritance
━━━━━━━━━━━━━━━
By: @DataScienceQ ✨
#include <iostream>
#include <vector>
int main() {
std::vector<int> numbers;
numbers.push_back(5);
numbers.push_back(10);
std::cout << "Vector size: " << numbers.size();
return 0;
}
Vector size: 2
#34.
.size()Member function that returns the number of elements in a container like
std::vector or std::string.#include <iostream>
#include <vector>
int main() {
std::vector<std::string> fruits = {"Apple", "Banana"};
std::cout << "There are " << fruits.size() << " fruits.";
return 0;
}
There are 2 fruits.
#35.
.length()A member function of
std::string that returns its length. It's synonymous with .size().#include <iostream>
#include <string>
int main() {
std::string text = "C++";
std::cout << "The length of the string is: " << text.length();
return 0;
}
The length of the string is: 3
---
#CPP #STL #Algorithms
#36.
#include <algorithm>Includes the standard library algorithms, like sort, find, copy, etc.
#include <iostream>
#include <vector>
#include <algorithm> // Required for std::sort
int main() {
std::vector<int> nums = {3, 1, 4};
std::sort(nums.begin(), nums.end());
std::cout << "Sorting is possible with <algorithm>.";
return 0;
}
Sorting is possible with <algorithm>.
#37.
std::sort()Sorts the elements in a range (e.g., a vector).
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> nums = {50, 20, 40, 10, 30};
std::sort(nums.begin(), nums.end());
for(int n : nums) {
std::cout << n << " ";
}
return 0;
}
10 20 30 40 50
#38.
.begin()Returns an iterator pointing to the first element in a container.
#include <iostream>
#include <vector>
int main() {
std::vector<int> nums = {100, 200, 300};
auto it = nums.begin();
std::cout << "First element: " << *it;
return 0;
}
First element: 100
#39.
.end()Returns an iterator referring to the past-the-end element in the container.
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> nums = {1, 2, 3};
// .end() points after the last element, used as a boundary
std::cout << "Vector has elements until the end.";
return 0;
}
Vector has elements until the end.
#40.
#defineA preprocessor directive used to create macros or symbolic constants.
#include <iostream>
#define PI 3.14159
int main() {
std::cout << "The value of PI is " << PI;
return 0;
}
The value of PI is 3.14159
---
#CPP #OOP #Classes
#41.
classA keyword used to declare a class, which is a blueprint for creating objects.
#include <iostream>
class Dog {
public:
void bark() {
std::cout << "Woof!";
}
};
int main() {
Dog myDog;
myDog.bark();
return 0;
}
Woof!
#42.
structSimilar to a class, but its members are public by default.
❤1
#include <iostream>
struct Point {
int x;
int y;
};
int main() {
Point p;
p.x = 10;
p.y = 20;
std::cout << "Point: (" << p.x << ", " << p.y << ")";
return 0;
}
Point: (10, 20)
#43.
publicAn access specifier that makes class members accessible from outside the class.
#include <iostream>
class MyClass {
public: // Accessible from anywhere
int myNum = 10;
};
int main() {
MyClass obj;
std::cout << obj.myNum;
return 0;
}
10
#44.
privateAn access specifier that makes class members accessible only from within the class itself.
#include <iostream>
class MyClass {
private:
int secret = 42;
public:
void printSecret() {
std::cout << secret; // Accessible from within the class
}
};
int main() {
MyClass obj;
// std::cout << obj.secret; // This would cause a compile error
obj.printSecret();
return 0;
}
42
#45. Constructor
A special member function of a class that is executed whenever a new object of that class is created.
#include <iostream>
class Car {
public:
// Constructor
Car() {
std::cout << "Car object created.";
}
};
int main() {
Car myCar; // Constructor is called here
return 0;
}
Car object created.
---
#CPP #OOP #MemoryManagement
#46. Destructor
A special member function that is executed automatically when an object is destroyed.
#include <iostream>
class MyClass {
public:
// Destructor
~MyClass() {
std::cout << "Object destroyed.";
}
};
int main() {
MyClass obj;
// Destructor is called when main() ends
return 0;
}
Object destroyed.
#47.
thisA keyword that refers to the current instance of the class.
#include <iostream>
class Box {
private:
int length;
public:
Box(int length) {
this->length = length; // Use 'this' to distinguish member from parameter
}
void printLength() {
std::cout << "Length: " << this->length;
}
};
int main() {
Box b(10);
b.printLength();
return 0;
}
Length: 10
#48.
newAn operator that allocates memory on the heap and returns a pointer to it.
#include <iostream>
int main() {
int* ptr = new int; // Allocate an integer on the heap
*ptr = 100;
std::cout << "Value from heap: " << *ptr;
delete ptr; // Must deallocate memory
return 0;
}
Value from heap: 100
#49.
deleteAn operator that deallocates memory previously allocated with
new.#include <iostream>
int main() {
int* ptr = new int(55);
std::cout << *ptr << " allocated. ";
delete ptr; // Deallocate the memory
std::cout << "Memory freed.";
// Accessing ptr now is undefined behavior
return 0;
}
55 allocated. Memory freed.
#50.
nullptrRepresents a null pointer literal. It indicates that a pointer does not point to any valid memory location.
#include <iostream>
int main() {
int* ptr = nullptr;
if (ptr == nullptr) {
std::cout << "The pointer is null.";
}
return 0;
}
The pointer is null.
━━━━━━━━━━━━━━━
By: @DataScienceQ ✨
❤1