Skip to content

Commit d47e653

Browse files
C++ Code
0 parents  commit d47e653

22 files changed

+724
-0
lines changed

1.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//WAP to find factorial of a given number using recursion.
2+
#include<iostream>
3+
using namespace std;
4+
5+
int factorial(int n);
6+
7+
int main()
8+
{
9+
int n;
10+
cout<< "Enter an Positive Number: ";
11+
cin>> n;
12+
13+
cout<< "Factorial of " << n << " = " << factorial(n);
14+
return 0;
15+
}
16+
17+
int factorial(int n)
18+
{
19+
if (n>=1)
20+
return n*factorial(n-1);
21+
else
22+
return 1;
23+
}

10.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//WAP create a class student take public data members roll no, name & age and enter these data from main function.
2+
#include<iostream>
3+
#include<string>
4+
using namespace std;
5+
6+
class students
7+
{
8+
public:
9+
string Name;
10+
int rollno;
11+
int age;
12+
13+
void displaydata()
14+
{
15+
cout<< "\nStudent Roll No.: "<<rollno << "\nStudent Name: "<<Name << "\nStudent Age: "<<age;
16+
}
17+
};
18+
19+
int main()
20+
{
21+
students one;
22+
cout<< "Enter Student Roll No.: ";
23+
cin>> one.rollno;
24+
cout<< "Enter Student Name: ";
25+
cin>> one.Name;
26+
cout<< "Enter Student Age: ";
27+
cin>> one.age;
28+
29+
one.displaydata();
30+
31+
return 0;
32+
}

11.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//WAP to show working of parameterized constructor.
2+
#include<iostream>
3+
#include<string>
4+
//using namespace std;
5+
using std::string;
6+
using std::cout;
7+
using std::cin;
8+
9+
class students
10+
{
11+
public:
12+
string Name;
13+
int rollno;
14+
int age;
15+
16+
students(string x,int y,int z);
17+
18+
void displaydata()
19+
{
20+
cout<< "\n\nStudent Roll No.: "<<rollno << "\nStudent Name: "<<Name << "\nStudent Age: "<<age;
21+
}
22+
};
23+
24+
students::students(string x,int y,int z)
25+
{
26+
Name = x;
27+
rollno = y;
28+
age = z;
29+
}
30+
31+
int main()
32+
{
33+
students one("Sagar", 40, 20 ),two("Mohit", 27, 20 );
34+
one.displaydata();
35+
two.displaydata();
36+
37+
return 0;
38+
}

12.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//WAP to copy value of 2 parameters from one constructor to another using copy constructor.
2+
#include<iostream>
3+
#include<string>
4+
using namespace std;
5+
6+
class students
7+
{
8+
public:
9+
int a,b;
10+
students() //Default Constructor
11+
{
12+
a=0;
13+
b=0;
14+
}
15+
students(int x,int y) //1st Constructor
16+
{
17+
a=x;
18+
b=y;
19+
}
20+
students(students & object) //Copy Constructor
21+
{
22+
cout<< "\nCopy Constructor Called";
23+
a=object.a;
24+
b=object.b;
25+
}
26+
void displaydata()
27+
{
28+
cout<< "\nA: "<< a <<"\nB: "<< b <<"\n";
29+
}
30+
};
31+
32+
int main()
33+
{
34+
students one;
35+
students two(2,3);
36+
37+
//one.displaydata();
38+
two.displaydata();
39+
40+
students s1(two);
41+
s1.displaydata();
42+
return 0;
43+
}

13.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//WAP to implementing an array of objects in a class using member functions to input & display data.
2+
#include<iostream>
3+
using namespace std;
4+
5+
class employee
6+
{
7+
private:
8+
int id;
9+
int age;
10+
float salary;
11+
12+
public:
13+
void displaydata(int i);
14+
void setdata(int i)
15+
{
16+
cout<< "Enter "<< i <<" Employee ID: ";
17+
cin>> id;
18+
cout<< "Enter "<< i <<" Employee Age: ";
19+
cin>> age;
20+
cout<< "Enter "<< i <<" Employee Salary: ";
21+
cin>> salary;
22+
}
23+
};
24+
25+
void employee:: displaydata(int i)
26+
{
27+
cout<< "\nEmployee " << i <<" ID: " << id <<" \nAge: " << age <<" \nSalary: " << salary <<"\n\n";
28+
}
29+
30+
int main()
31+
{
32+
int i;
33+
employee one[4];
34+
35+
for(i=1;i<=4;i++)
36+
{
37+
one[i].setdata(i);
38+
one[i].displaydata(i);
39+
}
40+
return 0;
41+
}

