diff --git a/docs/programming-fundamentals/language-syntax/arrays.md b/docs/programming-fundamentals/language-syntax/arrays.md index 32afd27d..dbc58dc1 100644 --- a/docs/programming-fundamentals/language-syntax/arrays.md +++ b/docs/programming-fundamentals/language-syntax/arrays.md @@ -172,6 +172,7 @@ graph LR; D -->|Index 3| E[Orange]; ``` +
This representation shows how each element in an array can be accessed by its index. diff --git a/docs/programming-fundamentals/language-syntax/classes.md b/docs/programming-fundamentals/language-syntax/classes.md new file mode 100644 index 00000000..1f9e459b --- /dev/null +++ b/docs/programming-fundamentals/language-syntax/classes.md @@ -0,0 +1,254 @@ +--- +id: classes +sidebar_position: 8 +title: Classes +sidebar_label: Classes +description: "Learn about classes and object-oriented programming concepts in JavaScript, Java, Python, and C++. Understand how to create, instantiate, and work with classes effectively in these popular languages." +tags: [classes, object-oriented programming, programming, syntax, js, java, python, cpp] +--- + +Classes are fundamental to object-oriented programming (OOP). They provide a blueprint for creating objects that encapsulate data and behavior. This guide covers the basics and key aspects of classes in JavaScript, Java, Python, and C++ with practical examples. + + + +## What is a Class? + +A class is a template for creating objects (instances) that share common properties and methods. It defines the structure and behavior of the objects. Classes promote code reusability and help in organizing code in a modular way. + +## Classes in Different Languages + + + + +### JavaScript Classes Overview + +JavaScript introduced class syntax in ES6. While it is syntactic sugar over JavaScript’s existing prototype-based inheritance, it provides a clear and readable way to create objects. + +#### Class Declaration + +```js title="JavaScript Class Example" +// Declaration of a class +class Person { + constructor(name, age) { + this.name = name; + this.age = age; + } + + // Method + greet() { + console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`); + } +} + +// Instantiation +const person1 = new Person('Alice', 30); +person1.greet(); // Output: Hello, my name is Alice and I am 30 years old. +``` + +#### Inheritance + +```js title="JavaScript Inheritance Example" +class Employee extends Person { + constructor(name, age, jobTitle) { + super(name, age); // Call the parent class constructor + this.jobTitle = jobTitle; + } + + describeJob() { + console.log(`I am a ${this.jobTitle}.`); + } +} + +const employee1 = new Employee('Bob', 25, 'Software Developer'); +employee1.greet(); // Output: Hello, my name is Bob and I am 25 years old. +employee1.describeJob(); // Output: I am a Software Developer. +``` + + + + + +### Java Classes Overview + +In Java, classes are the building blocks of OOP. They encapsulate data (fields) and behavior (methods). + +#### Class Declaration + +```java title="Java Class Example" +public class Person { + private String name; + private int age; + + // Constructor + public Person(String name, int age) { + this.name = name; + this.age = age; + } + + // Method + public void greet() { + System.out.println("Hello, my name is " + name + " and I am " + age + " years old."); + } +} + +// Instantiation +public class Main { + public static void main(String[] args) { + Person person1 = new Person("Alice", 30); + person1.greet(); // Output: Hello, my name is Alice and I am 30 years old. + } +} +``` + +#### Inheritance + +```java title="Java Inheritance Example" +public class Employee extends Person { + private String jobTitle; + + public Employee(String name, int age, String jobTitle) { + super(name, age); // Call the parent class constructor + this.jobTitle = jobTitle; + } + + public void describeJob() { + System.out.println("I am a " + jobTitle + "."); + } +} + +// Usage +public class Main { + public static void main(String[] args) { + Employee employee1 = new Employee("Bob", 25, "Software Developer"); + employee1.greet(); // Output: Hello, my name is Bob and I am 25 years old. + employee1.describeJob(); // Output: I am a Software Developer. + } +} +``` + + + + + +### Python Classes Overview + +In Python, classes are easy to define and work with, thanks to its simple and intuitive syntax. + +#### Class Declaration + +```python title="Python Class Example" +class Person: + def __init__(self, name, age): + self.name = name + self.age = age + + def greet(self): + print(f"Hello, my name is {self.name} and I am {self.age} years old.") + +# Instantiation +person1 = Person("Alice", 30) +person1.greet() # Output: Hello, my name is Alice and I am 30 years old. +``` + +#### Inheritance + +```python title="Python Inheritance Example" +class Employee(Person): + def __init__(self, name, age, job_title): + super().__init__(name, age) # Call the parent class constructor + self.job_title = job_title + + def describe_job(self): + print(f"I am a {self.job_title}.") + +# Usage +employee1 = Employee("Bob", 25, "Software Developer") +employee1.greet() # Output: Hello, my name is Bob and I am 25 years old. +employee1.describe_job() # Output: I am a Software Developer. +``` + + + + + +### C++ Classes Overview + +C++ provides robust support for OOP, allowing you to create complex programs using classes. + +#### Class Declaration + +```cpp title="C++ Class Example" +#include +#include + +class Person { +private: + std::string name; + int age; + +public: + // Constructor + Person(std::string name, int age) : name(name), age(age) {} + + // Method + void greet() const { + std::cout << "Hello, my name is " << name << " and I am " << age << " years old." << std::endl; + } +}; + +// Instantiation +int main() { + Person person1("Alice", 30); + person1.greet(); // Output: Hello, my name is Alice and I am 30 years old. + return 0; +} +``` + +#### Inheritance + +```cpp title="C++ Inheritance Example" +class Employee : public Person { +private: + std::string jobTitle; + +public: + Employee(std::string name, int age, std::string jobTitle) : Person(name, age), jobTitle(jobTitle) {} + + void describeJob() const { + std::cout << "I am a " << jobTitle << "." << std::endl; + } +}; + +// Usage +int main() { + Employee employee1("Bob", 25, "Software Developer"); + employee1.greet(); // Output: Hello, my name is Bob and I am 25 years old. + employee1.describeJob(); // Output: I am a Software Developer. + return 0; +} +``` + + + + + + +## Best Practices + +1. **Encapsulation**: Keep class attributes private and use methods to modify them safely. +2. **Inheritance**: Use inheritance to promote code reusability and hierarchy. +3. **Polymorphism**: Implement polymorphism to enhance flexibility and maintainability. +4. **Constructor Overloading**: In languages that support it, provide multiple constructors for different initialization needs. + + +## Conclusion + +Classes are a core concept in OOP, enabling you to model real-world entities effectively. Understanding how to create, instantiate, and work with classes is essential for building robust and maintainable software applications. + + + +--- + +

