Inheritance in Opp C++
Inheritance in Opp C++
#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;
}
}
}
#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;
}
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;
}