Skip to content

Commit 4c08e19

Browse files
committed
Initial Commit
1 parent 07d2ed4 commit 4c08e19

File tree

5 files changed

+276
-0
lines changed

5 files changed

+276
-0
lines changed

OOPS-Lab-7/OOPS-Lab-7.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
1. Create a class complex which stores real and imaginary part of a complex number. Include all types of constructors and destructor. The destructor should display a message about the destructor being invoked. Create objects using different constructors and display them.
2+
2. Create a class which stores time in hh:mm format. Include all the constructors. The parameterized constructor should initialize the minute value to zero, if it is not provided.
3+
3. Create a class which stores a sting and its length as data members. Include all the constructors. Include a member function to join two strings and display the concatenated string.
4+
4. WAP to demonstrate the order of call of constructors and destructors for a class.
5+
5. WAP to count number of objects created from a class using concept of static data members and static member function.

OOPS-Lab-7/Q1.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <iostream>
2+
using namespace std;
3+
class complex
4+
{
5+
int real;
6+
int img;
7+
int *r1, *i;
8+
9+
public:
10+
complex()
11+
{
12+
real = 2;
13+
img = 4;
14+
}
15+
complex(int a, int b)
16+
{
17+
real = a;
18+
img = b;
19+
}
20+
complex(complex &c)
21+
{
22+
real = c.real;
23+
img = c.img;
24+
}
25+
complex(int rea)
26+
{
27+
r1 = new int;
28+
i = new int;
29+
*r1 = rea;
30+
*i = 8;
31+
}
32+
void display()
33+
{
34+
cout << real << " + " << img << "i";
35+
}
36+
void output()
37+
{
38+
cout << *r1 << " + " << *i << "i";
39+
}
40+
~complex()
41+
{
42+
cout << "\nDestructor is invoked";
43+
}
44+
};
45+
int main()
46+
{
47+
cout << "\nProgrammed by 2005601_Rishabh Kumar\n";
48+
complex c1;
49+
complex c2(5, 9);
50+
complex c3 = c2;
51+
cout << "\nc1 = ";
52+
c1.display();
53+
cout << "\nc2 = ";
54+
c2.display();
55+
cout << "\nc3 = ";
56+
c3.display();
57+
cout << "\nc4 = ";
58+
complex c4(3);
59+
c4.output();
60+
}

OOPS-Lab-7/Q2.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <iostream>
2+
using namespace std;
3+
class time1
4+
{
5+
private:
6+
int hour, minu;
7+
int *h1, *m1;
8+
9+
public:
10+
time1()
11+
{
12+
hour = 14;
13+
minu = 3;
14+
cout << "\n\nDefault constructor called : " << endl;
15+
}
16+
time1(int h)
17+
{
18+
hour = h;
19+
minu = 0;
20+
cout << "\nDefault Parameterized constructor called : " << endl;
21+
}
22+
time1(int hou, int min)
23+
{
24+
h1 = new int;
25+
m1 = new int;
26+
*h1 = hou;
27+
*m1 = min;
28+
cout << "\nDynamic constructor called : " << endl;
29+
}
30+
time1(time1 &y)
31+
{
32+
hour = y.hour;
33+
minu = y.minu;
34+
}
35+
void display()
36+
{
37+
hour = hour + (minu / 60);
38+
minu = minu % 60;
39+
cout << hour << ":" << minu << endl;
40+
}
41+
void output()
42+
{
43+
cout << *h1 << ":" << *m1 << endl;
44+
}
45+
};
46+
int main()
47+
{
48+
cout << "\nAPNA NAAM PATA LIKHO";
49+
time1 t1;
50+
t1.display();
51+
time1 t2(4);
52+
t2.display();
53+
time1 t4 = t2;
54+
cout << "\nCopy constructor called : " << endl;
55+
t4.display();
56+
time1 t3(6, 7);
57+
t3.output();
58+
return 0;
59+
}

