Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

Lab_Manual_06(20F_0159)

Problem_01
#include<iostream>
#include<string>
using namespace std;
class Colony
{
public:
Colony(int size)
{
for (int i = 0; i < size; i++)
{
*(street + i) = "Allocated";
}
for (int i = size; i < 5; i++)
{
*(street + i) = "NotAllocated";
}
};
Colony(const Colony& obj1)
{
street = new string[5];
for (int i = 0; i < 5; i++)
{
*(street + i) = *(obj1.street + i);
}
};
void output()
{
for (int i = 0; i < 5; i++)
{
cout << "Street " << i + 1 << " is " << *(street + i) << endl;
}
};
void change(int size)
{
delete[] street;
street = new string[5];
for (int i = 0; i < size; i++)
{
*(street + i) = "Allocated";
}
for (int i = size; i < 5; i++)
{
*(street + i) = "NotAllocated";
}
};
~Colony()
{
delete[] street;
};
private:
string* street = new string[5];
};
int main()
{

int st = 0;
cout << "Enter Street for Colony 1 : ";
cin >> st;
Colony Colony1(st);
Colony Colony2(Colony1);
cout << "Steets in Colony 1 " << endl;
Colony1.output();
cout << "Steets in Colony 2 " << endl;
Colony2.output();
cout << "Enter change in size of Street for Colony 2 : ";
cin >> st;
Colony2.change(st);
cout << "Steets in Colony 1 " << endl;
Colony1.output();
cout << "Steets in Colony 2 " << endl;
Colony2.output();
return 0;
}
Problem_02
#include<iostream>
#include<string>
using namespace std;
class Colony
{
public:
Colony(int size)
{
for (int i = 0; i < size; i++)
{
*(street + i) = "Allocated";
}
for (int i = size; i < 5; i++)
{
*(street + i) = "NotAllocated";
}
};
Colony(const Colony& obj1, const Colony& obj2)
{
street = new string[5];
for (int i = 0; i < 5; i++)
{
*(street + i) = *(obj1.street + i);
}
};
void output()
{
for (int i = 0; i < 5; i++)
{
cout << "Street " << i + 1 << " is " << *(street + i) << endl;
}
};
void change(int size)
{
delete[] street;
street = new string[5];
for (int i = 0; i < size; i++)
{
*(street + i) = "Allocated";
}
for (int i = size; i < 5; i++)
{
*(street + i) = "NotAllocated";
}
};
~Colony()
{
delete[] street;
};

private:
string* street = new string[5];
};
int main()
{
int st = 0;
cout << "Enter Street for Colony 1 : ";
cin >> st;
Colony ColonyEX(st);
Colony Colony1(st);
Colony Colony2(Colony1, ColonyEX); //You can use this Overload
//Colony Colony2(Colony1, Colony1); //Or you can use this overload
cout << "Steets in Colony 1 " << endl;
Colony1.output();
cout << "Steets in Colony 2 " << endl;
Colony2.output();
cout << "Enter change in size of Street for Colony 2 : ";
cin >> st;
Colony2.change(st);
cout << "Steets in Colony 1 " << endl;
Colony1.output();
cout << "Steets in Colony 2 " << endl;
Colony2.output();
return 0;
}
Problem_03
#include<iostream>
#include<string>
using namespace std;
class Colony
{
public:
Colony(int size)
{
for (int i = 0; i < size; i++)
{
*(street + i) = "Allocated";
}
for (int i = size; i < 5; i++)
{
*(street + i) = "NotAllocated";
}
};
void classfuntion()
{
output();

};
int classfuntion(const Colony& obj)
{
street = new string[5];
for (int i = 0; i < 5; i++)
{
*(street + i) = *(obj.street + i);
}
output();
return 0;
};
int classfuntion2(const Colony& obj)
{
street = new string[5];
for (int i = 0; i < 5; i++)
{
*(street + i) = *(obj.street + i);
}
output();
return 0;
};
void output()
{
for (int i = 0; i < 5; i++)
{
cout << "Street " << i + 1 << " is " << *(street + i) << endl;
}

};
~Colony()
{
delete[] street;
};
private:
string* street = new string[5];
};
int main()
{

int st = 0;
cout << "Enter Street for Colony 1 : ";
cin >> st;
Colony Colony1(st);
Colony Colony2(Colony1);
Colony1.classfuntion();
Colony2.classfuntion2(Colony1);
Colony1.classfuntion(Colony2.classfuntion2(Colony1));
return 0;
}
Problem_04
#include<iostream>
using namespace std;
class class1
{
public:
class1(int a)
{
cout << "Contructor Called : " << endl;
int1 = new double;
*int1 = a;
};
class1(const class1& obj)
{
int1 = new double;
*int1 = *obj.int1;
};
void getdouble(class1 obj)
{
cout << *(obj.int1) << endl;
};
~class1()
{
cout << "Destructor Called : " << endl;
*int1 = -1;
delete[] int1;
*int1 = 0;
};

private:
double *int1;
};
int main()
{
class1 obj1(20);
class1 obj2(obj1);
obj2.getdouble(obj1);
return 0;
}
Problem_05
#include<iostream>
using namespace std;
class class1
{
public:
class1(string name1)
{
name = name1;
};
class1(const class1 &obj)
{
cout << "CopyConstructor is called.." << endl;
};
void function1(class1& name)
{

};
string function2(class1& name)
{
class1 name3(name);
return name3.name;
};
~class1()
{
cout << "Destructor is called" << endl;
};

private:
string name = "Usman";
};
int main()
{
class1 name1("UsmanAlyas");
class1 name2(name1);
name1.function1(name2);
name2 = name1.function2(name2);
}
Problem_06
#include<iostream>
using namespace std;
class class1
{
public:
class1()
{
for (int i = 0; i < 50; i++)
{
firstNAME[i] = NULL;
lastNAME[i] = NULL;
}

};
class1(char* fn, char* ln)
{
for (int i = 0; i < 50; i++)
{

firstNAME[i] = (fn[i]);
lastNAME[i] = (ln[i]);

};
class1(const class1 &obj)
{
for (int i = 0; i < 50; i++)
{
firstNAME[i] = *(obj.firstNAME + i);
lastNAME[i] = *(obj.lastNAME + i);
}
while (*(obj.firstNAME + count) != NULL)
{
firstNAME[count] = *(obj.firstNAME + count);
countcheck();
}
int i = 0;
while (*(obj.lastNAME + i)!=NULL)
{
lastNAME[i] = *(obj.lastNAME + i);
i++;
countcheck();
}

};
static void countcheck()
{
count++;
};
char* getfirstNAME(char* fn)
{
return firstNAME;
};
char* getlastNAME(char* ln)
{
return lastNAME;
};
static int getcount()
{
cout << "Count = " << count << endl;
return count;
};
~class1()
{
cout << "We are in Destructor : " << endl;
cout << "Destructing First Name .. " << endl;
delete[] firstNAME;
cout << "Destructing Last Name .. " << endl;
delete[] lastNAME;
};
private:
char* firstNAME = new char[50];
char* lastNAME = new char[50];
static int count;
};
int class1::count;
int main()
{
char* fn = new char[50];
char* ln = new char[50];
class1 obj1();
cout << "Enter First Name : ";
cin >> fn;
cout << "Enter Last Name : ";
cin >> ln;
class1 obj2(fn, ln);
class1 obj3(obj2);
cout << "FIRST NAME = " << obj3.getfirstNAME(fn) << endl;
cout << "LAST NAME = " << obj3.getlastNAME(ln) << endl;
class1::getcount();
return 0;
}
Problem_07
#include<iostream>
#include<math.h>
using namespace std;
class POINT
{
public:
POINT(int a, int b, int c)//default Constructor
{
setx(a);
sety(b);
setr(c);
};
POINT(int a,float b)//Parameterized Constructor
{
setx(a);
sety(b);
};
POINT(const POINT &obj)//Copy Constructor
{
x = obj.x;
y = obj.y;
};
void ttistance(POINT &obj)
{
r = sqrt((obj.x - x) ^ 2 - (obj.y - y) ^ 2);
output(r);
};
void isZero()
{
if (x == 0 && y == 0)
{
cout << "Origin" << endl;
r = 0;
}
};
void MiddlePoint()
{
if (x == 0 && y == 0)
{
cout << "MiddlePoint" << endl;
r = 0;
}
};
void isGreaterThan(POINT &obj)
{
if (obj.x > obj.y || obj.y > obj.x)
{
obj.r = sqrt(obj.y ^ 2 - obj.x ^ 2);
}
};
void isEqualTo(POINT &obj)
{
if (x == obj.x || y == obj.y)
{
cout << "Equal" << endl;
r = 0;
}
};
void input()
{

};
void output(int distance)
{
cout << "Distance = " << distance << endl;
};
void setx(int a)
{
x = a;
};
void sety(int b)
{
y = b;
};
void setr(int c)
{
r = c;
};
int getx()
{
return x;
};
int gety()
{
return y;
};
int getr()
{
return r;
};
~POINT()
{

};

private:
int x;
int y;
int r;
};
int main()
{
POINT g(0, 0, 0);
g.setx(-2);
g.isZero();
g.gety();
g.MiddlePoint();
g.isEqualTo(g);
POINT r(9, 8.5);
r.isGreaterThan(g);
POINT j(g);
g.isEqualTo(r);
g.isEqualTo(j);
POINT f(-6.7, -9.5);
f.setx(5.5);
f.ttistance(r);
g.ttistance(f);
g.ttistance(r);
return 0;
}

You might also like