Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f186b5e

Browse files
Add .cpp file
1 parent 6d3d627 commit f186b5e

File tree

6 files changed

+201
-0
lines changed

6 files changed

+201
-0
lines changed

‎18)OOPS_Constructors.cpp‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
class Employee // Class
5+
{
6+
public:
7+
string name;
8+
int salary;
9+
10+
Employee(string n, int s) // Constructor
11+
{
12+
this->name = n;
13+
this->salary = s;
14+
}
15+
16+
void printDetails() // Method
17+
{
18+
cout << "Name of our first employee is " << this->name << endl;
19+
cout << "Salary of our first employee is " << this->salary << endl;
20+
}
21+
};
22+
23+
int main()
24+
{
25+
Employee employee1("Mooazam", 100000); // Constructor Object
26+
employee1.printDetails();
27+
28+
return 0;
29+
}

‎19)OOPS_AccessSpecifiers.cpp‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
class Employee // Class
5+
{
6+
public:
7+
string name;
8+
int salary;
9+
10+
Employee(string n, int s, int sp) // Constructor
11+
{
12+
this->name = n;
13+
this->salary = s;
14+
this->secretPassword = sp;
15+
}
16+
17+
void printDetails() // Method
18+
{
19+
cout << "ame of our first employee is " << this->name << endl;
20+
cout << "Salary of our first employee is " << this->salary << endl;
21+
}
22+
23+
void getsecretPassword() // Getter and Setter Method
24+
{
25+
cout << "Secret Password is " << this->secretPassword << endl;
26+
}
27+
28+
private:
29+
int secretPassword;
30+
};
31+
32+
int main()
33+
{
34+
Employee employee1("Mooazam", 100000, 123); // Constructor Object
35+
employee1.printDetails();
36+
37+
// cout << "Secret Password is " << employee1.secretPassword << endl;
38+
// Above line can not run because secretPassword is private.
39+
40+
employee1.getsecretPassword();
41+
42+
return 0;
43+
}

‎20)OOPS_Inheritance.cpp‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
class Employee // Class
5+
{
6+
public:
7+
string name;
8+
int salary;
9+
10+
void printDetails() // Method
11+
{
12+
cout << "Name of our first employee is " << this->name << endl;
13+
cout << "Salary of our first employee is " << this->salary << endl;
14+
}
15+
};
16+
17+
class Engineer : public Employee // Child Class
18+
{
19+
public:
20+
string task = "Program Software";
21+
};
22+
23+
int main()
24+
{
25+
Engineer engineer1; // Object of child class
26+
engineer1.name = "Mooazam"; // Setting values in parent class
27+
engineer1.salary = 100000; // Setting values in parent class
28+
engineer1.printDetails(); // Accessing parent class
29+
cout << "Task of our first employee is to " << engineer1.task << endl;
30+
31+
return 0;
32+
}

‎21)OOPS_Encapsulation.cpp‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
class Employee
5+
{
6+
private:
7+
// Private attribute
8+
int salary;
9+
10+
public:
11+
// Setter
12+
void setSalary(int s)
13+
{
14+
salary = s;
15+
}
16+
17+
// Getter
18+
int getSalary()
19+
{
20+
return salary;
21+
}
22+
};
23+
24+
int main()
25+
{
26+
Employee myObj;
27+
myObj.setSalary(100000);
28+
cout << "Salary is " << myObj.getSalary();
29+
return 0;
30+
}

‎22)OOPS_Polymorphism.cpp‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
// Main class
5+
class Animal
6+
{
7+
public:
8+
void animalSound()
9+
{
10+
cout << "The animal makes a sound \n";
11+
}
12+
};
13+
14+
// Child class
15+
class Cat : public Animal
16+
{
17+
public:
18+
void animalSound()
19+
{
20+
cout << "The cat says: meo meo" << endl;
21+
}
22+
};
23+
24+
// Child class
25+
class Dog : public Animal
26+
{
27+
public:
28+
void animalSound()
29+
{
30+
cout << "The dog says: bow wow" << endl;
31+
}
32+
};
33+
34+
int main()
35+
{
36+
Animal myAnimal;
37+
Cat myCat;
38+
Dog myDog;
39+
40+
myAnimal.animalSound();
41+
myCat.animalSound();
42+
myDog.animalSound();
43+
44+
return 0;
45+
}

‎23)tryCatch.cpp‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
try
7+
{
8+
int age = 15;
9+
if (age >= 18)
10+
{
11+
cout << "Access granted - you are old enough.";
12+
}
13+
else
14+
{
15+
throw 505;
16+
}
17+
}
18+
catch (...)
19+
{
20+
cout << "Access denied - You must be at least 18 years old.";
21+
}
22+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /