blob: 954b3bf816c4da7c75f2d15502c567314338889e (
plain) (
blame)
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
|
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
struct Student
{
char* name;
int age;
Student(const char* name, int age)
{
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
this->age = age;
}
void print() const
{
std::cout << "Name: " << name << ", age: " << age << std::endl;
}
~Student()
{
delete[] name;
}
};
int main()
{
Student s("Ivan", 33);
Student* ptr = new Student("Petur", 17);
s.print();
ptr->print();
delete ptr; //calls destructor of ptr
}//calls destructor of s
|