Skip to content

Commit 9d8d471

Browse files
committed
Mod 2
1 parent a72e928 commit 9d8d471

File tree

15 files changed

+719
-0
lines changed

15 files changed

+719
-0
lines changed

pyqs/mod2-1.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
int x = 40;
5+
6+
int& setx() { return x; }
7+
8+
int main() {
9+
//setx();
10+
setx() = 22;
11+
cout << "x=" << x << endl;
12+
return 0;
13+
}

pyqs/mod2-10b.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
class Complex
5+
{
6+
public:
7+
int real, img;
8+
9+
void getData()
10+
{
11+
cout << "Enter No." << endl;
12+
cin >> real;
13+
cin >> img;
14+
}
15+
16+
void showData()
17+
{
18+
cout << "(" << real << ") + (" << img << ")i" << endl;
19+
}
20+
21+
Complex addComplex(Complex ob)
22+
{
23+
Complex temp;
24+
temp.real = this->real + ob.real;
25+
temp.img = this->img + ob.img;
26+
27+
return temp;
28+
}
29+
};
30+
31+
int main()
32+
{
33+
Complex num1;
34+
Complex num2;
35+
36+
num1.getData();
37+
num2.getData();
38+
num1.showData();
39+
num2.showData();
40+
41+
Complex res;
42+
res = num1.addComplex(num2);
43+
res.showData();
44+
45+
return 0;
46+
}

pyqs/mod2-11a.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <iostream>
2+
#include <cstring>
3+
using namespace std;
4+
5+
int main()
6+
{
7+
char s1[6] = "Hello";
8+
char s2[6] = "World";
9+
char s3[12]; // Enough space for "Hello World" + '\0'
10+
11+
strcpy(s3, s1); // Copy "Hello" into s3
12+
strcat(s3, " "); // Concatenate space
13+
strcat(s3, s2); // Concatenate "World"
14+
15+
cout << s3 << endl; // Output: Hello World
16+
return 0;
17+
}

pyqs/mod2-11c.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <iostream>
2+
#include <string>
3+
using namespace std;
4+
5+
class ECE_faculty;
6+
class CSE_faculty
7+
{
8+
private:
9+
int highest_degree;
10+
11+
public:
12+
string name;
13+
14+
CSE_faculty(int m, string n) : highest_degree(m), name(n) {}
15+
16+
friend void compare(const CSE_faculty &, const ECE_faculty &);
17+
};
18+
19+
class ECE_faculty
20+
{
21+
private:
22+
int highest_degree;
23+
24+
public:
25+
string name;
26+
27+
ECE_faculty(int m, string n) : highest_degree(m), name(n) {}
28+
29+
friend void compare(const CSE_faculty &, const ECE_faculty &);
30+
};
31+
32+
void compare(const CSE_faculty &cse, const ECE_faculty &ece)
33+
{
34+
cout << "Comparing faculty degrees..." << endl;
35+
if (cse.highest_degree > ece.highest_degree)
36+
cout << cse.name << " (CSE) has a higher degree." << endl;
37+
else if (cse.highest_degree < ece.highest_degree)
38+
cout << ece.name << " (ECE) has a higher degree." << endl;
39+
else
40+
cout << "Both have the same highest degree." << endl;
41+
}
42+
43+
int main()
44+
{
45+
CSE_faculty f1(4, "Dr. Sharma");
46+
ECE_faculty f2(4, "Dr. Rao");
47+
48+
compare(f1, f2);
49+
50+
return 0;
51+
}

pyqs/mod2-13c.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <iostream>
2+
#include <string>
3+
using namespace std;
4+
class A
5+
{
6+
int a;
7+
8+
public:
9+
A()
10+
{
11+
cout << "Default constructor called\n";
12+
}
13+
A(const A &a)
14+
{
15+
cout << "Copy Constructor called\n";
16+
}
17+
};
18+
19+
int main()
20+
{
21+
A obj;
22+
A a1 = obj;
23+
A a2(obj);
24+
return 0;
25+
}

pyqs/mod2-2b.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <iostream>
2+
#include <string>
3+
using namespace std;
4+
5+
class Demo
6+
{
7+
public:
8+
string name;
9+
10+
// Constructor
11+
Demo(string n) : name(n)
12+
{
13+
cout << "Constructing " << name << endl;
14+
}
15+
16+
// Destructor
17+
~Demo()
18+
{
19+
cout << "Destructing " << name << endl;
20+
}
21+
};
22+
23+
Demo globalObj("Global Object"); // global object
24+
25+
void f()
26+
{
27+
Demo funcObj("Object in f()"); // function object
28+
}
29+
30+
int main()
31+
{
32+
cout << "Inside main" << endl;
33+
34+
static Demo staticObj("Static Object"); // static object
35+
Demo("Nameless Object"); // nameless object
36+
Demo localMainObj("Local Main Object"); // local object
37+
{
38+
Demo blockObj("Block Object"); // block object
39+
}
40+
f();
41+
42+
cout << "Exiting main" << endl;
43+
return 0;
44+
}