14.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//WAP to print sum of elements above the diagonal elements in a matrix of 3*3 elements.
2+
#include<iostream>
3+
using namespace std;
4+
5+
int main()
6+
{
7+
int i,j,sumofdiagonal;
8+
9+
int a[3][3]={1,2,3,
10+
4,5,6,
11+
7,8,9};
12+
13+
sumofdiagonal=0;
14+
15+
for(i=0;i<3;i++)
16+
{
17+
cout<< '\n';
18+
for(j=0;j<3;j++)
19+
{
20+
if(i==0&&j==0)
21+
{
22+
sumofdiagonal=a[0][0];
23+
}
24+
else if(j >= i)
25+
{
26+
sumofdiagonal=sumofdiagonal+a[i][j];
27+
a[i][j]=a[i][j+1];
28+
}
29+
}
30+
}
31+
cout<< "Sum of Upper Diagonal Elements Are: ";
32+
cout<< sumofdiagonal;
33+
}

15.cpp

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*WAP to show one class students having data member roll no, name & age.
2+
Inherits some features of teacher, when inherited public-ally.*/
3+
#include<iostream>
4+
#include<string>
5+
using namespace std;
6+
7+
class teacher
8+
{
9+
public:
10+
char sagar(char mohit)
11+
{
12+
cout << "\n\nEnter Teacher's Initials: ";
13+
cin >> mohit;
14+
cout << "Teacher's Initials: ";
15+
cout << mohit;
16+
}
17+
};
18+
19+
class students: teacher
20+
{
21+
public:
22+
string name;
23+
int roll_no;
24+
int age;
25+
26+
void displaydata(char mohit)
27+
{
28+
cout << "\nStudent Roll No.: "<< roll_no << "\nStudent Name: "<< name << "\nStudent Age: "<< age;
29+
sagar(mohit);
30+
}
31+
};
32+
33+
int main()
34+
{
35+
char mohit;
36+
37+
students one;
38+
cout<< "Enter Student Roll No.: ";
39+
cin>> one.roll_no;
40+
cout<< "Enter Student Name: ";
41+
cin>> one.name;
42+
cout<< "Enter Student Age: ";
43+
cin>> one.age;
44+
45+
one.displaydata(mohit);
46+
47+
return 0;
48+
}

16.cpp

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//WAP for implementing multiple inheritance concept using classes.
2+
#include<iostream>
3+
#include<string>
4+
using namespace std;
5+
6+
class teacher
7+
{
8+
public:
9+
char sagar(char mohit)
10+
{
11+
cout << "\n\nEnter Teacher's Initials: ";
12+
cin >> mohit;
13+
cout << "Teacher's Initials: ";
14+
cout << mohit;
15+
}
16+
};
17+
18+
class chor
19+
{
20+
public:
21+
void prince()
22+
{
23+
cout<< "\n\nSagar is a Bad Boy";
24+
}
25+
};
26+
27+
class students: teacher,chor
28+
{
29+
public:
30+
string name;
31+
int roll_no;
32+
int age;
33+
34+
void displaydata(char mohit)
35+
{
36+
cout << "\nStudent Roll No.: "<< roll_no << "\nStudent Name: "<< name << "\nStudent Age: "<< age;
37+
sagar(mohit);
38+
prince();
39+
}
40+
};
41+
42+
int main()
43+
{
44+
char mohit;
45+
46+
students one;
47+
cout<< "Enter Student Roll No.: ";
48+
cin>> one.roll_no;
49+
cout<< "Enter Student Name: ";
50+
cin>> one.name;
51+
cout<< "Enter Student Age: ";
52+
cin>> one.age;
53+
54+
one.displaydata(mohit);
55+
56+
return 0;
57+
}
58+

17.cpp

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//WAP to overload a binary operator ‘+’ to add two values using objects.
2+
#include<iostream>
3+
using namespace std;
4+
5+
class imaginary
6+
{
7+
private:
8+
int a;
9+
int b;
10+
11+
public:
12+
imaginary(int v1)
13+
{
14+
b=v1;
15+
}
16+
void operator+(imaginary obj1)
17+
{
18+
a = b + obj1.b;
19+
}
20+
void printdata(int i)
21+
{
22+
cout<< "Your imaginary No." << i << " " << b << "i\n";
23+
}
24+
void printcomplexsum()
25+
{
26+
cout<< "Sum of Your Complex No. " << a << "i\n";
27+
}
28+
};
29+
30+
int main()
31+
{
32+
imaginary obj0(2),obj1(4);
33+
34+
obj0.printdata(0);
35+
36+
obj1.printdata(1);
37+
38+
obj0+obj1;
39+
obj0.printcomplexsum();
40+
41+
return 0;
42+
}

0 commit comments

Comments
 (0)