Feedback and Support

+ + \ No newline at end of file diff --git a/docs/programming-fundamentals/language-syntax/objects.md b/docs/programming-fundamentals/language-syntax/objects.md new file mode 100644 index 00000000..0bac0f7d --- /dev/null +++ b/docs/programming-fundamentals/language-syntax/objects.md @@ -0,0 +1,176 @@ +--- +id: objects +sidebar_position: 9 +title: Objects +sidebar_label: Objects +description: Learn about objects in JavaScript, Java, Python, and C++. Understand how to define, create, and use objects effectively in these popular programming languages. +tags: [objects, object-oriented programming, programming, syntax, js, java, python, cpp] +keywords: [objects, object-oriented programming, classes, instances, data, methods, properties] +--- + +Objects are instances created from classes or defined directly. They are key to object-oriented programming (OOP) as they encapsulate both state (data) and behavior (methods). This section covers how to define, create, and use objects in JavaScript, Java, Python, and C++ with practical examples. + + + +## What is an Object? + +An object is a self-contained entity that consists of properties (attributes) and methods (functions). It is a concrete representation of a class (or standalone structure) in memory, holding specific data and functionalities. + +## Objects in Different Languages + + + + +### JavaScript Objects Overview + +In JavaScript, objects can be created using literal notation or class instantiation. + +#### Object Literal + +```js title="JavaScript Object Example" +// Creating an object using literal notation +const person = { + name: 'Alice', + age: 30, + greet: function() { + console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`); + } +}; + +person.greet(); // Output: Hello, my name is Alice and I am 30 years old. +``` + +#### Object from a Class + +```js title="JavaScript Class Example" +class Person { + constructor(name, age) { + this.name = name; + this.age = age; + } + + greet() { + console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`); + } +} + +const person1 = new Person('Bob', 25); +person1.greet(); // Output: Hello, my name is Bob and I am 25 years old. +``` + + + + + +### Java Objects Overview + +In Java, objects are created by instantiating classes using the `new` keyword. + +#### Creating an Object + +```java title="Java Object Example" +public class Person { + private String name; + private int age; + + public Person(String name, int age) { + this.name = name; + this.age = age; + } + + public void greet() { + System.out.println("Hello, my name is " + name + " and I am " + age + " years old."); + } +} + +public class Main { + public static void main(String[] args) { + Person person1 = new Person("Alice", 30); + person1.greet(); // Output: Hello, my name is Alice and I am 30 years old. + } +} +``` + + + + + +### Python Objects Overview + +Objects in Python are straightforward to create by instantiating classes. + +#### Creating an Object + +```python title="Python Object Example" +class Person: + def __init__(self, name, age): + self.name = name + self.age = age + + def greet(self): + print(f"Hello, my name is {self.name} and I am {self.age} years old.") + +# Creating an instance of the class +person1 = Person("Alice", 30) +person1.greet() # Output: Hello, my name is Alice and I am 30 years old. +``` + + + + + +### C++ Objects Overview + +In C++, objects are created by instantiating classes defined in your code. + +#### Creating an Object + +```cpp title="C++ Object Example" +#include +#include + +class Person { +private: + std::string name; + int age; + +public: + Person(std::string name, int age) : name(name), age(age) {} + + void greet() const { + std::cout << "Hello, my name is " << name << " and I am " << age << " years old." << std::endl; + } +}; + +int main() { + Person person1("Alice", 30); + person1.greet(); // Output: Hello, my name is Alice and I am 30 years old. + return 0; +} +``` + + + + + + +## Key Characteristics of Objects + +1. **State**: The properties or fields that hold the data. +2. **Behavior**: The methods that define what actions the object can perform. +3. **Identity**: A unique reference to distinguish each object. + +Understanding how to effectively create and use objects will empower you to write robust, maintainable, and modular code across programming languages. + + +## Conclusion + +Objects are fundamental to object-oriented programming, encapsulating both data and behavior. By mastering the creation and use of objects in JavaScript, Java, Python, and C++, you can build powerful applications that model real-world entities effectively. This guide has provided you with the essential knowledge and examples to get started with objects in these popular programming languages. + + + +--- + +

