Posts

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 ...

Problem 1 Inheritance OOP

Image
Problem:                   You have to make three classes: Person class Student class Employee class Criteria:                 Person class is the parent class or base class. Student class is derived or child of Person class and Employee class is also derived or child of Person class.          Member variables and methods/functions: Person class:                   Person class must have 3 private member variables  Name CNIC Address                And two public member functions: get set   To get and set values of member variables. Student class:                Student class must have 4 private member variables: RollNo Sub1 Sub2 Sub3               Three public member functions/methods: get1 set1  calcG...

Inheritance in C++ part 1

Image
Inheritance: A programming technique that is used to reuse an existing class to create a new class is called inheritance . The new class inherits all the behaviour of the original class. The existing class that is raised to create a new class is called super class, base class or parent class. The new class that inherits the properties and functions of the existing class is known as subclass, derived class or child class. The basic principle of inheritance is that each child class shares common properties with the class from which it is derived. The child class inherits all properties of the parent class and can add its own capabilities. Example: Suppose we have a class named Vehicle. The derived classes of this class may share similar properties such as wheels and motor etc. Additionally, derived classes may have their own particular characteristics. For example, a derived class bus may have seats for people but, another derived class truck may have space to carry goods. Advantages of ...

Abstraction and Encapsulation

Image
Encapsulation: Encapsulation is a process of combining data members and functions in a single unit called class. This is to prevent the access to the data directly. To get access to the data member functions of the class are used. It helps in data hiding. It is one of the popular features of object oriented programming.   How to achieve Encapsulation in a class: For this purpose you have to make all the data members private. Then create public getter and setter methods in such a way that the way function set the value of the data member and the hat function get the value of the data member. Abstraction:    Abstraction is similar to the concept of a black box. Input goes in, black box does something and output comes out. It doesn't matter what happens in the black box, all you have to know is that it works.    It means displaying only essential information and hiding the background details or implementation. Real world example of abstraction:     ...