Essential C++ Programming Examples and Code Snippets

Posted by Anonymous and classified in Computers

Written on in with a size of 3.19 KB

Palindrome and Digit Sum Calculation

#include <iostream>
using namespace std;
int main() {
    int n, r, rev = 0, sum = 0, t;
    cin >> n; t = n;
    while (n) {
        r = n % 10;
        rev = rev * 10 + r;
        sum += r;
        n /= 10;
    }
    cout << (t == rev ? "Palindrome" : "Not");
    cout << "\nSum=" << sum;
}

Matrix Addition in C++

#include <iostream>
using namespace std;
int main() {
    int a[10][10], b[10][10], c[10][10], r, col;
    cin >> r >> col;
    for (int i = 0; i < r; i++)
        for (int j = 0; j < col; j++) cin >> a[i][j];
    for (int i = 0; i < r; i++)
        for (int j = 0; j < col; j++) cin >> b[i][j];
    for (int i = 0; i < r; i++)
        for (int j = 0; j < col; j++)
            c[i][j] = a[i][j] + b[i][j];
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < col; j++)
            cout << c[i][j] << " ";
        cout << endl;
    }
}

Simple Interest Using Classes

#include <iostream>
using namespace std;
class SI {
    float p, r, t;
public:
    SI() {}
    SI(float a, float b, float c) { p = a; r = b; t = c; }
    void show() { cout << "SI=" << (p * r * t) / 100; }
};
int main() {
    SI s(1000, 5, 2);
    s.show();
}

Private Inheritance Example

#include <iostream>
using namespace std;
class A {
    int x;
public:
    void get() { cin >> x; }
    void show() { cout << x; }
};
class B : private A {
public:
    void display() { get(); show(); }
};
int main() {
    B b;
    b.display();
}

Operator Overloading for Complex Numbers

#include <iostream>
using namespace std;
class C {
    int r, i;
public:
    C(int a = 0, int b = 0) { r = a; i = b; }
    C operator+(C x) { return C(r + x.r, i + x.i); }
    void show() { cout << r << "+" << i << "i"; }
};
int main() {
    C a(2, 3), b(1, 2), c;
    c = a + b;
    c.show();
}

Protected Inheritance Access

#include <iostream>
using namespace std;
class A {
protected:
    int x = 5;
};
class B : protected A {
public:
    void show() { cout << x; }
};
int main() {
    B b;
    b.show();
}

Clock Class Implementation

#include <iostream>
using namespace std;
class Clock {
    int h, m;
public:
    void set(int a, int b) { h = a; m = b; }
    void show() { cout << h << ":" << m; }
    void toMin() { cout << "\nMin=" << h * 60 + m; }
};
int main() {
    Clock c;
    c.set(2, 30);
    c.show();
    c.toMin();
}

Function Overloading for Absolute Values

#include <iostream>
using namespace std;
class AbsVal {
public:
    int val(int x) { return x < 0 ? -x : x; }
    float val(float x) { return x < 0 ? -x : x; }
};
int main() {
    AbsVal a;
    cout << a.val(-5);
}

Friend Function Usage

#include <iostream>
using namespace std;
class B;
class A {
    int x = 5;
    friend void show(A, B);
};
class B {
    int y = 10;
    friend void show(A, B);
};
void show(A a, B b) { cout << a.x + b.y; }
int main() {
    A a; B b;
    show(a, b);
}

Related entries: