Skip to content

Instantly share code, notes, and snippets.

@nahiyan
Created November 19, 2020 05:25
Show Gist options
  • Save nahiyan/de8fd8df73b0b56eaaaa0c7029097a80 to your computer and use it in GitHub Desktop.
Save nahiyan/de8fd8df73b0b56eaaaa0c7029097a80 to your computer and use it in GitHub Desktop.

Revisions

  1. nahiyan created this gist Nov 19, 2020.
    25 changes: 25 additions & 0 deletions 1.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    #include <iostream>
    using namespace std;
    class MyInfo
    {
    private: // data hidden from outside world
    int x;

    public:
    // function to set value of variable x
    void set(int a) { x = a; }
    // function to return value of variable x
    int get()
    {
    return x;
    }
    };
    // main function
    int main()
    {
    MyInfo obj;
    obj.set(5);
    cout << obj.get();
    // cout is used inspite of printf()
    return 0;
    }
    23 changes: 23 additions & 0 deletions 2.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    #include <iostream>
    using namespace std;
    class MyInfo
    {
    private:
    // data hidden from outside world
    int x;

    public:
    // function to set value of variable x
    void set(int a);
    // function to return value of variable x
    int get();
    };
    void MyInfo::set(int a) { x = a; }
    int MyInfo::get() { return x; } // main function
    int main()
    {
    MyInfo obj;
    obj.set(5);
    cout << obj.get(); // cout is used inspire of printf()
    return 0;
    }
    36 changes: 36 additions & 0 deletions 3.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    #include <iostream>
    using namespace std;
    class Calculator
    {
    private:
    int num1;
    int num2;

    public:
    Calculator(int n1, int n2)
    { // Constructor
    num1 = n1;
    num2 = n2;
    }
    int add() { return num1 + num2; }
    int multiply()
    {
    return num1 * num2;
    }
    void display()
    {
    cout << "Numbers : " << num1 << " and " << num2;
    cout << "\n\t Add : " << add();
    cout << "\n\t Multiply : " << multiply();
    cout << endl
    << endl;
    }
    };
    int main()
    {
    Calculator a(2, 4);
    Calculator b(5, 3);
    a.display();
    b.display();
    return 0;
    }
    40 changes: 40 additions & 0 deletions 4.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    #include <iostream>
    using namespace std;
    template <class T>
    class Calculator
    {
    private:
    T num1;
    T num2;

    public:
    Calculator(T n1, T n2)
    {
    num1 = n1;
    num2 = n2;
    }
    T add()
    {
    return num1 + num2;
    }
    T multiply()
    {
    return num1 * num2;
    }
    void display()
    {
    cout << "Numbers : " << num1 << " and " << num2;
    cout << "\n\t Add : " << add();
    cout << "\n\t Multiply : " << multiply();
    cout << endl
    << endl;
    }
    };
    int main()
    {
    Calculator<int> a(2, 4);
    Calculator<float> b(2.2, 4.1);
    a.display();
    b.display();
    return 0;
    }
    48 changes: 48 additions & 0 deletions 5.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    #include <iostream>
    using namespace std;
    template <class T>
    class Calculator
    {
    private:
    T num1;
    T num2;

    public:
    Calculator(T n1, T n2);
    T add();
    T multiply();
    void display();
    };
    template <class T>
    Calculator<T>::Calculator(T n1, T n2)
    {
    num1 = n1;
    num2 = n2;
    }
    template <class T>
    T Calculator<T>::add()
    {
    return num1 + num2;
    }
    template <class T>
    T Calculator<T>::multiply()
    {
    return num1 * num2;
    }
    template <class T>
    void Calculator<T>::display()
    {
    cout << "Numbers : " << num1 << " and " << num2;
    cout << "\n\t Add : " << add();
    cout << "\n\t Multiply : " << multiply();
    cout << endl
    << endl;
    }
    int main()
    {
    Calculator<int> a(2, 4);
    Calculator<float> b(2.2, 4.1);
    a.display();
    b.display();
    return 0;
    }
    37 changes: 37 additions & 0 deletions 6.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    #include <iostream>
    using namespace std;
    class Box
    {
    private:
    double length;
    // Length of a box
    double breadth;
    // Breadth of a box
    double height;
    // Height of a box
    public:
    // Constructor definition
    Box(double l = 2.0, double b = 2.0, double h = 2.0)
    {
    cout << " Constructor called." << endl;
    length = l;
    breadth = b;
    height = h;
    }
    double Volume() { return length * breadth * height; }
    int compare(Box box) { return this->Volume() > box.Volume(); }
    };
    int main(void)
    {
    Box Box1(3.3, 1.2, 1.5); // Declare box
    Box Box2(8.5, 6.0, 2.0); // Declare box
    if (Box1.compare(Box2))
    {
    cout << "Box2 is smaller than Box1" << endl;
    }
    else
    {
    cout << "Box2 is equal to or larger than Box1" << endl;
    }
    return 0;
    }
    31 changes: 31 additions & 0 deletions 7.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    #include <iostream>
    using namespace std;
    class Complex
    {
    private:
    int real, imag;

    public:
    Complex(int r = 0, int i = 0)
    {
    real = r;
    imag = i;
    }
    // This i s automatically called when '+'i s used with
    // between two Complex objects
    Complex operator+(Complex &obj)
    {
    Complex res;
    res.real = real + obj.real;
    res.imag = imag + obj.imag;
    return res;
    }
    void print() { cout << real << " + i" << imag << endl; }
    };
    int main()
    {
    Complex c1(10, 5);
    Complex c2(2, 4);
    Complex c3 = c1 + c2; // An example call to "operator+"
    c3.print();
    }