Posts

Showing posts from July, 2020

Multiple, Multilevel, Hybrid and Hierarchical inheritance

Image
Multiple inheritance:                          Multiple inheritance is a type of inheritance in c++ in which a class can be inherited from more than one classes. In this case the inherited class has more than one parent class. Multiple inheritance example:         #include <iostream> using namespace   std; class Animal { public:    Animal(){ cout<<" Animal's constructor "<<endl;} }; class Mammal { public:    Mammal(){cout<<" Mammal's constructor "<<endl;} }; class Bat : public Animal,public Mammal { public:   Bat(){ cout<<" Bat's constructor "<<endl;} }; int main(){     Bat bat1;    return 0; } Output: Animal's constructor Mammal's constructor Bat's constructor Multilevel inheritance:            In multilevel inheritance a class can be derived from another class who is already def...

Friend function part 1 | very very easy

Friend function: Friend function is not a member function of a class to which it is a friend. Friend function is declared inside the class with friend keyword. It must be defined outside the class to which it is friend. Friend function can access any member of the class to which it is a friend. Friend function can not access the members of the class directly. Friend function can not access members of the class directly because it has no caller object. It should not be defined with membership label. Let's consider a simple example to implement all above points: #include<iostream> using namespace std;        class Complex{     private:            int a, b;     public:            void setData(int x, int y){            a=x;            b=y; }            void showData(){         ...

Practice problem classes and objects.

        Creating classes and objects in C++ Problem:        Create a class named ClassRoom  that has 6 private  member variables named t otalChairs, totalTables, classGrade, teacherName, totalStudents and  classRep. Your class must have 2  public member functions named as input and  output. These two input and output functions will work as getter and setter. You must also create a constructor that initializes the values of all the private member variables to null or 0. Remember: We can use any name for our getter and setter function. Now for our main method create an object of our class ClassRoom named Class1. Input values from the user by calling the Input function of the object Class1. Then output all the values. All the above conditions must be fulfilled. Visit here for reference about classes and objects: C++ classes and objects

Polymorphism Part 1 C++

Image
Polymorphism:       Poly means many and morph means forms. So polymorphism means having many forms. Implementation: Polymorphism can be implemented by: Function overriding Operator overloading Virtual function    Polymorphism implementation by Function overriding: What is function overriding? Function overriding is a feature that allows us to have the same function in child class which is already present in the base class. As we know that a child class inherits the data members and functions of the base class, but when we want to override a functionality of a function of base class in child class we can use function overriding. For example:     Consider you make a base class named Person then you make a function of the class Person named Profession. As every child class of Person class that you make will have different profession.  #include<iostream> using namespace std; class Person{ public: void profession(){ //base class ...