Skip to main content

Posts

C++ Code For Function Overloading

Example 1: Function Overloading by Number of Arguments A C++ code having a Calculator class with function overloading (multiple add methods). Here’s the full working program: # include < iostream > using namespace std ; // Calculator class demonstrating Function Overloading class Calculator { public : // Function to add two integers int add ( int a , int b ) { return a + b ; } // Function to add three integers int add ( int a , int b , int c ) { return a + b + c ; } // Function to add two doubles double add ( double a , double b ) { return a + b ; } } ; int main ( ) { Calculator calc ; cout < < " Sum of 2 and 3 (int): " < < calc . add ( 2 , 3 ) < < endl ; cout < < " Sum of 2, 3 and 5 (int): " < < calc . add ( 2 , 3 , 5 ) < < endl ; cout < < " Sum of 2.5 and 3.5 (doubl...

C++ code for Hybrid Inheritance

Here’s a C++ program that shows how a Research Scholar can inherit from both Student and Teacher, and how virtual inheritance solves the diamond problem: # include < iostream > using namespace std ; // Base class class Person { public : void introduce ( ) { cout < < " I am a person. " < < endl ; } } ; // Derived class Student (virtual inheritance) class Student : virtual public Person { public : void study ( ) { cout < < " I am studying. " < < endl ; } } ; // Derived class Teacher (virtual inheritance) class Teacher : virtual public Person { public : void teach ( ) { cout < < " I am teaching. " < < endl ; } } ; // Derived class ResearchScholar (inherits from both Student and Teacher) class ResearchScholar : public Student , public Teacher { public : void research ( ) { cout < < ...

C++ code for Multiple Inheritance.

Here’s a C++ program that shows how a Multiple Inheritance actually works with the help of Perfomer,Singer and Dancer # include < iostream > using namespace std ; // Base class 1 class Singer { public : void sing ( ) { cout < < " Singing... " < < endl ; } } ; // Base class 2 class Dancer { public : void dance ( ) { cout < < " Dancing... " < < endl ; } } ; // Derived class (inherits from both Singer and Dancer) class Performer : public Singer , public Dancer { } ; int main ( ) { Performer p ; p . sing ( ) ; // Inherited from Singer p . dance ( ) ; // Inherited from Dancer return 0 ; } Output:        Singing...        Dancing... Explanation: Singer and Dancer are two independent base classes. Performer inherits from both, using multiple inheritance . This means Performer automatically gets the ability to ...

C++ code for Hierarchical Inheritance

Here’s a C++ program that shows how a Hierarchical Inheritance actually works with the help of Person,Teacher,and Student # include < iostream > using namespace std ; // Base class class Person { public : void walk ( ) { cout < < " Walking... " < < endl ; } } ; // Derived class 1 class Student : public Person { public : void study ( ) { cout < < " Studying... " < < endl ; } } ; // Derived class 2 class Teacher : public Person { public : void teach ( ) { cout < < " Teaching... " < < endl ; } } ; // Main function int main ( ) { Student s1 ; Teacher t1 ; // Student object can use both Person and Student methods s1 . walk ( ) ; s1 . study ( ) ; // Teacher object can use both Person and Teacher methods t1 . walk ( ) ; t1 . teach ( ) ; return 0 ; } Output:      Walking......

C++ code for Multi Level Inheritance

Here’s a C++ program that shows how a MultiLevel Inheritance actually works with the help of Electronic Device, Phone, and Smartphone # include < iostream > using namespace std ; // Base class class ElectronicDevice { public : void powerOn ( ) { cout < < " Device ON " < < endl ; } } ; // Derived class (inherits from ElectronicDevice) class Phone : public ElectronicDevice { public : void call ( ) { cout < < " Making a call " < < endl ; } } ; // Further derived class (inherits from Phone) class Smartphone : public Phone { public : void browse ( ) { cout < < " Browsing internet " < < endl ; } } ; // Main function int main ( ) { Smartphone myPhone ; // Accessing methods from all levels myPhone . powerOn ( ) ; // From ElectronicDevice myPhone . call ( ) ; // From Phone myPhone . browse ...

C++ code for Single Inheritance

Here’s a C++ program that shows how a Single Inheritance actually works with the help of Vehicle and Car class # include < iostream > using namespace std ; // Base class (Parent) class Vehicle { public : void start ( ) { cout < < " Vehicle started " < < endl ; } } ; // Derived class (Child) class Car : public Vehicle { public : void playMusic ( ) { cout < < " Music is playing " < < endl ; } } ; // Main function int main ( ) { Car myCar ; // Accessing parent class method myCar . start ( ) ; // Accessing child class method myCar . playMusic ( ) ; return 0 ; } Output:           Vehicle started           Music is playing Short Explanation: Vehicle is the base (parent) class . It has a method start() that prints "Vehicle started" . Car is the derived (child) ...

All About Inheritance in OOP: Explained with Real-Life Examples

  The first thing that comes to mind when  you hear the word  inheritance,  is passing something down from parents to children . In Object-Oriented Programming, it works in a very similar way—it allows one class (child class) to inherit properties and behaviors from another class (parent class). This concept makes code reusable, organized, and easier to maintain . Let’s explore inheritance step by step with real-time analogies to make it super simple. What is Inheritance in OOP? In OOP, inheritance is a concept in which a class inherits the properties (variables) and behaviors (methods) of other classes. Common features are defined in the parent class . These features are extended by the child class which is capable of adding its own features.. It is best to consider it as a family tree : a child takes after a parent in terms of eye color or height but can also possess an individual characteristic. Why Use Inheritance? Code Reusability – No need t...