From f4586e83fc0de983fe9e3fb6e7221aa1ce2b8300 Mon Sep 17 00:00:00 2001 From: Gurdit Singh <118066728+askgurdit@users.noreply.github.com> Date: Sun, 14 Apr 2024 23:49:57 +0530 Subject: [PATCH] friend function --- C++ PROGAMMING/OOPS/friend_function.cpp | 41 ++++++++++++ C++ PROGAMMING/OOPS/run_time_polymorphism.cpp | 62 +++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 C++ PROGAMMING/OOPS/friend_function.cpp create mode 100644 C++ PROGAMMING/OOPS/run_time_polymorphism.cpp diff --git a/C++ PROGAMMING/OOPS/friend_function.cpp b/C++ PROGAMMING/OOPS/friend_function.cpp new file mode 100644 index 0000000..c2d49b7 --- /dev/null +++ b/C++ PROGAMMING/OOPS/friend_function.cpp @@ -0,0 +1,41 @@ +/*Non member function which can access private member of the class +classA{ +------ +------ +friend (); +}; +Here, friend A will be able to access private member of class A---------We use it when we don't want to maek parent & child class*/ + +#include +using namespace std; + +class A { + int x; +public: + A(int y) { + x = y; + } + friend void print(A &obj); +}; + +void print(A &obj) { + cout << obj.x << endl; +} + +int main() { + A obj(5); + print(obj); + return 0; +} + +/*This C++ code defines a class `A` with a private member `x` and a constructor that initializes `x` with a value passed as an argument. It also declares a friend function `print` that can access the private member `x` of class `A`. The `print` function simply outputs the value of `x` to the console. + +In the `main` function, an object `obj` of class `A` is created with an initial value of `5`. The `print` function is then called with `obj` as an argument, which prints the value of `x` (i.e., `5`) to the console. + +Here's the output of the program: + +``` +5 +``` + +This output indicates that the `print` function successfully accessed and printed the private member `x` of the `A` class object `obj`.*/ \ No newline at end of file diff --git a/C++ PROGAMMING/OOPS/run_time_polymorphism.cpp b/C++ PROGAMMING/OOPS/run_time_polymorphism.cpp new file mode 100644 index 0000000..ca4610d --- /dev/null +++ b/C++ PROGAMMING/OOPS/run_time_polymorphism.cpp @@ -0,0 +1,62 @@ +/*Resolved at runtime +using function overloading child class defines a function of parent class*/ +#include +using namespace std; + +class Parent { +public: + virtual void print() { + cout << "parent class" << endl; + } + void show() { + cout << "parent class" << endl; + } +}; + +class Child : public Parent { +public: + void print() { + cout << "child class" << endl; + } + void show() { + cout << "child class" << endl; + } +}; + +int main() { + Parent* p; + Child c; + p = &c; + p->print(); + p->show(); + + return 0; +} + +/*This code demonstrates function overloading in a child class that inherits from a parent class. Here's a breakdown of the code with its output: + +1. **Parent Class (`Parent`):** + - It has two functions, `print()` and `show()`. + - `print()` is a virtual function, indicating that it can be overridden by a function with the same signature in a derived class. + - `show()` is a normal function. + +2. **Child Class (`Child`):** + - It inherits from the `Parent` class. + - It overrides both `print()` and `show()` functions from the parent class. + +3. **Main Function:** + - It declares a pointer `p` of type `Parent`. + - It creates an object `c` of type `Child`. + - It assigns the address of `c` to the pointer `p`. + - It calls the `print()` and `show()` functions using the pointer `p`. + +**Output:** +- Since `print()` is a virtual function, the overridden version in the `Child` class is called, which prints "child class". +- `show()` is not a virtual function, so the version in the `Parent` class is called, which prints "parent class". + +Therefore, the output of the code will be: +``` +child class +parent class +```*/ +/* -------------Difference of Compile time and run time*/ \ No newline at end of file