Skip to content

Commit 5cde778

Browse files
committed
add: cpp-programming-questions 13_virtual_abstract 02
1 parent eaee227 commit 5cde778

File tree

3 files changed

+86
-5
lines changed

3 files changed

+86
-5
lines changed

13_virtual_function_and_abstract_class/00_questions.txt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@
44
🟢 Topic ➡ Virtual Function And Abstract Class
55

66

7-
01. 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
8-
triangle and rectangle from the base shape. Add to the base class, a member
9-
function get_data() to initialise base class data members and another member
10-
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.
7+
01. 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.
128

139
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.)
1410

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// // 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.)
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 = 0)
20+
{
21+
this->d1 = d1;
22+
this->d2 = d2;
23+
}
24+
25+
// instance member function to display the area
26+
virtual void displayArea() const
27+
{
28+
cout << "\nNot Implemented this for class Shape...";
29+
}
30+
};
31+
32+
// // define class Triangle by inheriting class Shape
33+
class Triangle : public Shape
34+
{
35+
public:
36+
// instance member function to display the area
37+
void displayArea() const override
38+
{
39+
cout << "\nArea of Triangle => " << 0.5 * d1 * d2;
40+
}
41+
};
42+
43+
// // define class Rectangle by inheriting class Shape
44+
class Rectangle : public Shape
45+
{
46+
public:
47+
// instance member function to display the area
48+
void displayArea() const override
49+
{
50+
cout << "\nArea of Rectangle => " << d1 * d2;
51+
}
52+
};
53+
54+
// // define class Circle by inheriting class Shape
55+
class Circle : public Shape
56+
{
57+
public:
58+
// instance member function to display the area
59+
void displayArea() const override
60+
{
61+
cout << "\nArea of Circle => " << 3.14159 * d1 * d1;
62+
}
63+
};
64+
65+
int main()
66+
{
67+
// // create an instance of Triangle
68+
Triangle t1;
69+
t1.setData(4, 3);
70+
t1.displayArea();
71+
72+
// // create an instance of Rectangle
73+
Rectangle r1;
74+
r1.setData(4, 5);
75+
r1.displayArea();
76+
77+
// // create an instance of Circle
78+
Circle c1;
79+
c1.setData(7.5);
80+
c1.displayArea();
81+
82+
cout << endl; // Add new line
83+
getch();
84+
return 0;
85+
}
49.3 KB
Binary file not shown.

0 commit comments

Comments
 (0)