Skip to content

Commit 7c3e7fa

Browse files
committed
add: cpp-programming-questions 13_virtual_abstract 03
1 parent 5cfa613 commit 7c3e7fa

File tree

5 files changed

+78
-2
lines changed

5 files changed

+78
-2
lines changed

.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"files.associations": {
3+
"iostream": "cpp"
4+
}
5+
}

13_virtual_function_and_abstract_class/00_questions.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
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.
88

9-
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.)
9+
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 setData() function in the base class requires two values to be passed. (Hint: Make the second argument of setData() function as a default one with zero value.)
1010

1111
03. Using the concept of pointers, write a function that swaps the private data values of two objects of the same class type.
1212

13_virtual_function_and_abstract_class/02_extend_01.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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.)
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 setData() function in the base class requires two values to be passed. (Hint: Make the second argument of setData() function as a default one with zero value.)
22

33
// // Header files
44
#include <iostream>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// // Using the concept of pointers, write a function that swaps the private data values of two objects of the same class type.
2+
3+
// // Header files
4+
#include <iostream>
5+
#include <conio.h>
6+
7+
// // use namespace
8+
using namespace std;
9+
10+
// // define class Integer
11+
class Integer
12+
{
13+
protected:
14+
// // instance member variable
15+
int data;
16+
17+
public:
18+
// // constructors
19+
Integer() {}
20+
21+
Integer(int data) : data(data) {}
22+
23+
// instance member function to set data
24+
void setData(int data)
25+
{
26+
this->data = data;
27+
}
28+
29+
// instance member function to get data
30+
int getData() const
31+
{
32+
return data;
33+
}
34+
35+
// instance member function to swap values of private data members of two objects
36+
void swap(Integer *obj2)
37+
{
38+
// // Check if the pointer is valid (not nullptr)
39+
if (obj2)
40+
{
41+
int temp = data;
42+
data = obj2->data;
43+
obj2->data = temp;
44+
}
45+
else
46+
{
47+
cout << "\n!!! Invalid Pointer provided for Swapping." << endl;
48+
}
49+
}
50+
};
51+
52+
int main()
53+
{
54+
// // create an instances of Integer
55+
Integer i1(2), i2(6);
56+
57+
cout << "\n>>>>>>> Before Swapping <<<<<<<<";
58+
cout << "\ni1.data => " << i1.getData();
59+
cout << "\ni2.data => " << i2.getData();
60+
61+
// // swap value of private data members
62+
i1.swap(&i2);
63+
64+
cout << "\n\n>>>>>>> After Swapping <<<<<<<<";
65+
cout << "\ni1.data => " << i1.getData();
66+
cout << "\ni2.data => " << i2.getData();
67+
68+
cout << endl; // Add new line
69+
getch();
70+
return 0;
71+
}
Binary file not shown.

0 commit comments

Comments
 (0)