0% found this document useful (0 votes)
69 views5 pages

Lab 13 - 1781

The document contains code for 6 tasks that demonstrate C++ templates and inheritance. It defines classes like Animal and Dog with inheritance, and templates like range, Sequence, and tornadoException. Main functions instantiate objects of these classes and call their methods to output text, demonstrating polymorphism through virtual functions and templates working on different data types.

Uploaded by

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

Lab 13 - 1781

The document contains code for 6 tasks that demonstrate C++ templates and inheritance. It defines classes like Animal and Dog with inheritance, and templates like range, Sequence, and tornadoException. Main functions instantiate objects of these classes and call their methods to output text, demonstrating polymorphism through virtual functions and templates working on different data types.

Uploaded by

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

===================================TASK

1=================================================
#include <iostream>
using namespace std;

template <typename T> class range {


private:
T** p;
T ran;
int row, col;
public:
range() {
row = 0;
col = 0;
}
range(T** arr, int row, int col);
void shift(T** arr);
void print();
};

template <typename T> range<T>::range(T** a, int r, int c)


{
row = r;
col = c;
int min, max;
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
if (a[i][j] >= a[0][0])
{
max = a[i][j];
}
else if (a[i][j] <= a[0][0])
{
min = a[i][j];
}
}
cout << endl;
}
ran = ((max - min) / 4) + min;
cout << "RAMGE : \t" << ran << endl;
}

template <typename T> void range<T>::shift(T** a) {


p = new T * [row];
for (int i = 0; i < col; i++) {
p[i] = new T[col];
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
p[i][j] = a[i][j];
}
}
}

template <typename T> void range<T>::print() {


cout << "With SHIFTING : " << endl;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
cout << p[i][j] << " ";
}
cout << endl;
}
}

int main()
{
int** a;
a = new int* [4];
for (int i = 0; i < 4; i++) {
a[i] = new int[4];
}
int count = 0;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
a[i][j] = count;
count++;
}
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
cout << a[i][j] << " ";
}
cout << endl;
}

range<int> First(a, 4, 4);


char** ch;
ch = new char* [3];
for (int i = 0; i < 3; i++) {
ch[i] = new char[3];
}
count = 65;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
ch[i][j] = (char)count;
count++;
cout << (char)ch[i][j] << " ";
}
cout << endl;
}
range<char> Second(ch, 3, 3);
First.shift(a);
First.print();
Second.shift(ch);
Second.print();
return 0;
}

===============================TASK
2==========================================================
#include <iostream>
using namespace std;

template <typename T>


T* maximum(T* x, T* y) {
if (strcmp(x, y) == 1)
return x;
else
return y;
}

int main()
{
char* a;
a = new char[10];
char* b;
b = new char[10];
cout << "Enter FIRST Array: \t";
cin.getline(a, 10);
cout << "Enter SECOND Array:\t";
cin.getline(b, 10);
cout << "After Changing to upper case :\t";
string str;
str = maximum(a, b);
for (int i = 0; i < str.length(); i++) {
cout << (char)(str[i] - 32);
}
return 0;
}

==============================TASK
3=========================================================
#include <iostream>
using namespace std;

template <class T, int N> class Sequence {


T memblock[N];
public:
void setmember(int x, T value) {
memblock[x] = value;
}

T getmember(int x) {
return memblock[x];
}
};
int main()
{
Sequence <int, 5> myints;
Sequence <double, 5> myfloats;
myints.setmember(0, 100);
myfloats.setmember(3, 3.1416);
cout << myints.getmember(0) << '\n';
cout << myfloats.getmember(3) << '\n';
return 0;
}
=====================TASK
4============================================================

#include <iostream>
using namespace std;
template <typename T>
class tornadoException {
public:
tornadoException() {
cout << "TORNADO = Take cover immediately!" << endl;
}
tornadoException(T m);
};

template <typename T> tornadoException<T>::tornadoException(T m) {


cout << "Tornado is " << m << " Away" << endl;
}

int main() {
string str = "";
tornadoException <string>();
tornadoException<int>(5);
}

===================================TASK 5=================================
#include <iostream>
using namespace std;

class Animal
{
public:
virtual void speak() {
cout << "SPAEK Constructor called" << endl;
}
virtual ~Animal() {
cout << "ANIMAL Destructor called" << endl;
}
};

class Dog :public Animal


{
public:
void speak() {
cout << "Bow Wooh!" << endl;
}
~Dog() {
cout << "Dog Destructor called." << endl;
}
};

int main()
{
Animal objAnimal;
Dog objDog;
Animal* ptrAnimal = &objAnimal;
Dog* ptrDog = &objDog;
objAnimal.speak();
objDog.speak();
ptrAnimal->speak();
ptrDog->speak();
ptrDog->Animal::speak();

}
====================TASK 6================================================
#include <iostream>
using namespace std;

class Animal
{
public:
virtual void speak() {
cout << "Speak Constructor called" << endl;
}
virtual ~Animal() {
cout << "~Animal Destructor called" << endl;
}
};

class Dog :public Animal


{
public:
void speak() {
cout << "Bow Wooh!" << endl;
}
~Dog() {
cout << "Dog Destructor called." << endl;
}
};

int main()
{
Animal objAnimal;
Dog objDog;
Dog* ptrDog = &objDog;
Animal* ptrAnimal = &ptrDog;
objAnimal.speak();
objDog.speak();
ptrAnimal->speak();
ptrDog->speak();
ptrDog->Animal::speak();
}

You might also like