Skip to content

Commit 38db388

Browse files
authored
Initialize
1 parent 23ab7f3 commit 38db388

10 files changed

+213
-0
lines changed

tutorial19.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
/*
5+
1. We can define a function in the sub class having same return type, same name, same arguments as that of one in the base class.
6+
2. In this case the method in the base class will get overridden by the method in the subclass.
7+
3. To call the overridden base class method we can use scope resolution operator.
8+
4. We can aslo call the base class method inside the sub class method using svoe resolution operator.
9+
5. We can also call the base calss method by receiving the argument of type base class.
10+
*/
11+
12+
class Human
13+
{
14+
public:
15+
void Introduce()
16+
{
17+
cout<<"Hello! I am a Human."<<endl;
18+
}
19+
};
20+
21+
class Student : public Human
22+
{
23+
public:
24+
void Introduce()
25+
{
26+
cout<<"Hello! I am a Student."<<endl;
27+
Human :: Introduce(); // calling base class method from inside the class method.
28+
}
29+
};
30+
31+
void WhoseThis(Human h) // here we are receiving saurav as of type Human instead of Person
32+
{
33+
h.Introduce(); // since here saurav is of type human, Introduce of Human will get called
34+
}
35+
36+
int main()
37+
{
38+
Student saurav;
39+
saurav.Introduce();
40+
saurav.Human :: Introduce(); // calling base class overridden method.
41+
WhoseThis(saurav);
42+
}

tutorial19.exe

1.86 MB
Binary file not shown.

tutorial20.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
/*
5+
1. this pointer - this pointer holds the address of the current object when we call any (non - static) member function for any object.
6+
2. this pointer is available locally inside the function.
7+
3. this pointer is not available for static member functions and in friend functions.
8+
4. it gets implicitally used inside the member function but we can also use it explicitally if needed.
9+
*/
10+
11+
class Human
12+
{
13+
int age;
14+
public:
15+
void setAge(int age) // local variable name is same as that of data member name
16+
{
17+
this->age = age; // or *(this).age = age;
18+
}
19+
void showAge()
20+
{
21+
cout<<"My age is "<<age<<"."<<endl;
22+
}
23+
};
24+
25+
int main()
26+
{
27+
Human saurav;
28+
saurav.setAge(22);
29+
saurav.showAge();
30+
}

tutorial20.exe

1.86 MB
Binary file not shown.

tutorial21.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
/*
5+
POLYMORPHISM - many forms
6+
1. Polymorphism can be classified into two categories : Compile Time and Run Time.
7+
2. Compile Time Polymorphism can be achieved using Function Overloading and Operator Overloading.
8+
3. Run Time Polymorphism is achieved using virtual functions.
9+
*/
10+
11+
// Compile time polymorphism - Function Overloading
12+
13+
class Number
14+
{
15+
public:
16+
void num()
17+
{
18+
cout<<"No number obtained."<<endl;
19+
}
20+
void num(int x)
21+
{
22+
cout<<"Number obtained is "<<x<<"."<<endl;
23+
}
24+
void num(int x, int y)
25+
{
26+
cout<<"Numbers obtained are "<<x<<" and "<<y<<"."<<endl;
27+
}
28+
};
29+
30+
int main()
31+
{
32+
Number n;
33+
n.num();
34+
n.num(34);
35+
n.num(12, 24);
36+
}

tutorial21.exe

1.86 MB
Binary file not shown.

tutorial22.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
/*
4+
POLYMORPHISM - many forms
5+
1. Polymorphism can be classified into two categories : Compile Time and Run Time.
6+
2. Compile Time Polymorphism can be achieved using Function Overloading and Operator Overloading.
7+
3. Run Time Polymorphism is achieved using virtual functions.
8+
*/
9+
10+
// Run time polymorphism - Virtual Functions
11+
class Human
12+
{
13+
public:
14+
virtual void speak()
15+
{
16+
cout<<"Human is speaking."<<endl;
17+
}
18+
void sing()
19+
{
20+
cout<<"Human is singing."<<endl;
21+
}
22+
};
23+
24+
class Student : public Human
25+
{
26+
public:
27+
void speak()
28+
{
29+
cout<<"Student is speaking."<<endl;
30+
}
31+
void sing()
32+
{
33+
cout<<"Student is singing."<<endl;
34+
}
35+
};
36+
37+
void LetsSpeak(Human &h) // Do not forget to receive as reference
38+
{
39+
h.speak();
40+
}
41+
42+
void LetsSing(Human &h) // Do not forget to receive as reference
43+
{
44+
h.sing();
45+
}
46+
47+
int main()
48+
{
49+
Student saurav;
50+
LetsSpeak(saurav);
51+
LetsSing(saurav);
52+
53+
Human nishant;
54+
LetsSpeak(nishant);
55+
LetsSing(nishant);
56+
}

tutorial22.exe

1.86 MB
Binary file not shown.

tutorial23.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
/*
4+
Run time polymorphism - Virtual Functions
5+
6+
note - Even if we inherit a class that has inherited the class having a virtual function. The virtual nature gets inherited too.
7+
8+
*/
9+
class Human
10+
{
11+
public:
12+
virtual void speak()
13+
{
14+
cout<<"Human is speaking."<<endl;
15+
}
16+
};
17+
18+
class Student : public Human
19+
{
20+
public:
21+
void speak()
22+
{
23+
cout<<"Student is speaking."<<endl;
24+
}
25+
};
26+
27+
class ChemicalStudent : public Student
28+
{
29+
public:
30+
void speak()
31+
{
32+
cout<<"Chemical Student is speaking."<<endl;
33+
}
34+
};
35+
36+
void LetsSpeak(Human &h) // Do not forget to receive as reference
37+
{
38+
h.speak();
39+
}
40+
41+
int main()
42+
{
43+
Human saurav;
44+
Student ravi;
45+
ChemicalStudent nishant;
46+
LetsSpeak(saurav);
47+
LetsSpeak(ravi);
48+
LetsSpeak(nishant);
49+
}

tutorial23.exe

1.86 MB
Binary file not shown.

0 commit comments

Comments
 (0)