Skip to content

Commit

Permalink
OOPS in CPP
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishak798 authored Feb 13, 2024
1 parent c453ffd commit ebd2a0f
Show file tree
Hide file tree
Showing 16 changed files with 157 additions and 0 deletions.
26 changes: 26 additions & 0 deletions OOPS/accessspecifire.cpp
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 added OOPS/accessspecifire.exe
Binary file not shown.
15 changes: 15 additions & 0 deletions OOPS/addconstructor.cpp
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 added OOPS/addconstructor.exe
Binary file not shown.
14 changes: 14 additions & 0 deletions OOPS/class.cpp
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 added OOPS/class.exe
Binary file not shown.
19 changes: 19 additions & 0 deletions OOPS/classmethod2.cpp
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 added OOPS/classmethod2.exe
Binary file not shown.
17 changes: 17 additions & 0 deletions OOPS/classmethods.cpp
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 added OOPS/classmethods.exe
Binary file not shown.
16 changes: 16 additions & 0 deletions OOPS/constructor.cpp
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 added OOPS/constructor.exe
Binary file not shown.
25 changes: 25 additions & 0 deletions OOPS/encapsulation.cpp
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 added OOPS/encapsulation.exe
Binary file not shown.
25 changes: 25 additions & 0 deletions OOPS/polymorphism.cpp
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 added OOPS/polymorphism.exe
Binary file not shown.

0 comments on commit ebd2a0f

Please sign in to comment.