Skip to content

Commit

Permalink
friend function
Browse files Browse the repository at this point in the history
  • Loading branch information
askgurdit committed Apr 14, 2024
1 parent 80b4dd6 commit f4586e8
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
41 changes: 41 additions & 0 deletions C++ PROGAMMING/OOPS/friend_function.cpp
Original file line number Diff line number Diff line change
@@ -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 <iostream>
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`.*/
62 changes: 62 additions & 0 deletions C++ PROGAMMING/OOPS/run_time_polymorphism.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*Resolved at runtime
using function overloading child class defines a function of parent class*/
#include <iostream>
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*/

0 comments on commit f4586e8

Please sign in to comment.