0% found this document useful (0 votes)
21 views7 pages

Inheritance in Opp C++

The document contains 5 tasks that demonstrate the use of C++ templates for generic programming. Task 1 defines a class to represent counting objects that can be incremented. Task 2 defines a matrix class with operator overloading for matrix multiplication. Task 3 defines a generic swap function and demonstrates swapping integers, floats and characters. Task 4 defines a generic function to return the maximum of 4 values. Task 5 defines a generic number class to store and retrieve values of different types.

Uploaded by

f228802
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views7 pages

Inheritance in Opp C++

The document contains 5 tasks that demonstrate the use of C++ templates for generic programming. Task 1 defines a class to represent counting objects that can be incremented. Task 2 defines a matrix class with operator overloading for matrix multiplication. Task 3 defines a generic swap function and demonstrates swapping integers, floats and characters. Task 4 defines a generic function to return the maximum of 4 values. Task 5 defines a generic number class to store and retrieve values of different types.

Uploaded by

f228802
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

TASK 1

#include<iostream>
using namespace std;
class COUNTING {
int count;
public:
COUNTING()
{
count = 1;
}
COUNTING(int n)
{
count = n;
}
COUNTING operator -()
{
COUNTING result;
result.count = count * 3;
return result;
}
void display()
{
cout << count << endl;
}
};
int main()
{
int n;
cout << "enter a number" << endl;
cin >> n;
COUNTING c(n);
COUNTING m = -c;
m.display();
system("pause");
}

TASK 2
#include<iostream>
#include<string>
using namespace std;
const int max_size = 3;
class Matrix
{
int rows;
int col;
int matrix[max_size][max_size];
public:
Matrix()
{
rows = col = 0;
for (int i = 0; i <rows; i++)
{
for (int j = 0; j <col; j++)
{
matrix[i][j] = 0;
}
}
}
Matrix(int rows,int col)
{
rows = col = 0;
for (int i = 0; i <rows; i++)
{
for (int j = 0; j <col; j++)
{
matrix[i][j] = 0;
}
}
}

void readmatrix(int rows,int col)


{
cout << "enter elements for matrix " << endl;
for (int i = 0; i <rows; i++)
{
for (int j = 0; j < col; j++)
{
cin >> matrix[i][j];
}
}
}
void display()
{
for (int i = 0; i <rows; i++)
{
for (int j = 0; j <col; j++)
{
cout << matrix[i][j];
}
cout << endl;
}
}
Matrix operator*(Matrix b)
{
Matrix result(rows,b.col);
if (col != b.rows)
{
cout << "multiplication is not possible" << endl;
}
else
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < b.rows; j++)
{
for (int k = 0; k < col; k++)
{
result.matrix[i][j] += matrix[i][k] * matrix[j]
[k];
}
}
}
return result;
}
}
};
int main()
{
int rows;
int col;
Matrix m;
cout << "enter rows and columns for matrix " << endl;
cin >> rows >> col;
m.readmatrix(rows,col);
Matrix b;
cout << "enter rows and columns for matrix 2" << endl;
cin >> rows >> col;
b.readmatrix(rows, col);
cout << "______________________original matrix 1_____________________________" <<
endl;
m.display();
cout << "______________________original matrix 2_____________________________" <<
endl;
b.display();
Matrix mt = m*b;
cout << "_______________________matrix after
multiplication_______________________" << endl;
mt.display();
system("pause");
}
TASK 3

#include<iostream>
#include<string>
template <typename T>
void swapv(T&a, T&b)
{
T temp = a;
a = b;
b = temp;
}
int main()
{
int a = 5, b = 10;
std::cout << "before swapping" << std::endl;
std::cout << "a=" << a << " " << "b=" << b << " " << std::endl;
std::cout << "after swapping" << std::endl;
swapv(a, b);
std::cout << "a=" << a << " " << "b=" << b << " " << std::endl;
float c = 3.5, d = 4.5;
std::cout << "before swapping" << std::endl;
std::cout << "c=" << c << " " << "d=" << d << " " << std::endl;
std::cout << "after swapping" << std::endl;
swapv(c, d);
std::cout << "c=" << c << " " << "d=" << d << " " << std::endl;
char e = 'h', f = 'l';
std::cout << "before swapping" << std::endl;
std::cout << "e=" << e << " " << "f=" << f << " " << std::endl;
std::cout << "after swapping" << std::endl;
swapv(e, f);
std::cout << "e=" << e << " " << "f=" << f << " " << std::endl;
system("pause");
}
TASK 4

#include<iostream>
#include<string>
template <typename T>
T greater(T a, T b, T c, T d)
{
T max = a;
if (b > max)
{
max = b;
}
if (c > max)
{
max = c;
}
if (d > max)
{
max = d;
}
return max;
}
int main()
{
int a = 2, b = 3, c = 5, d = 7;
std::cout << greater(a, b, c, d) << std::endl;
float e = 2.5, f = 4.5, g = 6.5, h = 7.5;
std::cout << greater(e, f, g, h);
system ("pause");
}

TASK 5
#include<iostream>
#include<string>
using namespace std;
template <class t>
class number {
private:
t num;
public:
number(t n):num(n){}
t getnum()
{
return num;
}
};
int main()
{
number<int> NumberInt(7);
number<double> NumberDouble(7.7);
cout << "int number= " << NumberInt.getnum() << endl;
cout << "double number = " << NumberDouble.getnum() << endl;
system("pause");
}

#include <iostream>
using namespace std;

template <class T>


class Number
{
private:
T num, num1, num2;
public:
Number(T n, T n1) :num(n), num1(n1)
{

}
T getnum()
{
return num;
return num1;
}
T divide()
{
num2 = num / num1;
if (num1 == 0)
{
cout << "denominator is zero!!" << endl;
}
else
{
return num2;
}
}

};
int main()
{
Number <int>numberInt(8, 2);
numberInt.divide();
cout << " number is:" << numberInt.divide();
return 0;
}

You might also like