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: ...
C ++ Day 9: Functions Deep Dive
🔹 1. Function overloadingDefinition: Function overloading allows many functions with the same name but different parameters.
CPP
Copy
edit
#INCLUDE <iostream>
using namespace std;
Zero print (int I) {
Cout << "Printing into:" << I << Endl;
,
Zero print (double d) {
Cout << "Printing double:" << d << endl;
,
Zero print (string s) {
cout << "printing string:" << s << endl;
,
int main() {
Print (5);
Print (3.14);
Print ("hello");
Return 0;
,
🔹 2. Default argument
Definition: You can provide a default value for function criteria.
CPP
Copy
edit
#INCLUDE <iostream>
using namespace std;
Zero greeting (string name = "guest") {
Cout << "Hello," << Name << "!" << Endl;
,
int main() {
Greetings ("Alice");
greet(); // Uses default
Return 0;
,
🔹 3. Recurrence
Definition: A function that calls itself.
Example: Factorial
CPP
Copy
edit
#INCLUDE <iostream>
using namespace std;
Int Factorial (Int N) {
If (n <= 1) returns 1;
Return N * Facterial (N - 1);
,
int main() {
Cout << is the factial of "5:" << Factorial (5) << Endl;
Return 0;
,
🔹 4. Inline functions
Definition: The compiler suggests the body of the function directly inserting to reduce overhead.
CPP
Copy
edit
#INCLUDE <iostream>
using namespace std;
Inline Int Square (Int X) {
X * x return;
,
int main() {
Cout << is the class of 4: "<< class (4) << endl;
Return 0;
,
🔹 5. Static variable in functions
Definition: A stable variable inside a function retains its value between the call.
CPP
Copy
edit
#INCLUDE <iostream>
using namespace std;
Zero counter () {
Static int count = 0;
Counting ++;
Cout << "count:" << count << Endl;
,
int main() {
counter();
counter();
counter();
Return 0;
,
🧠 Practice
Comments
Post a Comment