Feedback and Support

+ + \ No newline at end of file diff --git a/docs/programming-fundamentals/language-syntax/strings.md b/docs/programming-fundamentals/language-syntax/strings.md new file mode 100644 index 00000000..54c2e2a5 --- /dev/null +++ b/docs/programming-fundamentals/language-syntax/strings.md @@ -0,0 +1,191 @@ +--- +id: strings +sidebar_position: 7 +title: Strings +sidebar_label: Strings +description: "Learn about strings in JavaScript, Java, Python, and C++. Understand how to create, manipulate, and use strings effectively in programming." +tags: [strings, data structures, programming, syntax, js, java, python, cpp] +--- + +Strings are a sequence of characters used to represent text. They are an essential data type in all programming languages and are often used for storing and manipulating text data. This guide covers how to work with strings in JavaScript, Java, Python, and C++ with practical examples and best practices. + + + +## What is a String? + +A string is a data structure that holds a sequence of characters, such as letters, numbers, and symbols. Strings are immutable in many programming languages, meaning their content cannot be changed once created. Understanding how to create, manipulate, and perform operations on strings is crucial for text processing and data handling. + +## Strings in Different Languages + + + + +### JavaScript Strings Overview + +In JavaScript, strings can be enclosed in single quotes (`'`), double quotes (`"`), or template literals (`` ` ``) for multi-line strings and interpolation. + +#### Declaration and Initialization + +```js title="JavaScript String Example" +// Declaration +let singleQuoteString = 'Hello, world!'; +let doubleQuoteString = "JavaScript is fun!"; +let templateString = `This is a template literal.`; + +// String Interpolation +let name = "Alice"; +let greeting = `Hello, ${name}!`; +console.log(greeting); // Output: Hello, Alice! +``` + +#### Common String Methods + +- **`length`**: Returns the length of the string. +- **`toUpperCase()`**: Converts the string to uppercase. +- **`toLowerCase()`**: Converts the string to lowercase. +- **`slice()`**: Extracts a section of the string. +- **`split()`**: Splits the string into an array based on a delimiter. + +```js title="JavaScript String Methods Example" +let message = "JavaScript"; +console.log(message.length); // Output: 10 +console.log(message.toUpperCase()); // Output: JAVASCRIPT +console.log(message.slice(0, 4)); // Output: Java +``` + + + + + +### Java Strings Overview + +In Java, strings are objects of the `String` class and are immutable. Java provides a rich set of methods to manipulate strings efficiently. + +#### Declaration and Initialization + +```java title="Java String Example" +// Declaration +String greeting = "Hello, Java!"; +String name = new String("Alice"); + +// String concatenation +String combined = greeting + " Welcome, " + name + "!"; +System.out.println(combined); // Output: Hello, Java! Welcome, Alice! +``` + +#### Common String Methods + +- **`length()`**: Returns the length of the string. +- **`toUpperCase()`**: Converts the string to uppercase. +- **`toLowerCase()`**: Converts the string to lowercase. +- **`substring()`**: Extracts a substring. +- **`charAt()`**: Returns the character at a specified index. + +```java title="Java String Methods Example" +String text = "Programming"; +System.out.println(text.length()); // Output: 11 +System.out.println(text.charAt(0)); // Output: P +System.out.println(text.substring(0, 6)); // Output: Progra +``` + + + + + +### Python Strings Overview + +In Python, strings are a built-in data type and are immutable. Python provides various methods to manipulate strings efficiently. + +#### Declaration and Initialization + +```python title="Python String Example" +# Declaration +single_quote_string = 'Hello, Python!' +double_quote_string = "Python is powerful!" +triple_quote_string = '''This is a multi-line string.''' + +# String interpolation using f-strings +name = "Alice" +greeting = f"Hello, {name}!" +print(greeting) # Output: Hello, Alice! +``` + +#### Common String Methods + +- **`len()`**: Returns the length of the string. +- **`upper()`**: Converts the string to uppercase. +- **`lower()`**: Converts the string to lowercase. +- **`split()`**: Splits the string into a list. +- **`find()`**: Finds the first occurrence of a substring. + +```python title="Python String Methods Example" +text = "Hello, World!" +print(len(text)) # Output: 13 +print(text.upper()) # Output: HELLO, WORLD! +print(text.split(", ")) # Output: ['Hello', 'World!'] +``` + + + + + +### C++ Strings Overview + +In C++, strings can be handled using C-style character arrays or the `std::string` class from the Standard Library. + +#### Declaration and Initialization + +```cpp title="C++ String Example" +#include +#include + +int main() { + // Declaration using std::string + std::string greeting = "Hello, C++!"; + + // String concatenation + std::string name = "Alice"; + std::string fullGreeting = greeting + " Welcome, " + name + "!"; + + std::cout << fullGreeting << std::endl; // Output: Hello, C++! Welcome, Alice! + return 0; +} +``` + +#### Common String Methods + +- **`length()` / `size()`**: Returns the length of the string. +- **`substr()`**: Extracts a substring. +- **`find()`**: Finds the first occurrence of a substring. +- **`append()`**: Appends a string. + +```cpp title="C++ String Methods Example" +std::string text = "C++ Programming"; +std::cout << text.length() << std::endl; // Output: 15 +std::cout << text.substr(0, 3) << std::endl; // Output: C++ +std::cout << text.find("Program") << std::endl; // Output: 4 +``` + + + + + + +## Best Practices + +1. **Use Immutable Strings for Safety**: In most languages, strings are immutable, ensuring that any modification creates a new string. +2. **Optimize String Concatenation**: Use language-specific methods for efficient string concatenation (e.g., `StringBuilder` in Java). +3. **Handle Edge Cases**: Be aware of special characters, empty strings, and null values when manipulating strings. + + +## Conclusion + +Strings are a fundamental data type in programming languages, used for representing text data. Understanding how to create, manipulate, and work with strings is essential for developing applications that handle textual information effectively. By following the examples and best practices in this guide, you can master the use of strings in JavaScript, Java, Python, and C++. + + + +--- + +

Feedback and Support

+ +