pyqs/mod2-4b.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
class Solution
5+
{
6+
public:
7+
double n;
8+
int p;
9+
10+
Solution(double x, int y) : n(x), p(y) {}
11+
12+
double power(int p = 2)
13+
{
14+
if (p == 0)
15+
return 1;
16+
else
17+
return n * power(p - 1);
18+
}
19+
};
20+
21+
int main()
22+
{
23+
double n;
24+
int p;
25+
26+
cout << "Enter Number = ";
27+
cin >> n;
28+
cout << "Enter exponent = ";
29+
cin >> p;
30+
Solution obj(n, p);
31+
cout << "Result = " << obj.power(p) << endl;
32+
cout << "Default Result = " << obj.power() << endl;
33+
return 0;
34+
}

pyqs/mod2-4c.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
class TIME
5+
{
6+
public:
7+
int hour, minute, second;
8+
9+
TIME(int h, int m, int s)
10+
{
11+
hour = h;
12+
minute = m;
13+
second = s;
14+
}
15+
16+
void display()
17+
{
18+
cout << hour << ":" << minute << ":" << second << endl;
19+
}
20+
21+
TIME add(TIME ob)
22+
{
23+
TIME temp(0, 0, 0);
24+
25+
temp.second = this->second + ob.second;
26+
temp.minute = this->minute + ob.minute;
27+
temp.hour = this->hour + ob.hour;
28+
29+
if (temp.second >= 60)
30+
{
31+
temp.second -= 60;
32+
temp.minute += 1;
33+
}
34+
35+
if (temp.minute >= 60)
36+
{
37+
temp.minute -= 60;
38+
temp.hour += 1;
39+
}
40+
41+
return temp;
42+
}
43+
};
44+
45+
int main()
46+
{
47+
int h1, h2;
48+
int m1, m2;
49+
int s1, s2;
50+
51+
cout << "Enter time 1" << endl;
52+
cin >> h1;
53+
cin >> m1;
54+
cin >> s1;
55+
cout << "Enter time 2" << endl;
56+
cin >> h2;
57+
cin >> m2;
58+
cin >> s2;
59+
60+
TIME t1(h1, m1, s1);
61+
TIME t2(h2, m2, s2);
62+
t1.display();
63+
t2.display();
64+
65+
TIME res(0, 0, 0);
66+
res = t1.add(t2);
67+
cout << "Result:" << endl;
68+
res.display();
69+
return 0;
70+
}

pyqs/mod2-5a.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
class Employee
5+
{
6+
public:
7+
int employee_id;
8+
string name;
9+
float salary;
10+
11+
Employee(int id, string n, float s) : employee_id(id), name(n), salary(s) {}
12+
13+
void show()
14+
{
15+
cout << "Employee Id: " << employee_id << endl;
16+
cout << "Name: " << name << endl;
17+
cout << "Salary: " << salary << endl;
18+
}
19+
};
20+
21+
class Manager
22+
{
23+
public:
24+
int manager_id;
25+
string name;
26+
float salary;
27+
28+
Manager(int id, string n, float s) : manager_id(id), name(n), salary(s) {}
29+
30+
void show()
31+
{
32+
cout << "Manager Id: " << manager_id << endl;
33+
cout << "Name: " << name << endl;
34+
cout << "Salary: " << salary << endl;
35+
}
36+
37+
void promote(Employee &emp, float inc)
38+
{
39+
emp.salary += inc;
40+
cout << "Promoted " << emp.name << " Salary to = " << emp.salary << endl;
41+
}
42+
};
43+
44+
int main() {
45+
Employee e1(101, "Alice", 40000);
46+
Employee e2(102, "Bob", 45000);
47+
Employee e3(103, "Charlie", 42000);
48+
49+
Manager m1(201, "Diana", 80000);
50+
51+
cout << "Initial Employee Details:\n";
52+
e1.show();
53+
e2.show();
54+
e3.show();
55+
56+
cout << "Manager Details:\n";
57+
m1.show();
58+
59+
// Promoting some employees
60+
m1.promote(e1, 10);
61+
m1.promote(e3, 15);
62+
63+
cout << "Updated Employee Details After Promotion:\n";
64+
e1.show();
65+
e3.show();
66+
67+
return 0;
68+
}

0 commit comments

Comments
 (0)