-
Notifications
You must be signed in to change notification settings - Fork 0
/
WhyCpp2.cpp
50 lines (38 loc) · 1 KB
/
WhyCpp2.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
#include<stdio.h>
#include<string.h>
struct CPP
{
int version;
char *p;
};
void method( struct CPP *this1 )
{
// methods will not have owners to any memory variables
// methods will have address of owner variables -version 2. pointer p
// methods have pointer-this1 that has ownership
this1->version =10; // intalising 10 to an address
this1->p= new char[10];
strcpy( this1->p ,"hapy coders");
}
int method1( struct CPP *this1 )
{
return this1->version; //copy data of variable to another memory
}
void method2( struct CPP *this1 )
{
printf(" %d ",this1->version ); // copy data to function printf
}
void method3( struct CPP *this1 )
{
delete this1->p;
}
int main()
{
struct CPP venkatesh;
// venkatesh is allocating memory for owner variable - version ,- p
method(&venkatesh ); //purpose : intalise
method1(&venkatesh ); // purpose :return a value
method2(&venkatesh ); // purpose :to display value
method3(&venkatesh); // purpose : deallocate
return 0;
}