-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c453ffd
commit ebd2a0f
Showing
16 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
class myClass | ||
{ | ||
private: | ||
int y = 10; // declared as inaccessible | ||
public: | ||
int x = 5; | ||
myClass() // constructor having same name as the class | ||
{ | ||
cout << "hello world!"; | ||
} | ||
}; | ||
int main() | ||
{ | ||
myClass myObj; | ||
cout << myObj.x; | ||
// cout << myObj.y; | ||
return 0; | ||
} | ||
/*cessspecifire.cpp:18:19: error: 'int myClass::y' is private within this context | ||
cout << myObj.y; | ||
^ | ||
accessspecifire.cpp:6:13: note: declared private here | ||
int y = 10; // declared as inaccessible | ||
^~*/ |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
class myAddition | ||
{ | ||
public: | ||
myAddition(int x, int y) // constructor having same name as the class | ||
{ | ||
cout << "Sum: " << x + y; | ||
} | ||
}; | ||
int main() | ||
{ | ||
myAddition myObj(4, 5); | ||
return 0; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/*Classes in Cpp*/ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
class myClass | ||
{ | ||
public: | ||
string s = "hello"; | ||
}; | ||
int main() | ||
{ | ||
myClass myObj; | ||
cout << myObj.s; | ||
return 0; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/*class methods inside class defination*/ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
class myClass | ||
{ | ||
public: | ||
void myMethod(); | ||
}; | ||
void myClass ::myMethod() | ||
{ | ||
cout << "hello world from outside the class!"; | ||
} | ||
|
||
int main() | ||
{ | ||
myClass myObj; | ||
myObj.myMethod(); | ||
return 0; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/*class methods inside class defination*/ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
class myClass | ||
{ | ||
public: | ||
void myMethod() | ||
{ | ||
cout << "hello world!"; | ||
} | ||
}; | ||
int main() | ||
{ | ||
myClass myObj; | ||
myObj.myMethod(); | ||
return 0; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
class myClass | ||
{ | ||
public: | ||
myClass() // constructor having same name as the class | ||
{ | ||
cout << "hello world, I am a constructor!"; | ||
} | ||
}; | ||
int main() | ||
{ | ||
myClass myObj; // constructor call itself | ||
|
||
return 0; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
class Employee | ||
{ | ||
private: | ||
int salary; | ||
// through encapsulation we can access private data members | ||
public: | ||
// setter method to set salaray | ||
void setsalary(int s) | ||
{ | ||
salary = s; | ||
} | ||
int getsalary() | ||
{ | ||
return salary; | ||
} | ||
}; | ||
int main() | ||
{ | ||
Employee empObj; | ||
empObj.setsalary(50000); | ||
cout << empObj.getsalary(); | ||
return 0; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
class Animal | ||
{ | ||
public: | ||
void animalsound() | ||
{ | ||
cout << "this animal makes sound...."; | ||
} | ||
}; | ||
class Monkey : public Animal | ||
{ | ||
public: | ||
void animalsound() | ||
{ | ||
cout << "monkey makes sound...."; | ||
} | ||
}; | ||
int main() | ||
{ | ||
Monkey myObj; | ||
myObj.animalsound(); | ||
myObj.Animal::animalsound(); // Call animalsound() from Animal class | ||
return 0; | ||
} |
Binary file not shown.