-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from gaurovgiri/feat/inheritance
add solution to single inheritance
- Loading branch information
Showing
8 changed files
with
277 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 |
---|---|---|
|
@@ -26,3 +26,11 @@ int main() | |
B b; | ||
return 0; | ||
} | ||
|
||
/* | ||
Output: | ||
Default constructor of class A | ||
Default constructor of class B | ||
*/ |
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,33 @@ | ||
// WAP to show single inheritance with main function access. | ||
|
||
#include <iostream> | ||
using namespace std; | ||
|
||
// Base class | ||
class Base | ||
{ | ||
public: | ||
void display() | ||
{ | ||
cout << "This is the base class." << endl; | ||
} | ||
}; | ||
|
||
// Derived class | ||
class Derived : public Base | ||
{ | ||
public: | ||
void show() | ||
{ | ||
cout << "This is the derived class." << endl; | ||
} | ||
}; | ||
|
||
// Main function | ||
int main() | ||
{ | ||
Derived d; | ||
d.display(); // Accessing base class function from derived class object | ||
d.show(); // Accessing derived class function from derived class object | ||
return 0; | ||
} |
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,59 @@ | ||
// WAP to show single inheritance parameterize constructor. | ||
|
||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
class Father | ||
{ | ||
private: | ||
string surname; | ||
|
||
public: | ||
Father(string surname) | ||
{ | ||
this->surname = surname; | ||
cout << "Father surname set" << endl; | ||
} | ||
void displayName() | ||
{ | ||
cout << "Father class surname is: " << surname << endl; | ||
} | ||
}; | ||
|
||
class Child : public Father | ||
{ | ||
private: | ||
string surname; | ||
|
||
public: | ||
Child(string surname) | ||
: Father(surname) | ||
{ | ||
this->surname = surname; | ||
cout << "Child surname set" << endl; | ||
} | ||
void showName() | ||
{ | ||
cout << "Child class surname is " << surname << endl; | ||
} | ||
}; | ||
|
||
int main() | ||
{ | ||
Child child("Giri"); | ||
child.displayName(); // displays father surname | ||
child.showName(); // displays child surname | ||
|
||
return 0; | ||
} | ||
|
||
/* | ||
Output: | ||
Father surname set | ||
Child surname set | ||
Father class surname is: Giri | ||
Child class surname is Giri | ||
*/ |
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,37 @@ | ||
#include <iostream> | ||
using namespace std; | ||
|
||
class Base | ||
{ | ||
protected: | ||
int x; | ||
|
||
public: | ||
Base(int a) | ||
{ | ||
x = a; | ||
} | ||
}; | ||
|
||
class Derived : public Base | ||
{ | ||
public: | ||
Derived(int a, int b) : Base(a) | ||
{ | ||
cout << "Base class parameter: " << x << endl; | ||
cout << "Derived class parameter: " << b << endl; | ||
} | ||
}; | ||
|
||
int main() | ||
{ | ||
Derived d(10, 20); | ||
return 0; | ||
} | ||
|
||
/* | ||
Base class parameter: 10 | ||
Derived class parameter: 20 | ||
*/ |
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,36 @@ | ||
// WAP to show single inheritance with same function name of base and derived class. | ||
|
||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
class Dinosaurous | ||
{ | ||
public: | ||
void speak() | ||
{ | ||
cout << "Raaarrrr" << endl; | ||
} | ||
}; | ||
|
||
class Chicken : public Dinosaurous | ||
{ | ||
public: | ||
void speak() | ||
{ | ||
cout << "Pakkak" << endl; | ||
} | ||
}; | ||
|
||
int main() | ||
{ | ||
Chicken c; | ||
c.speak(); | ||
} | ||
|
||
/* | ||
Output: | ||
Pakkak | ||
*/ |
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,41 @@ | ||
// WAP to show parent function call inside child class. | ||
|
||
#include <iostream> | ||
using namespace std; | ||
|
||
// Base class | ||
class Parent | ||
{ | ||
public: | ||
void display() | ||
{ | ||
cout << "This is the parent class." << endl; | ||
} | ||
}; | ||
|
||
// Derived class | ||
class Child : public Parent | ||
{ | ||
public: | ||
void show() | ||
{ | ||
cout << "This is the child class." << endl; | ||
display(); // Calling parent class function from child class | ||
} | ||
}; | ||
|
||
// Main function | ||
int main() | ||
{ | ||
Child c; | ||
c.show(); // Accessing derived class function from derived class object | ||
return 0; | ||
} | ||
|
||
/* | ||
Output: | ||
This is the child class. | ||
This is the parent class. | ||
*/ |
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,30 @@ | ||
#include <iostream> | ||
using namespace std; | ||
|
||
class Base | ||
{ | ||
protected: | ||
int x; | ||
|
||
public: | ||
Base() | ||
{ | ||
x = 10; | ||
} | ||
}; | ||
|
||
class Derived : public Base | ||
{ | ||
public: | ||
void display() | ||
{ | ||
cout << "The value of x is: " << x << endl; | ||
} | ||
}; | ||
|
||
int main() | ||
{ | ||
Derived d; | ||
d.display(); // but d.x cannot be accessed. | ||
return 0; | ||
} |
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,33 @@ | ||
// WAP to show parent function call inside child class having same member function name. | ||
|
||
#include <iostream> | ||
using namespace std; | ||
|
||
// Base class | ||
class Parent | ||
{ | ||
public: | ||
void display() | ||
{ | ||
cout << "This is the parent class." << endl; | ||
} | ||
}; | ||
|
||
// Derived class | ||
class Child : public Parent | ||
{ | ||
public: | ||
void display() | ||
{ | ||
cout << "This is the child class." << endl; | ||
Parent::display(); // Calling parent class function from child class | ||
} | ||
}; | ||
|
||
// Main function | ||
int main() | ||
{ | ||
Child c; | ||
c.display(); // Accessing derived class function from derived class object | ||
return 0; | ||
} |