Skip to content

Commit

Permalink
Merge pull request #32 from gaurovgiri/feat/inheritance
Browse files Browse the repository at this point in the history
add solution to single inheritance
  • Loading branch information
aayush105 authored Nov 15, 2023
2 parents e286556 + e49bd7d commit 0b494b3
Show file tree
Hide file tree
Showing 8 changed files with 277 additions and 0 deletions.
8 changes: 8 additions & 0 deletions programs/Inheritance/Single Inheritance/q1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,11 @@ int main()
B b;
return 0;
}

/*
Output:
Default constructor of class A
Default constructor of class B
*/
33 changes: 33 additions & 0 deletions programs/Inheritance/Single Inheritance/q2.cpp
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;
}
59 changes: 59 additions & 0 deletions programs/Inheritance/Single Inheritance/q3.cpp
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
*/
37 changes: 37 additions & 0 deletions programs/Inheritance/Single Inheritance/q4.cpp
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
*/
36 changes: 36 additions & 0 deletions programs/Inheritance/Single Inheritance/q5.cpp
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
*/
41 changes: 41 additions & 0 deletions programs/Inheritance/Single Inheritance/q6.cpp
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.
*/
30 changes: 30 additions & 0 deletions programs/Inheritance/Single Inheritance/q7.cpp
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;
}
33 changes: 33 additions & 0 deletions programs/Inheritance/Single Inheritance/q8.cpp
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;
}

0 comments on commit 0b494b3

Please sign in to comment.