-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprog9.cpp
More file actions
53 lines (40 loc) · 822 Bytes
/
prog9.cpp
File metadata and controls
53 lines (40 loc) · 822 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
Write a C++ program to create a base class Number that reads an integer and
derived classes Square and Cube that calculate and display the square and cube
of an integer respectively.
*/
#include <iostream>
using namespace std;
class Number {
private:
int num;
public:
void read_num(int num) { this->num = num; }
int get_num() { return num; }
};
class Square : public Number {
public:
int calc_sq() {
int n = get_num();
return n * n;
}
};
class Cube : public Number {
public:
int calc_cb() {
int n = get_num();
return n * n * n;
}
};
int main() {
Square n1;
Cube n2;
int num;
cout << "Enter the number: ";
cin >> num;
n1.read_num(num);
n2.read_num(num);
cout << "Square is: " << n1.calc_sq() << endl;
cout << "Cube is: " << n2.calc_cb() << endl;
return 0;
}