Skip to content

Commit ac6ebda

Browse files
Added documentation for the code
Added documentation for the code
1 parent a71fa43 commit ac6ebda

36 files changed

+1911
-187
lines changed
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
// WAP in C++ to find out the area of circle,square, rectangle & triangle using function overloading.
5+
6+
/**
7+
* @brief Calculates and prints the area of a circle.
8+
*
9+
* This function takes a float value representing the radius of a circle,
10+
* calculates the area using the formula π*r^2, and prints the result.
11+
*
12+
* @param r The radius of the circle. It must be a positive value.
13+
*
14+
* @return void. The function does not return a value.
15+
*/
16+
void area(float r)
17+
{
18+
float c = 3.14 * r * r;
19+
cout << "Area of circle is:" << c << endl;
20+
}
21+
22+
/**
23+
* @brief Calculates and prints the area of a square.
24+
*
25+
* This function takes an integer value representing the side of a square,
26+
* calculates the area using the formula s^2, and prints the result.
27+
*
28+
* @param s The side of the square. It must be a positive value.
29+
*
30+
* @return void. The function does not return a value.
31+
*/
32+
void area(int s)
33+
{
34+
int sq = s * s;
35+
cout << "Area of square is:" << sq << endl;
36+
}
37+
38+
/**
39+
* @brief Calculates and prints the area of a rectangle.
40+
*
41+
* This function takes two integer values representing the length and breadth of a rectangle,
42+
* calculates the area using the formula length*breadth, and prints the result.
43+
*
44+
* @param l The length of the rectangle. It must be a positive value.
45+
* @param b The breadth of the rectangle. It must be a positive value.
46+
*
47+
* @return void. The function does not return a value.
48+
*/
49+
void area(int l, int b)
50+
{
51+
int r = l * b;
52+
cout << "Area of rectangle is:" << r << endl;
53+
}
54+
/**
55+
* @brief Calculates and prints the area of a triangle.
56+
*
57+
* This function takes two float values representing the base and height of a triangle,
58+
* calculates the area using the formula 0.5*base*height, and prints the result.
59+
*
60+
* @param b The base of the triangle. It must be a positive value.
61+
* @param h The height of the triangle. It must be a positive value.
62+
*
63+
* @return void. The function does not return a value. It only prints the calculated area.
64+
*/
65+
void area(float b, float h)
66+
{
67+
float t = 0.5 * b * h;
68+
cout << "Area of triangle is:" << t << endl;
69+
}
70+
71+
/**
72+
* @brief The main function of the program.
73+
*
74+
* This function prompts the user to enter the dimensions of a square, rectangle, circle, and triangle,
75+
* then calls the appropriate area function to calculate and print the area of each shape.
76+
*
77+
* @return 0. The function does not return a value.
78+
*/
79+
int main()
80+
{
81+
int s, l, b;
82+
float r, bs, ht;
83+
cout << "Enter side of a square:";
84+
cin >> s;
85+
cout << "Enter length and breadth of rectangle:";
86+
cin >> l >> b;
87+
cout << "Enter radius of circle:";
88+
cin >> r;
89+
cout << "Enter base and height of triangle:";
90+
cin >> bs >> ht;
91+
area(r);
92+
area(s);
93+
area(l, b);
94+
area(bs, ht);
95+
/*Enter side of a square:10
96+
Enter length and breadth of rectangle:12 13
97+
Enter radius of circle:2.33
98+
Enter base and height of triangle:15 67
99+
Area of circle is:17.0467
100+
Area of square is:100
101+
Area of rectangle is:156
102+
Area of triangle is:502.5 */
103+
return 0;
104+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <iostream>
2+
using namespace std;
3+
class Cal
4+
{
5+
public:
6+
/**
7+
* @brief Adds two integers together.
8+
*
9+
* This function takes two integers as input and returns their sum.
10+
*
11+
* @param a The first integer to add.
12+
* @param b The second integer to add.
13+
*
14+
* @return The sum of the two input integers.
15+
*/
16+
int add(int a, int b)
17+
{
18+
return a + b;
19+
}
20+
/**
21+
* @brief Adds three integers together.
22+
*
23+
* This function takes three integers as input and returns their sum.
24+
*
25+
* @param a The first integer to add.
26+
* @param b The second integer to add.
27+
* @param c The third integer to add.
28+
*
29+
* @return The sum of the three input integers.
30+
*/
31+
int add(int a, int b, int c)
32+
{
33+
return a + b + c;
34+
}
35+
};
36+
37+
/**
38+
* @brief Main function to demonstrate the usage of the Cal class.
39+
*
40+
* This function creates an instance of the Cal class, calls its add() method with two and three arguments,
41+
* and prints the results to the console.
42+
*
43+
* @return 0 to indicate successful program execution.
44+
*/
45+
int main()
46+
{
47+
Cal C;
48+
cout << C.add(10, 20) << endl; // Prints the sum of 10 and 20.
49+
cout << C.add(12, 20, 23); // Prints the sum of 12, 20, and 23.
50+
return 0;
51+
}
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <iostream>
2+
using namespace std;
3+
class calc
4+
{
5+
public:
6+
/**
7+
* @brief Adds two integers together.
8+
*
9+
* This function takes two integers as input and returns their sum.
10+
*
11+
* @param a The first integer to add.
12+
* @param b The second integer to add.
13+
*
14+
* @return The sum of the two input integers.
15+
*/
16+
int add(int a, int b)
17+
{
18+
return a + b;
19+
}
20+
/**
21+
* @brief Adds three integers together.
22+
*
23+
* This function takes three integers as input and returns their sum.
24+
*
25+
* @param a The first integer to add.
26+
* @param b The second integer to add.
27+
* @param c The third integer to add.
28+
*
29+
* @return The sum of the three input integers.
30+
*/
31+
float add(int a, int b, int c)
32+
{
33+
return a + b + c;
34+
}
35+
};
36+
37+
/**
38+
* @brief Main function to demonstrate the usage of the calc class.
39+
*
40+
* This function creates an instance of the calc class, calls its add() method with two and three parameters,
41+
* and prints the results to the console.
42+
*
43+
* @return 0 to indicate successful program execution.
44+
*/
45+
int main()
46+
{
47+
calc c;
48+
cout << c.add(12, 34) << endl; // Calls the add() method with two parameters.
49+
cout << c.add(10, 20, 90) << endl; // Calls the add() method with three parameters.
50+
return 0;
51+
}

Overloading/Operator/Complex-number-string.-code.cpp

+40-5
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,41 @@ class Complex
66
{
77
private:
88
int real, imag;
9+
910
public:
11+
/**
12+
* @brief Default constructor for the Complex class.
13+
*
14+
* Initializes the real and imaginary parts of the complex number to 0.
15+
*/
1016
Complex()
1117
{
1218
real = imag = 0;
1319
}
14-
Complex (int r, int i)
20+
21+
/**
22+
* @brief Constructor for the Complex class.
23+
*
24+
* Initializes a complex number with given real and imaginary parts.
25+
*
26+
* @param r The real part of the complex number.
27+
* @param i The imaginary part of the complex number.
28+
*/
29+
Complex(int r, int i)
1530
{
1631
real = r;
1732
imag = i;
1833
}
34+
35+
/**
36+
* @brief Converts the complex number to a string representation.
37+
*
38+
* This function generates a string representation of the complex number in the format "(real+imaginaryi)".
39+
* If the imaginary part is zero, it is omitted from the string.
40+
* If the imaginary part is negative, its absolute value is used and a minus sign is added before it.
41+
*
42+
* @return A string representation of the complex number.
43+
*/
1944
string to_string()
2045
{
2146
stringstream ss;
@@ -33,10 +58,20 @@ class Complex
3358
return ret;
3459
}
3560
};
61+
62+
/**
63+
* @brief Main function to demonstrate the usage of the Complex class.
64+
*
65+
* This function creates two Complex objects, c1 and c2, with given real and imaginary parts.
66+
* It then adds these two complex numbers using the overloaded '+' operator and stores the result in the 'res' variable.
67+
* Finally, it prints the string representation of the result using the 'to_string' method of the Complex class.
68+
*
69+
* @return 0 to indicate successful program execution.
70+
*/
3671
int main()
3772
{
38-
Complex c1(8, -5), c2(2, 3);
39-
Complex res = c1 + c2;
40-
cout << res.to_string();
73+
Complex c1(8, -5), c2(2, 3); ///< Initialize two Complex objects with given real and imaginary parts.
74+
Complex res = c1 + c2; ///< Add the two complex numbers using the overloaded '+' operator.
75+
cout << res.to_string(); ///< Print the string representation of the result.
76+
return 0;
4177
}
42-

0 commit comments

Comments
 (0)