Sunday, February 25, 2018

C ++ Program : Type casting operator

#include <iostream>
using namespace std;
class A {
    int i;
public:
    A(int ai) : i(ai) {}
    int get() const { return i; }
    void update() { ++i; }
};
class B {
    int i;
public:
    B(int ai) : i(ai) {}
    int get() const { return i; }
B( const A x){ i= x.get();}
operator A(){get();}
 
 
 
 

           
  
    void update() { ++i; }
};
int main() {
    int i;
    cin >> i;
    A a(i++);
    B b(i);
    const B &r = static_cast<B>(a);
    a.update();
    cout << a.get() << ":";
    cout << r.get() << ":";
    const A &s = static_cast<A>(b);
    b.update();
    cout << b.get() << ":";
    cout << s.get() << ":";
    return 0;
}

C ++ program : Type Casting example

#include <iostream>
using namespace std;
class A {
protected:
    int i;
public:
    A(int a = 0) : i(a) {}
void fun() { cout << i; }
};
class B :public A{
public:
    B(int a = 0) : A(a) {}
 virtual void fun() { cout << i*10; }
};
class C :public B{
public:
    C(int a = 0) : B(a) {}
 virtual void fun() { cout << i*15; }
};
int main(){
    int num;
    cin >> num;
    B *bp = new C(num);
    bp->fun();
    return 0;
}

Thanks
Happy Programming !

C++ : Polymorphism and mutiple inheritence

#include <iostream>
using namespace std;
class Shape {
protected:
    int width, height;
public:
virtual int getArea(){}
    void setWidth(int w) { width = w; }
    void setHeight(int h) { height = h; }
};

class Rectangle : public Shape {
public:
    int getArea() { return (width * height); }
};

class Triangle : public Shape {
public:
    int getArea() { return (width * height) / 2; }
};

int main(void) {
    int x, y;
    cin >> y >> x;
    Rectangle Rect;
    Triangle Tri;
    Rect.setWidth(x);
    Rect.setHeight(y);
    Tri.setWidth(x);
    Tri.setHeight(y);
    Shape *shape[] = { &Rect, &Tri, 0 };
    Shape **pShape = &shape[0];
    while (*pShape)
    cout << (*pShape++)->getArea() << " ";
    return 0;
}

C++ : Multiple Inheritence

#include <iostream>
#include <string>
using namespace std;
class Employee {
public:
    string Name;
    double salary;
    Employee(string fName, double sal) : Name(fName), salary(sal) {}
    void show() {
        cout << Name << " " << salary;
    }
    void addBonus(double bonus) {
        salary += bonus;
    }
};

class Manager :public Employee {
public:
    Manager(string fName, double sal) : Employee(fName, sal) {}
};

class Clerk :public Employee {
public:
    Clerk(string fName, double sal) : Employee(fName, sal) {}
};
void congratulate(Employee* emp) {
    emp->addBonus(200);
    emp->show();
    cout << " ";
};

int main() {
    Employee* emp;
    int sal_m, sal_c;
    cin >> sal_c >> sal_m;
    Manager m1("Steve", sal_m);
    Clerk c1("Kevin", sal_c);
emp = &c1;
congratulate (emp);
emp = &m1;
congratulate (emp);
    return 0;
}

C++ : Dynamic Binding

#include <iostream>
using namespace std;
class A {
    int n;
protected:
   A(int i) : n(i) { }
   virtual int get() = 0;
   virtual void print() = 0;
};

int A::get() {
    return n;
}

class B : private A {
protected:
    B(int i) : A(i) {}
int get(){ return A::get();}
};  // End of class B

class C : public B {
public:
    C(int i) : B(i) {}
    void print() {
        cout << get() ;
    }
};

int main(){
    int n;
    cin >> n;
    C *p = new C(n);
    p->print();
    return 0;
}

Thanks
Happy Programming !

C++ : Private Properties

#include <iostream>
using namespace std;
class M {
protected:
    int m;
public:
   void set_m(int x) { m = x; };
};
class N {
protected:
    int n;
public:
    void set_n(int y) { n = y; };
};
class P : public M, public N {
public:
    void display() { cout << m * n << endl; };
};

int main(){
    int a, b;
    cin >> a >> b;
    P p;



p.M::set_m(a);
p.N::set_n(b);
p.display ();
    return 0;
}  // End of main()

Thanks
Happy Computing !

C++ : Polymorphism - Virtual Destructors illustration through program

#include <iostream>
using namespace std;
class B {
public:
    B() { cout << "98 "; }
virtual ~B() { cout << "56 "; } // Don't Edit or Modify the "cout"

}; // End of class B

class D : public B {
    int n;
public:
    D(int p):n(p) { cout << n << " "; }
    ~D() { cout << n*2 << " "; }
}; // End of class D

int main() {
    int n ; cin >> n ;
    B *basePtr = new D(n);
    delete basePtr;
    return 0;
}

what is crystal field theory and why do we need it?

  Calculating CFSE