OOPS-Lab-7/Q3.cpp

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#include <iostream>
2+
#include <string>
3+
using namespace std;
4+
class A
5+
{
6+
private:
7+
string s;
8+
string s1;
9+
int len;
10+
string *sPtr;
11+
int *lenPtr;
12+
13+
public:
14+
A() //default constructor
15+
{
16+
s = "";
17+
len = 0;
18+
cout << "**Default constructor is called**" << endl;
19+
}
20+
A(string S, string ree, int l) //parameterized constructor
21+
{
22+
s = S;
23+
s1 = ree;
24+
len = l;
25+
cout << "**Parameterized constructor is called**" << endl;
26+
}
27+
A(string S, string ss) //parameterized constructor
28+
{
29+
s = S;
30+
s1 = ss;
31+
cout << "**Parameterized constructor is called**" << endl;
32+
}
33+
A(int a) //dynamic constructor
34+
{
35+
sPtr = new string;
36+
*sPtr = "Hello";
37+
lenPtr = new int;
38+
*lenPtr = (*sPtr).length();
39+
cout << "**Dynamic constructor**" << endl;
40+
}
41+
A(string S, float L) //dynamic parameterized constructor
42+
{
43+
sPtr = new string;
44+
*sPtr = S;
45+
lenPtr = new int;
46+
*lenPtr = L;
47+
cout << "**Dynamic parameterized constructor**" << endl;
48+
}
49+
~A()
50+
{
51+
cout << "Destructor is invoked" << endl;
52+
delete (sPtr);
53+
delete (lenPtr);
54+
}
55+
void display()
56+
{
57+
cout << "String: " << s << endl;
58+
cout << "String Length: " << len << endl;
59+
cout << endl;
60+
}
61+
void display(int a)
62+
{
63+
cout << "String: " << *sPtr << endl;
64+
cout << "String Length: " << *lenPtr << endl;
65+
cout << endl;
66+
}
67+
void add()
68+
{
69+
string s3 = s.append(s1);
70+
int ll = s3.length();
71+
cout << "Resulted String: " << s3 << endl;
72+
cout << "Resulted String Length: " << ll << endl;
73+
cout << endl;
74+
}
75+
};
76+
int main()
77+
{
78+
cout << "\nProgrammed by 2005601_Rishabh Kumar\n";
79+
string r;
80+
int i;
81+
cout << "\nEnter the string: ";
82+
cin >> r;
83+
i = r.length();
84+
string ree;
85+
float imm;
86+
cout << "\nEnter another string: ";
87+
cin >> ree;
88+
imm = ree.length();
89+
A obj1; //default constructor
90+
obj1.display();
91+
A obj2(r, ree, i); //parameterized constructor
92+
obj2.display();
93+
A obj3(0); //dynamic constructor
94+
obj3.display(0);
95+
A obj4(ree, imm); //dynamic parameterized constructor
96+
obj4.display(0);
97+
A obj5 = obj2; //copy constructor
98+
obj5.display();
99+
A obj6(r, ree); //parameterized constructor
100+
obj6.add();
101+
return 0;
102+
}

OOPS-Lab-7/Q5.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <iostream>
2+
using namespace std;
3+
class test
4+
{
5+
int *l;
6+
static int c;
7+
8+
public:
9+
test()
10+
{
11+
c++;
12+
l = new int(0);
13+
}
14+
test(int n)
15+
{
16+
c++;
17+
l = new int(n);
18+
}
19+
~test()
20+
{
21+
cout << "Deleted" << endl;
22+
delete (l);
23+
}
24+
void display();
25+
static void counter();
26+
};
27+
int test::c = 0;
28+
void test::display()
29+
{
30+
cout << "Number : " << *l << endl;
31+
}
32+
void test::counter()
33+
{
34+
cout << "\nNumber of objects created: " << c << endl << endl;
35+
}
36+
int main()
37+
{
38+
cout << "\nProgrammed by 2005601_Rishabh Kumar\n";
39+
cout << "\nCalling functions :" << endl;
40+
test a(1), b(2), c(3), d(4), e(5), f(6), g(7);
41+
a.display();
42+
b.display();
43+
c.display();
44+
d.display();
45+
e.display();
46+
f.display();
47+
g.display();
48+
a.counter();
49+
return 0;
50+
}

0 commit comments

Comments
 (0)