-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpractice1.cpp
55 lines (45 loc) · 999 Bytes
/
practice1.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
51
52
53
54
55
#include <iostream>
#include <string>
using namespace std;
/**
*
* Diffrerences between a struct and a class.
*
* 1. Default access for class - private; for struct - public.
* 2. No inheriance in struct.
* 3. Struct basically for user-defined datatype; Class is for encapsulation and OOPS.
*
*/
struct Student
{
string name;
int age;
Student *frnd;
};
int main()
{
Student s1;
Student s2;
Student s3;
s1.name = "Aryan";
s1.age = 25;
s1.frnd = NULL;
s2.name = "Josh";
s2.age = 23;
s2.frnd = &s1;
cout << "Structures" << endl;
cout << s1.name << endl;
cout << s1.age << endl;
if (s1.frnd != NULL)
{
cout << "Friend: " << s1.frnd->name << endl; // error because s1 has NULL friends.
}
else
{
cout << s1.name << " has no friends." << endl;
}
cout << s2.name << endl;
cout << s2.age << endl;
cout << s2.frnd->name << endl; // Josh has one friend - Aryan (s1)
return 1;
}