-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOopsBasics.cpp
More file actions
123 lines (108 loc) · 3.49 KB
/
OopsBasics.cpp
File metadata and controls
123 lines (108 loc) · 3.49 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include<iostream>
using namespace std;
//basic syntax to define a class
class student{
//you should make a class public if you want to add/access elements out of the class definition, otherwise it will be
//default.
//if you write any data member before public, it will be private by default.
//now for example you have a private attribute in your class
//and you want to make it private then you will define a seperate
//function.
//string name;
public:
string name;
int age;
bool gender;
static int ClassToComplete;//static keyword use
student(){
cout<<"Default Constructor"<<endl;
}//default constructor
student(string s, int a, bool g){
cout<<"Parameterised Constructor"<<endl;
name=s;
age=a;
gender=g;
}//constructor (Parameterised Constructor)
student(student &a){
//there is also a default copy constructor
//but the problem it is shallow copy constructor
//i.e. it does not copy memory locations as well
//that's why we define our own copy constructor
//DEEP COPY CONSTRUCTOR
cout<<"Copy Constructor"<<endl;
name = a.name;
age = a.age;
gender = a.gender;
}//copy constructor
~student(){//Destructor
cout<<"Destructor Called!"<<endl;
}
//here is the function to use a private class:
//this is a set function to set something in the class.
void setName(string s){
name=s;
}
//now to get someting from a private class object
//we will define a get function!
void getName(){
cout<<name<<endl;
}
void printInfo(){
cout<<"Name = ";
cout<<name<<endl;
cout<<"Age = ";
cout<<age<<endl;
cout<<"Gender = ";
cout<<gender<<endl;
}
bool operator == (student &a){
if(name==a.name && age==a.age && gender==a.gender){
return true;
}
return false;
}
};
//do add a terminator after class defining.
int student::ClassToComplete = 5;//Intitializing Static Object
//:: is scope resolution operator
//IT DOES NOT REQUIRED TO CREATE AN OBJECT TO USE A STATIC DATA MEMBER
int main(){
//defining an array of class datatype including objects
// student arr[3];
// for(int i=0;i<3;i++){
// //for example name is a private class attribute
// string s; //to take s as input
// //cin>>s; //taking s input
// //arr[i].setName(s); we access private name using this function.
// cout<<"Name: ";
// cin>>arr[i].name;
// cout<<"Age: ";
// cin>>arr[i].age;
// cout<<"Gender: ";
// cin>>arr[i].gender;
// }
// for(int i=0;i<3;i++){
// arr[i].printInfo();
// }
/* student a;
a.name = 'Ayush';
a.age = 18;
a.gender = 0; */
//alternative method to define a object in a class
//WHAT IF WE WANT TO ADD A OBJECT NAME AS A STUDENT NAME?
//WE WILL USE A CONSTRUCTOR
student a("Ayush",18,0);
//a.getName();
//a.printInfo();
student b;//as b does not exist it will return default constructor
student c = a;//copy constructor
cout<<student::ClassToComplete<<endl;//static member call - best method
//A static function can only access static member simple!
if(c==a){
cout<<"Same"<<endl;
}
else{
cout<<"notSame"<<endl;
}
return 0;
}