-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamic-alloc.cpp
More file actions
51 lines (48 loc) · 871 Bytes
/
dynamic-alloc.cpp
File metadata and controls
51 lines (48 loc) · 871 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
//dynamic mem alloc using constructor
#include<iostream>
using namespace std;
class student
{
int roll_no;
char *name;
public:
static int i;
student()
{
cout<<"Enter the roll number: ";
cin>>roll_no;
name = new char[30];
cout<<"Enter the name: ";
cin>>name;
i++;
}
student(int rn,char *n)
{
this->roll_no = rn;
this->name = new char[30];
// strcpy(this->name,n);
i++;
}
void display()
{
cout<<"Student details are\n";
cout<<"Roll No: "<<roll_no<<endl;
cout<<"Name: "<<name<<endl;
}
~student()
{
cout<<"Object "<<i--<<"destroyed\n";
delete [] this->name;
name = NULL;
}
};
int student ::i=0;
int main()
{
char name1[]="Rahul", name2[]="Shreedhar";
student s1,s2(2,name1),s3(11,name2);
s1.display();
s2.display();
s3.display();
return 0;
}