File tree Expand file tree Collapse file tree 1 file changed +53
-0
lines changed Expand file tree Collapse file tree 1 file changed +53
-0
lines changed Original file line number Diff line number Diff line change
1
+ /* Suppose a teacher said to his student to create a class Display which have a
2
+ member function show through which it can print integer,float and string.So student
3
+ said OK sir i will use function overloading to do that..
4
+ Function Overloading: It is a way through which you can create more than one function
5
+ of same name but different argument or you can say that different signature */
6
+ #include < iostream>
7
+ #include < string.h>
8
+ using namespace std ;
9
+ // creating Display class which will display integer,float and string.
10
+ class Display
11
+ {
12
+ public:
13
+ // this show function will display you integer
14
+ void show (int n)
15
+ {
16
+ cout<<n<<" \n " ;
17
+ }
18
+ // this show function will display you float value
19
+ void show (float n)
20
+ {
21
+ cout<<n<<" \n " ;
22
+ }
23
+ // this show function will display you string value
24
+ void show (string n)
25
+ {
26
+ cout<<n<<" \n " ;
27
+ }
28
+ };
29
+ // driver code
30
+ int main ()
31
+ {
32
+ // initialize variable
33
+ int n;
34
+ float a;
35
+ char *str;
36
+ // dynamically memory allocation
37
+ str=(char *)malloc (sizeof (char )*1000 );
38
+ // initialize Display class variable d to access the show function
39
+ Display d;
40
+ cout<<" Enter a string, integer and float: " ;
41
+ // taking inputs
42
+ cin.getline (str,1000 );
43
+ cin>>n>>a;
44
+ // resizing str length
45
+ realloc (str,strlen (str)+1 );
46
+ // calling Display member function show by passing string
47
+ d.show (str);
48
+ // calling Display member function show by passing integer
49
+ d.show (n);
50
+ // calling Display member function show by passing float
51
+ d.show (a);
52
+ return 0 ;
53
+ }
You can’t perform that action at this time.
0 commit comments