Blog

C++ program to illustrate virtual base classes

#include<iostream> using namespace std; class Base1 { public : void showbase1( ) { cout<<endl<<“base1”; } }; class Base2 : virtual public Base1 { public: void showbase2( ) { cout<<endl<<“base2”; } }; class Base3 : virtual public Base1 { public: void showbase3( ) { cout<<endl<<“base3”; } }; class Derived : public Base2, public Base3 { public: […]

C++ program to illustrate Inheritance

#include<iostream> using namespace std; class Base { int a,b; public : void getbase() { cin >> a >> b; } void showbase() { cout << a << b; } }; class Derived : public Base { int c; public: void getderived() { cin >> c; } void showderived() { cout << c; } }; int […]

C++ program to illustrate Type Conversions using operator function

The following C++ program illustrates Type Conversions from one class to another class type using operator function. #include<iostream> using namespace std; class N { int a; public: void set(int p) { a=p; } void display( ) { cout<<a; } }; class M { int x; public : M(int a) { x=a; } operator N( ) […]

C++ Program to illustrate Type Conversions (From one class to another)

The following is a C++ program to illustrate Type Conversions from one class to another class type using the constructor function. #include<iostream> using namespace std; class M { int x; public : M(int a) { x=a; } int getx() { return x; } }; class N { int k; public : N() { } N(M […]

C++ program to illustrate Type Conversions (from class to basic type)

#include<iostream> using namespace std; class M { float x; public : M(float a) { x=a; } operator float() { return x; } }; int main() { M m(3.4); float f; f=float(m); // from class to basic type cout<<f; return 0; } Output 3.4

C++ program to illustrate Type Conversions (From basic to class type)

#include<iostream> using namespace std; class MyClass { int x; public : MyClass( ) { x=0; } MyClass(int a) { x=a; } void display( ) { cout<<x; } }; int main( ) { MyClass obj; obj=MyClass(23); // from basic to class type obj.display( ); return 0; } Output 23

C++ program to illustrate Operator overloading only with friend functions

#include<iostream> using namespace std; class B { int a; public : B( ) { a=0; } B(int x) { a=x; } void show( ) { cout<<endl<<a; } friend B operator +(int,B); }; B operator +(int x,B obj) { B t; t.a=x+obj.a; return t; } int main( ) { B obj1(10); B obj2; obj2 = 2 […]

C++ program to illustrate Overloading Binary Operator (Friend Functions)

#include<iostream> using namespace std; class Complex { float x,y; public: Complex() { x=0; y=0; } Complex(float real,float imag) { x=real; y=imag; } void show() { cout<<endl<<x<<“+j”<<y; } friend Complex operator +(Complex,Complex); }; Complex operator +(Complex obj1,Complex obj2) { Complex temp; temp.x=obj1.x+obj2.x; temp.y=obj1.y+obj2.y; return temp; } int main() { Complex c1(1.6,3.6); Complex c2(2.6,4.9); Complex c3; c3 […]

C++ program to illustrate Overloading Binary Operator (Member Functions)

#include<iostream> using namespace std; class Complex { float x,y; public: Complex() { x=0; y=0; } Complex(float real,float imag) { x=real; y=imag; } void show() { cout<<endl<<x<<“+j”<<y; } Complex operator +(Complex); }; Complex Complex :: operator +(Complex obj) { Complex temp; temp.x=x+obj.x; temp.y=y+obj.y; return temp; } int main() { Complex c1(1.6,3.6); Complex c2(2.6,4.9); Complex c3; c3 […]

C++ Program to illustrate Overloading Unary minus (friend functions)

#include<iostream> using namespace std; class M { int a,b; public: M(int x,int y) { a=x; b=y; } void show() { cout<<a<<b; } friend void operator -(M &); }; void operator -(M & obj) { obj.a = -obj.a; obj.b = -obj.b; } int main() { M m(7,-2); -m; m.show(); return 0; } Output -7   2