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 13: Heritage Basics
🔹 1. What is inheritance? The inheritance allows a square (derivative/child) to obtain qualities and methods from another class (base/parent). This helps reusion the code and establish relationships like "IS-A". 🔹 2. Syntax CPP Copy edit Class base { public: Zero Show () { cout << "base class method" << Endl; , , Class derivative: Public base { public: Zero display () { cout << "derived class method" << endl; , , int main() { Derved OBJ; Obj.show (); // Aadhaar Obj.display (); // derived from Return 0; , 🔹 3. Inheritance Access Specifier Aadhaar Member Public Heritage Protected Heritage Private Heritage Publicly protected private Protected protected private Private heritage has not been inherited by not inherited Example: CPP Copy edit A class { public: Int x; reserve: Int y; Personal: Int z; , Class B: Public A { public: Zero Show () { cout << x; // Ok cout << y; // Ok // Cout << Z; // Mistake , , 🔹 4. Inheritance constctor and distribution order The base class construction is called before the derivative class. The destructive is called in the reverse order. CPP Copy edit A class { public: A () {cout << "base constitution \ n"; , ~ A () {cout << "base disastrous \ n"; , , Class B: Public A { public: B () {cout << "derivative constructor \ n"; , ~ B () {cout << "derivative disastrous \ n"; , , 🔹 5. Single versus multi -layered heritage ✔ single heritage CPP Copy edit A class {}; Class B: Public A {}; ✔ multi -level heritage CPP Copy edit A class {}; Class B: Public A {}; Class C: Public B {}; // C inherited A and B 🔹 6. Function overrising The derivative class can override the methods of the base class with the same signature. CPP Copy
Comments
Post a Comment