Skip to content

Commit 319d36a

Browse files
committed
add: cpp-programming-questions 13_virtual_abstract 06
1 parent fb48e83 commit 319d36a

File tree

4 files changed

+73
-2
lines changed

4 files changed

+73
-2
lines changed

12_overloading_overriding_constructor_in_inheritance/03_animal.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Animal
2020
};
2121

2222
// // define class Dog by inheriting class Animal
23-
class Dog
23+
class Dog : public Animal
2424
{
2525
public:
2626
// // instance member function to produce sound

13_virtual_function_and_abstract_class/00_questions.txt

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

1515
05. Design a class hierarchy for a banking system with a base class Account and two derived classes SavingsAccount and CurrentAccount. Each class should have data members for account balance, account number, and an interest rate (for savings account). Implement virtual functions for deposit, withdraw, and display account details. Demonstrate the use of these classes by creating objects and performing transactions.
1616

17-
06. Extend above to display the area of circles. For a circle,only one value is needed i.e. radius but in get_data() function 2 values are passed.
17+
06.
1818

1919
07. Create a base class called Matrix. Use this class to store 4 int type values that could be used to calculate determinants and create matrices. Create class
2020
calculate_determinant which will calculate the determinant of a matrix.Using these classes, calculate the determinant of the matrix.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// // Define a class Animal with a pure virtual function makeSound(). Derive three classes Dog, Cat, and Bird from Animal and implement the makeSound() function in each derived class. Create objects of each class and make them produce sounds.
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 an abstract class Animal
12+
class Animal
13+
{
14+
public:
15+
// // pure virtual function to be overridden by derived classes
16+
virtual void makeSound() = 0;
17+
};
18+
19+
// // define class Dog by inheriting an abstract class Animal
20+
class Dog : public Animal
21+
{
22+
public:
23+
// // override base class function makeSound
24+
void makeSound() override
25+
{
26+
cout << "\nSound of Dog...";
27+
}
28+
};
29+
30+
// // define class Cat by inheriting an abstract class Animal
31+
class Cat : public Animal
32+
{
33+
public:
34+
// // override base class function makeSound
35+
void makeSound() override
36+
{
37+
cout << "\nSound of Cat...";
38+
}
39+
};
40+
41+
// // define class Bird by inheriting an abstract class Animal
42+
class Bird : public Animal
43+
{
44+
public:
45+
// // override base class function makeSound
46+
void makeSound() override
47+
{
48+
cout << "\nSound of Bird...";
49+
}
50+
};
51+
52+
// // Main Function Start
53+
int main()
54+
{
55+
// // instance of Dog
56+
Dog d1;
57+
d1.makeSound();
58+
59+
// // instance of Cat
60+
Cat c1;
61+
c1.makeSound();
62+
63+
// // instance of Bird
64+
Bird b1;
65+
b1.makeSound();
66+
67+
cout << endl; // Add new line
68+
getch();
69+
return 0;
70+
}
71+
// // Main Function End
45.9 KB
Binary file not shown.

0 commit comments

Comments
 (0)