-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSession9B.cpp
More file actions
74 lines (54 loc) · 1.39 KB
/
Session9B.cpp
File metadata and controls
74 lines (54 loc) · 1.39 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<iostream>
#include<string>
using namespace std;
class Student{
// non static -> Property of Object
int roll;
string name;
public:
// static -> Property of Class
static string schoolName;
// Declaring Methods and not defining them
Student();
void setStudent(int r, string n);
void showStudent();
static void showSchoolName(){
cout<<"SchoolName: "<<schoolName<<"\n";
}
};
// Declare varibale outside if they are static
string Student::schoolName;
// Lets define them outside the class
// :: Scope Resolution Operator
Student::Student(){
roll = 0;
name = "NA";
}
void Student::setStudent(int r, string n){
roll = r;
name = n;
}
// Property of Object ? -> non static
// Property of Object can access Property of Class
void Student::showStudent(){
cout<<roll<<" belongs to "<<name<<"\n";
cout<<name<<" studies in "<<schoolName<<"\n";
}
int main(int argc, char const *argv[]){
Student::schoolName = "ABC International";
// Compile Time or Static Memory Management
// Object Constructed in a static way !!
Student s1;
// Object can also access property of class
s1.schoolName = "XYZ International";
s1.setStudent(101,"John");
// Run Time or Dynamic Memory Management
// Object Constructed in a dynamic way !!
Student *s2 = new Student();
s2->setStudent(201,"Jennie");
s1.showStudent();
s2->showStudent();
Student::showSchoolName();
s1.showSchoolName();
return 0;
}