Skip to content

Commit bff7513

Browse files
committed
add: cpp-programming-questions 13_virtual_abstract 10
1 parent 0fc092c commit bff7513

File tree

5 files changed

+80
-6
lines changed

5 files changed

+80
-6
lines changed

.vscode/settings.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{
22
"files.associations": {
3-
"iostream": "cpp"
3+
"iostream": "cpp",
4+
"array": "cpp",
5+
"string": "cpp"
46
}
57
}

13_virtual_function_and_abstract_class/00_questions.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ display the result. (Summary: Prove that triangle is a right angled triangle usi
2525
that could be used to compute the volume of figures. Derive two specific classes called cube and sphere from the base shape. Add to the base class, a member function setData() to initialise base class data member and another member function displayVolume() to compute and display the volume of figures. Make displayVolume() as a pure virtual function and redefine this function in the derived classes to suit their requirements. Using these three classes, design a program that will accept dimensions of a cube or a sphere interactively, and display the volume.
2626

2727
10. Create a base class called shape. Use this class to store two double type values that could be used to compute the area of figures. Derive two specific classes called
28-
square and parallelogram from the base shape. Add to the base class, a member function get_data() to initialise base class data members and another member function display_area() to compute and display the area of figures. Make display_area() as a virtual function and redefine this function in the derived classes to suit their requirements. Using these three classes, design a program that will accept dimensions of a square or a parallelogram interactively, and display the area
28+
Rectangle and parallelogram from the base shape. Add to the base class, a member function setData() to initialise base class data members and another member function displayArea() to compute and display the area of figures. Make displayArea() as a pure virtual function and redefine this function in the derived classes to suit their requirements. Using these three classes, design a program that will accept dimensions of a square or a parallelogram interactively, and display the area.

13_virtual_function_and_abstract_class/09_volume.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// // Header files
44
#include <iostream>
55
#include <conio.h>
6-
#include <algorithm>
76

87
// // use namespace
98
using namespace std;
@@ -33,7 +32,7 @@ class Cube : public Volume
3332
// // override base class function displayVolume
3433
void displayVolume() const
3534
{
36-
cout << "Volume of Cube => " << data * data * data;
35+
cout << "\nVolume of Cube => " << data * data * data;
3736
}
3837
};
3938

@@ -47,7 +46,7 @@ class Sphere : public Volume
4746
// // override base class function displayVolume
4847
void displayVolume() const
4948
{
50-
cout << "Volume of Sphere => " << 4.0 / 3 * 3.14159 * data * data * data;
49+
cout << "\nVolume of Sphere => " << 4.0 / 3 * 3.14159 * data * data * data;
5150
}
5251
};
5352

@@ -62,7 +61,7 @@ int main()
6261
Cube c1(data);
6362
c1.displayVolume();
6463

65-
cout << "\nEnter Radius of A Sphere => ";
64+
cout << "\n\nEnter Radius of A Sphere => ";
6665
cin >> data;
6766

6867
// // create an instance of Sphere
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// // Create a base class called shape. Use this class to store two double type values that could be used to compute the area of figures. Derive two specific classes called Rectangle and parallelogram from the base shape.Add to the base class, a member function setData() to initialise base class data members and another member function displayArea() to compute and display the area of figures.Make displayArea() as a pure virtual function and redefine this function in the derived classes to suit their requirements.Using these three classes, design a program that will accept dimensions of a square or a parallelogram interactively, and display the area.
2+
3+
// // Header files
4+
#include <iostream>
5+
#include <conio.h>
6+
7+
// // use namespace
8+
using namespace std;
9+
10+
// // define ab abstract class Shape
11+
class Shape
12+
{
13+
protected:
14+
// // instance member variables
15+
double length, breadth;
16+
17+
public:
18+
// // constructor
19+
Shape(int l, int b) : length(l), breadth(b) {}
20+
21+
// // pure virtual function to be overridden by derived classes
22+
virtual void displayArea() const = 0;
23+
};
24+
25+
// // define class Rectangle by inheriting an abstract class Shape
26+
class Rectangle : public Shape
27+
{
28+
public:
29+
// // inheriting the constructor of the base class
30+
using Shape::Shape;
31+
32+
// // override base class function displayArea
33+
void displayArea() const
34+
{
35+
cout << "\nArea of Rectangle => " << length * breadth;
36+
}
37+
};
38+
39+
// // define class Sphere by inheriting an abstract class Volume
40+
class Parallelogram : public Shape
41+
{
42+
public:
43+
// // inheriting the constructor of the base class
44+
using Shape::Shape;
45+
46+
// // override base class function displayArea
47+
void displayArea() const
48+
{
49+
cout << "\nArea of Parallelogram => " << 0.5 * length * breadth;
50+
}
51+
};
52+
53+
int main()
54+
{
55+
double length, breadth;
56+
cout << "\nEnter Dimensions of A Rectangle => ";
57+
cin >> length >> breadth;
58+
59+
// // create an instance of Rectangle
60+
Rectangle r1(length, breadth);
61+
r1.displayArea();
62+
63+
cout << "\n\nEnter Breadth And Height of A Parallelogram => ";
64+
cin >> breadth >> length;
65+
66+
// // create an instance of Sphere
67+
Parallelogram p1(length, breadth);
68+
p1.displayArea();
69+
70+
cout << endl; // Add new line
71+
getch();
72+
return 0;
73+
}
48.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)