C ++ day 15: polymorphism and virtual work 🔹 1. What is polymorphism? Polytestrality = "many forms". In C ++, it is mainly obtained: Ceremony surcharge (pre -covered) Ceremony overriding Virtual work 🔹 2. Function Overroid (Ricap) CPP Copy edit Class base { public: Zero greetings () {cout << "Hello base \ n"; , , Class derivative: Public base { public: Zero greetings () {cout << "Hello derived \ n"; , , 🔹 3. Virtual work Runtime allows polymorphism. Call the correct function through base class pointer/reference. CPP Copy edit Class animal { public: Virtual zero sound () { cout << "Animal sound \ N"; , , Class Dog: Public Animal { public: Zero sound () override { cout << "dog barks \ n"; , , int main() { Animal* a = new dog (); A-> sound (); // Dog bark Remove A; , 🔹 4. Pure virtual work and abstract class CPP Copy edit Class size { public: ...
Great progress! Let’s jump into Day 4 of C++ learning. Today, you'll explore arrays, strings, and basic loops with data structures.
C++ Day 4: Arrays and Strings
🔹 1. Arrays
An array is a collection of elements of the same data type, stored in contiguous memory locations.
✅ Declaration:
int numbers[5]; // uninitialized
int scores[5] = {10, 20, 30, 40, 50}; // initialized
✅ Accessing Elements:
std::cout << scores[2]; // Output: 30
✅ Looping Through an Array:
for (int i = 0; i < 5; i++) {
std::cout << scores[i] << " ";
}
🔹 2. Multidimensional Arrays
✅ Example: 2D Array (Matrix)
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
✅ Accessing:
std::cout << matrix[1][2]; // Output: 6
🔹 3. Strings in C++
C++ offers two ways to handle strings:
✅ C-style strings (char arrays)
char name[] = "Alice";
std::cout << name;
✅ C++ string
class (recommended)
#include <string>
std::string name = "Alice";
std::cout << name;
✅ String Operations:
std::string s1 = "Hello";
std::string s2 = "World";
std::string result = s1 + " " + s2;
std::cout << result; // Output: Hello World
✅ Useful Functions:
s.length(); // returns length
s.substr(0, 3); // returns substring
s.find("lo"); // returns index of first match
s.append("!"); // appends to the end
🔹 4. Common Array/String Problems
-
Find the maximum or minimum in an array
-
Calculate sum/average
-
Reverse a string or array
-
Count vowels/consonants in a string
-
Sort an array (will cover later in depth)
✅ Summary for Day 4:
-
You learned how to declare, initialize, and use arrays.
-
You practiced with both 1D and 2D arrays.
-
You learned the difference between C-style strings and C++ strings, and basic operations.
Comments
Post a Comment