Skip to content

Commit d13ccda

Browse files
committed
add : cpp_programming_questions 10
1 parent 6ddeaa3 commit d13ccda

File tree

9 files changed

+135
-17
lines changed

9 files changed

+135
-17
lines changed

01_basics_of_c++/04_area_of_circle.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ using namespace std;
1010
// // Main Function Start
1111
int main()
1212
{
13-
float radius, area;
13+
double radius, area;
1414

1515
cout << "\nEnter Radius of Circle => ";
1616
cin >> radius;

01_basics_of_c++/05_volume_of_cuboid.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ using namespace std;
1010
// // Main Function Start
1111
int main()
1212
{
13-
float l, b, h, vol;
13+
double l, b, h, vol;
1414

1515
cout << "\nEnter Length, Breadth and Height of Cuboid to Find Volume => ";
1616
cin >> l >> b >> h;

01_basics_of_c++/06_average.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ using namespace std;
1010
// // Main Function Start
1111
int main()
1212
{
13-
float num1, num2, num3, sum, avg;
13+
double num1, num2, num3, sum, avg;
1414

1515
cout << "\nEnter 3 Numbers to Find Average => ";
1616
cin >> num1 >> num2 >> num3;

03_classes_and_objects/00_questions.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ Reverse of a Number using class.
2525
09. Define a class Circle and define an instance member function to find the area of the circle.
2626

2727
10. Define a class Area and define instance member functions to find the area of the
28-
different shapes like square, rectangle , circle etc.
28+
different shapes like square, rectangle, triangle and circle etc.

03_classes_and_objects/01_complex.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ using namespace std;
1111
class Complex
1212
{
1313
// // instance member variables
14-
float real;
15-
float imag;
14+
double real;
15+
double imag;
1616

1717
public:
1818
// // instance member function to set compelx number
19-
void setComplex(float r, float i)
19+
void setComplex(double r, double i)
2020
{
2121
real = r;
2222
imag = i;
@@ -34,7 +34,7 @@ class Complex
3434
int main()
3535
{
3636
Complex c1; // create object of Complex
37-
float real, imag;
37+
double real, imag;
3838

3939
// // Get Complex number
4040
cout << "\n>>>>>>>> Enter A Complex Number <<<<<<<<<\n";

03_classes_and_objects/08_area_of_rectangle.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ using namespace std;
1111
class Rectangle
1212
{
1313
// // instance member variables
14-
float length, breadth, area;
14+
double length, breadth, area;
1515

1616
public:
1717
// // instance member function to set length of rectangle
@@ -34,13 +34,13 @@ class Rectangle
3434
}
3535

3636
// // instance member function to get length of rectangle
37-
float getLength(int l)
37+
double getLength(int l)
3838
{
3939
return length;
4040
}
4141

4242
// // instance member function to get breadth of rectangle
43-
float getBreadth(int b)
43+
double getBreadth(int b)
4444
{
4545
return breadth;
4646
}
@@ -52,7 +52,7 @@ class Rectangle
5252
}
5353

5454
// // instance member function to get the area of rectangle
55-
int getArea()
55+
double getArea()
5656
{
5757
return area;
5858
}
@@ -62,7 +62,7 @@ class Rectangle
6262
int main()
6363
{
6464
Rectangle rec1; // create object of Rectangle
65-
float l, b, area;
65+
double l, b, area;
6666

6767
// // Get length and breadth of a rectangle to find its area
6868
cout << "\nEnter Length and Breadth of A Rectangle => ";

03_classes_and_objects/09_area_of_circle.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ using namespace std;
1111
class Circle
1212
{
1313
// // instance member variables
14-
float radius, area;
14+
double radius, area;
1515

1616
public:
1717
// // instance member function to set radius of circle
@@ -21,7 +21,7 @@ class Circle
2121
}
2222

2323
// // instance member function to get radius of circle
24-
float getRadius(int l)
24+
double getRadius(int l)
2525
{
2626
return radius;
2727
}
@@ -33,7 +33,7 @@ class Circle
3333
}
3434

3535
// // instance member function to get the area of circle
36-
int getArea()
36+
double getArea()
3737
{
3838
return area;
3939
}
@@ -43,7 +43,7 @@ class Circle
4343
int main()
4444
{
4545
Circle cir1; // create object of Circle
46-
float rad, area;
46+
double rad, area;
4747

4848
// // Get radius of a circle to find its area
4949
cout << "\nEnter Radius of A Circle => ";

03_classes_and_objects/10_area.cpp

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// // Define a class Shape and define an instance member function to find Reverse of a Number using class.
2+
3+
// // Header files
4+
#include <iostream>
5+
#include <conio.h>
6+
#include <string.h>
7+
8+
// // use namespace
9+
using namespace std;
10+
11+
// // define class Shape
12+
class Shape
13+
{
14+
// // instance member variables
15+
char shape[10];
16+
double dim1, dim2, area;
17+
18+
public:
19+
// // instance member function to set shape
20+
void setShape(char *shp)
21+
{
22+
strlwr(shp);
23+
strcpy(shape, shp);
24+
}
25+
26+
// // instance member function to get shape
27+
char *getShape(char *shp)
28+
{
29+
strcpy(shp, shape);
30+
return shp;
31+
}
32+
33+
// // instance member function to set dimensions
34+
double setDimensions(double d1, double d2 = 0)
35+
{
36+
dim1 = d1;
37+
dim2 = d2;
38+
}
39+
40+
// // instance member function to calculate area
41+
void calculateArea()
42+
{
43+
if (strcmp(shape, "square") == 0)
44+
area = dim1 * dim1;
45+
else if (strcmp(shape, "rectangle") == 0)
46+
area = dim1 * dim2;
47+
else if (strcmp(shape, "triangle") == 0)
48+
area = (dim1 * dim2) / 2;
49+
else
50+
area = 22.0 / 7 * dim1 * dim1;
51+
}
52+
53+
// // instance member function to get the area
54+
double getArea()
55+
{
56+
return area;
57+
}
58+
};
59+
60+
// // Main Function Start
61+
int main()
62+
{
63+
Shape square, rectangle, triangle, circle; // create objects of Shape
64+
double dim1, dim2, area;
65+
66+
// // Get length of a square to find its area
67+
cout << "\nEnter Length of A Square => ";
68+
cin >> dim1;
69+
70+
square.setShape((char *)"square"); // set shape
71+
square.setDimensions(dim1); // set dimensions of square
72+
square.calculateArea(); // find area
73+
74+
// // Get and display area of rectangle
75+
area = square.getArea();
76+
cout << "\nArea of Square => " << area;
77+
78+
// // Get length and breadth of a rectangle to find its area
79+
cout << "\n\nEnter Length and Breadth of A Rectangle => ";
80+
cin >> dim1 >> dim2;
81+
82+
rectangle.setShape((char *)"rectangle"); // set shape
83+
rectangle.setDimensions(dim1, dim2); // set dimensions of rectangle
84+
rectangle.calculateArea(); // find area
85+
86+
// // Get and display area of rectangle
87+
area = rectangle.getArea();
88+
cout << "\nArea of Rectangle => " << area;
89+
90+
// // Get length and breadth of a rectangle to find its area
91+
cout << "\n\nEnter Height and Breadth of A Triangle => ";
92+
cin >> dim1 >> dim2;
93+
94+
triangle.setShape((char *)"triangle"); // set shape
95+
triangle.setDimensions(dim1, dim2); // set dimensions of triangle
96+
triangle.calculateArea(); // find area
97+
98+
// // Get and display area of rectangle
99+
area = triangle.getArea();
100+
cout << "\nArea of Triangle => " << area;
101+
102+
// // Get radius of a circle to find its area
103+
cout << "\n\nEnter Radius of A Circle => ";
104+
cin >> dim1;
105+
106+
circle.setShape((char *)"circle"); // set shape
107+
circle.setDimensions(dim1); // set dimension of circle
108+
circle.calculateArea(); // find area
109+
110+
// // Get and display area of circle
111+
area = circle.getArea();
112+
cout << "\nArea of Circle => " << area;
113+
114+
cout << endl; // Add new line
115+
getch();
116+
return 0;
117+
}
118+
// // Main Function End

03_classes_and_objects/10_area.exe

47.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)