Skip to content

Commit c5f19e1

Browse files
committed
add: cpp-programming-questions 13_virtual_abstract 00
1 parent 666917d commit c5f19e1

File tree

4 files changed

+67
-4
lines changed

4 files changed

+67
-4
lines changed

13_virtual_function_and_abstract_class/00_questions.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
triangle and rectangle from the base shape. Add to the base class, a member
99
function get_data() to initialise base class data members and another member
1010
function display_area() to compute and display the area of figures. Make
11-
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 triangle or a rectangle interactively, and display the area. Remember the two values given as input will be treated as lengths of two sides in the case of rectangles, and as base and height in the case of the triangles, and used as follows:
12-
Area of rectangle = x * y Area of triangle = 1/2 * x * y
11+
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 triangle or a rectangle interactively, and display the area. Remember the two values given as input will be treated as lengths of two sides in the case of rectangles, and as base and height in the case of the triangles.
1312

1413
02. Extend the above program to display the area of circles. This requires the addition of a new derived class 'circle' that computes the area of a circle. Remember, for a circle we need only one value, its radius, but the get_data() function in the base class requires two values to be passed. (Hint: Make the second argument of get_data() function as a default one with zero value.)
1514

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 triangle and rectangle from the base shape. Add to the base class, a member function setData(double,double) to initialise base class data members and another member function displayArea() to compute and display the area of figures. Make displayArea() 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 triangle or a rectangle interactively, and display the area. Remember the two values given as input will be treated as lengths of two sides in the case of rectangles, and as base and height in the case of the triangles.
2+
3+
// // Header files
4+
#include <iostream>
5+
#include <conio.h>
6+
7+
// // use namespace
8+
using namespace std;
9+
10+
// // define class Shape
11+
class Shape
12+
{
13+
protected:
14+
// // instance member variables
15+
double d1, d2;
16+
17+
public:
18+
// instance member function to set data
19+
void setData(double d1, double d2)
20+
{
21+
this->d1 = d1;
22+
this->d2 = d2;
23+
}
24+
25+
// instance member function to display the area
26+
virtual void displayArea()
27+
{
28+
}
29+
};
30+
31+
// // define class Triangle by inheriting class Shape
32+
class Triangle : public Shape
33+
{
34+
public:
35+
// instance member function to display the area
36+
void displayArea()
37+
{
38+
cout << "\nArea of Triangle => " << 0.5 * d1 * d2;
39+
}
40+
};
41+
42+
// // define class Rectangle by inheriting class Shape
43+
class Rectangle : public Shape
44+
{
45+
public:
46+
// instance member function to display the area
47+
void displayArea()
48+
{
49+
cout << "\nArea of Rectangle => " << d1 * d2;
50+
}
51+
};
52+
53+
int main()
54+
{
55+
Rectangle r1;
56+
r1.setData(4, 5);
57+
r1.displayArea();
58+
59+
Triangle t1;
60+
t1.setData(4, 3);
61+
t1.displayArea();
62+
63+
cout << endl; // Add new line
64+
getch();
65+
return 0;
66+
}
Binary file not shown.

13_virtual_function_and_abstract_class/s.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)