PRACTICAL – 9.1
Construct a two-dimensional array using dynamic constructors.
#include <iostream>using namespace std;class matrix{int d1, d2;int **p;public:matrix(int x, int y);void getdata(int i, int j, int value){p[i][j] = value;}void putdata(int i, int j){cout << p[i][j];cout << " ";}};matrix ::matrix(int x, int y){d1 = x;d2 = y;p = new int *[d1]; // creates array pointerfor (int i = 0; i < d1; i++){p[i] = new int[d2];}}int main(){int m, n, value, i, j;cout << "Enter the size of matrix" << endl;cin >> m >> n;matrix a(m, n);cout << "Enter the elements row by row" << endl;for (i = 0; i < m; i++){for (j = 0; j < n; j++){cin >> value;a.getdata(i, j, value);}}cout << "\n****************************" << endl;for (i = 0; i < m; i++){for (j = 0; j < n; j++){a.putdata(i, j);}cout << endl;}}
PRACTICAL – 9.2
Write a program to overload the + and –operators for the complex class.
#include <iostream>using namespace std;class Operator_overloading{int num;public:Operator_overloading operator+(Operator_overloading &a){Operator_overloading x;x.num = num + a.num;return x;}Operator_overloading operator-(Operator_overloading &a){Operator_overloading x;x.num = num - a.num;return x;}void setdata(){cin >> num;}inline void getdata() { cout << num << endl; }};int main(){Operator_overloading o, p, q, r;o.setdata();p.setdata();q = o + p;r = o - p;q.getdata();r.getdata();return 0;}
PRACTICAL – 9.3
Write a program to overload the unary –operator for a suitable class.
#include <iostream>using namespace std;class Operator_overloading{int num;public:void operator-(){num = (-1) * num;}void setdata(){cin >> num;}inline void getdata() { cout << num; }};int main(){Operator_overloading o, p;o.setdata();-o;o.getdata();return 0;}
0 Comments