-
Notifications
You must be signed in to change notification settings - Fork 549
/
Copy path12_01.cpp
50 lines (41 loc) · 1013 Bytes
/
12_01.cpp
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
#include <bits/stdc++.h>
using namespace std;
class Person {
public:
void print() { cout << "Person\n"; }
~Person() { cout << "~Person\n";}
};
class Student: public Person {
public:
void print() { cout << "Student\n"; }
~Student() { cout << "~Student\n";}
};
int main() {
Person person; // first object
Student stud; // 2nd object
person.print();
stud.print();
// No object construct/deconstruct for the refrence
Person* per_ptr = &person;
Student* stud_ptr1 = &stud;
Person* stud_ptr2 = &stud;
Person* stud_ptr3 = new Student(); // 3rd object
per_ptr->print();
stud_ptr1->print();
stud_ptr2->print(); // Call Person not Student
stud_ptr3->print(); // Call Person not Student
delete stud_ptr3; // Memory leak
return 0;
}
/*
Person
Student
Person
Student
Person
Person
~Person from 3rd object: memory leak: student is not cleaned
~Student from 2nd object: derived
~Person from 2nd object: base
~Person from first object
*/