Skip to main content

Posts

Dynamic Default constructor & Destructor

#include <iostream> #include <string.h> using namespace std; class A{ int *id; string   name; string address; float *salary; public: A (int x =0, string   n="unknown" ,string ad ="city=unknown country =unknown ",float s=0 )     { id = new int; *id =x; name = n; address=ad ; salary =new float; *salary =s;   } ~A()       { cout<<"\t\t\tThe destroctor of: " <<endl; cout<<"Id is :   "<<*id   <<endl; cout<<"Name :   "<<name <<endl; cout<<"Address   :   "<<address <<endl; cout<<"Salary :   "<<*salary <<endl;         } void display ()      {        cout<<"The id is :   "<<*id   <<endl; cout<<"The Name is :   "<<name <<endl; cout<...
Recent posts

<< Create and Implement program in C++ to define 2 classes. Addition, subtraction, division, average, power and multiplication of 2 variables must be performed using functions. >> "With Separate Function of {Addition, subtraction, division, average, Multiplication,Power }

# include  <iostream> using namespace std; class Two; class One{int a,b; public: friend class Two; void multi (){ cout<< "The Multiply of two no is :" <<a*b<<endl;} void sub(){ cout<< "The subtraction of two no is :" <<a-b<<endl;} void sum(){ cout<< "The sum of two no is :" <<a+b<<endl;} void div(){ cout<< "The division of two no is :" <<a/b<<endl;}void avg(){ cout<< "The Average of two no is :" <<(a+b)/2<<endl;}}; class Two{ int n; int power; float base_no; int res = 1; public: friend class One; void power1(){ cout << " \t<< Enter Number >>: "<<endl; cin >> base_no ; cout << " \t<< Enter power >>: "<<endl; cin >> power; cout << base_no << "^" << power << " : "; while (power != 0) { res *= base_no; --power; } cou...

*Create and Implement program in C++ to define 2 classes. Addition, subtraction, division, average, power and multiplication of 2 variables must be performed using functions.*

#include <iostream> using namespace std; class B; class A{int a,b; public: friend   class B;}; class B{int x,power; float base_no; public: friend   class A; void calculator (A o1) { int result = 1; cout << "\nEnter the two number :" <<endl; cin>>o1.a>>o1.b; cout << "Enter Choise \nfor Multiply   (*) press 1:\t\nFor subtraction (-) press 2:\t\n For Addition(+) press 3:\t\n For Division (/)press 4:\nFor Average (avg)press 5:\n" <<endl; cin>>x; if (x==1){ cout<< "The Multiply of two no is :" <<o1.a*o1.b<<endl; } else if (x==2) {cout<< "The subtraction of two no is :" <<o1.a-o1.b<<endl;    } else if (x==3) {   cout<< "The sum of two no is :" <<o1.a+o1.b<<endl;   } else if (x==4)   {    cout<< "The division of two no is :" <<o1.a/o1.b<<endl;   } else if (x==5)   {  ...

Concept of Friend Class in OPP with C++

#include <iostream> using namespace std; class B; class A {int a; public:    void set_val (int x ){ a=x;  }      friend class B;}; class B{ int b; public: void set_val (int y ){ b=y;} void  show (A) {  cout << "I am In Class B"  << endl;}}; int main() {A obj;     B o;  o.show(obj);  return 0; } Output :

Swaping Value of Variable 'A 'and 'B' (Class A access Class B member function)

#include <iostream> using namespace std; class B; class A {int a; public: void set_val (int x ){ a=x;}  void swap1(A ,B );}; class B{ int b; public: friend void A::swap1 (A  ,B ); void set_val (int y ){ b=y;}}; void  A:: swap1 (A obj1, B obj2) {int temp; temp =obj1.a  ; obj1 . a=obj2.b; obj2.b = temp; cout << "The value of a :"<<  obj1.a  << endl; cout <<  "The value of b :"<< obj2.b  << endl;} int main() {  A obj; B o; obj.set_val (5); o.set_val(2); obj.swap1(obj,o); return 0; } Output:

Concept Of Friend Function Through Reference Class Object

#include <iostream> using namespace std; class B; class A {int a; public:    void set_val (int x ){ a=x;  }     friend void swap1(A &obj1 ,B &obj2);}; class B{ int b; public:    friend void swap1 (A &obj1 ,B &obj2); void set_val (int y ){ b=y;}}; void  swap1 (A &obj1, B &obj2) {int temp; temp =obj1.a  ; obj1 . a=obj2.b; obj2.b = temp;        cout << "The value of a :"<<  obj1.a  << endl;        cout <<  "The value of b :"<< obj2.b  << endl;} int main(){   A obj;     B o;     obj.set_val (5); o.set_val(2); swap1 (obj,o);     return 0;} Output:

Swaping value of variable A and B Through Friend Function in Classes

#include <iostream> using namespace std; class B; class A {int a; public: void set_val (int x ){ a=x;} friend void swap1(A,B);}; class B{ int b; public: friend void swap1 (A,B); void set_val (int y ){ b=y;}}; void   swap1 (A obj1, B obj2){ int temp; temp =obj1.a   ; obj1.a=obj2.b; obj2.b = temp; cout << "The value of a :"<<   obj1.a   << endl; cout <<   "The value of b :"<< obj2.b   << endl;} int main(){ A obj; B o; obj.set_val (5); o.set_val(2); swap1 (obj,o); return 0;}  Output: