karakib2k18 / Data-Structures-and-Algorithms-in-Cpp

1 stars 2 forks source link

Lecture 22 : OOPS 3 #9

Open karakib2k18 opened 1 year ago

karakib2k18 commented 1 year ago

Lecture 22 : OOPS 3  START HERE

Lecture 22 : OOPS 3


//============>>>>>>>>>> Lecture 22 : OOPS 3 NOTES

=>Lecture 22 : OOPS 3 Exception Handling Notes Lecture 22 : OOPS 3 Exception Handling Notes.pdf

=>Lecture 22 : OOPS 3 Notes Lecture 22 : OOPS 3 Notes.pdf


Abstraction & Encapsulation

image

Inheritance : Introduction

image

Inheritance : Syntax

// base class
class Animal {

  private :
      int maxSpeed;

  protected :
      int numTyres;

  public :
      string color;
};

class Dog : public Animal {
// all things will be same;
};

class Dog : protected Animal {
// public will be protected also;
};

class Dog : private Animal {
// all will be private;
};

Inheritance : Order of Constructor/Destructor cal

#include <bits/stdc++.h>
using namespace std;

class Vehi{
private:
  int maxSpeed;

protected:
  int numT;

public:
  string color;

  // Vehi(){
  //   cout<<"its vehi 1constructor"<<endl;
  // }

  Vehi(int x, int y){
    cout<< x <<endl;
    cout<< y<<endl;
    maxSpeed = x;
  }
  ~Vehi(){
    cout<<"its vehi distructor"<<endl;
  }
};

class Car: public Vehi {
public:
  int nG;

  Car(int x, int y) : Vehi(x,y){
    cout<< "car constructor"<<endl;
  }
  ~Car(){
    cout<< "car distructor"<<endl;
  }
};

class HCar: public Car {
public:

  HCar(int x,int y) : Car(x,y){
    cout<< "H Hcar constructor"<<endl;
  }
  ~HCar(){
    cout<< "H Hcar1 distructor"<<endl;
  }
};

int main()
{
  // // Vehi v;
  // // v.color="red";

  // Car c(50);
  // // c.color="blue";
  // // c.nG=5;
  HCar hc(10,20);
}

Inheritance : Types

image

#include <iostream>
using namespace std;

class Student {
  public :
    string name;

    void print() {
      cout << "Student " << endl;
    }
};

class Teacher {
  public : 
    string name;
    string age;

    void print() {
      cout << "Teacher" << endl;
    }
};

class TA : public Teacher, public Student {
  public :
    void print() {
      cout << "TA " << endl;
    }
};

int main() {
  TA a;

 // a.Student :: print();
 // a.Teacher :: print();

  a.print();

  //===========>>>> a. className :: property name = value;
  a.Teacher :: name = "abcd"; //a.name = "abc" won't work. bcz name was declare in Student and teacher. so I have to specifiy which name I'm calling.  for this, a.Teacher :: name OR a.Student :: name !

}

Hybrid Inheritance

image

// jekhane age print pabe oitai print korbe. own class a print function thakle r parent er print function call korbe nah. jekhanei age print function pabe oi khanei age print kore dibe. r uporer print a jabe nah.

#include <iostream>
using namespace std;

class Vehicle {
  private :
    int maxSpeed;

  protected :
    int numTyres;

  public :
    string color;

  /*
    Vehicle() {
    cout << "Vehicle's default constructor " << endl;
  }*/

  void print() {
    cout << "Vehicle" << endl;
  }

  Vehicle(int z) {
  cout << "Vehicle's Parametrized constructor " << z << endl;
    maxSpeed = z;
  }

  ~Vehicle() {
    cout << "Vehicle's Destructor " << endl;
  }

};

class Car : virtual public Vehicle {
  public :
    int numGears;

    Car() : Vehicle(3){
      cout << "Car's default constructor " << endl;
    }

    /*
    Car(int x, int y) : Vehicle(x) {
      cout << "Car's constructor " << endl;
      numGears = y;
    }*/

    ~Car() {
      cout << "Car's Destructor " << endl;
    }

    void print() {
      cout << "NumTyres : " << numTyres << endl;
      cout << "Color : " << color << endl;
      cout << "Num gears : " << numGears << endl;
      //    cout << "Max Speed : " << maxSpeed << endl;
    }

};

class Truck : virtual public Vehicle {
  public :

    Truck() : Vehicle(4) {
      cout << "Truck's constructor " << endl;
    }

    ~Truck() {
      cout << "Truck's Destructor " << endl;
    }
};

class Bus : public Car, public Truck {
  public :

    Bus() : Vehicle(5) {
      cout << "Bus's constructor " << endl;
    }

    // void print() {
    //   cout << "BUs" << endl;
    // }
    ~Bus() {
      cout << "Bus's Destructor " << endl;
    }
};

int main() {
  Bus b;

  b.print();

  // b.Car :: print();
  // b.Truck :: print();

}
// OUTPUT
Vehicle's Parametrized constructor 5
Car's default constructor 
Truck's constructor 
Bus's constructor 
NumTyres : 21989
Color : 
Num gears : 65535
Bus's Destructor 
Truck's Destructor 
Car's Destructor 
Vehicle's Destructor 

Polymorphism : Compile time

image

#include <iostream>
using namespace std;
class Vehicle {
  public :
    string color;

  void print() {
    cout << "Vehicle" << endl;
  }
};

class Car : public Vehicle {
  public :
    int numGears;

    void print() {
      cout << "Car" << endl;
    }
};

int main() {
  Vehicle v;
  Car c;

  v.print();
  c.print();

  Vehicle *v1 = new Vehicle;
  Vehicle *v2;
  // v2 = &v;
  v2 = &c;
  v1 -> print();
  v2 -> print(); // compile time Polymorphism

/*----------------------------------------*/
//funciton overloading => differnet funciton with same name
int test(int a, int b) {

}

int test(int a) {

}

int test() {

}
/*----------------------------------------*/
}

Polymorphism : Run time

image

#include <iostream>
using namespace std;

class Vehicle {
  public :
    string color;

// vehicle class dia Car class ke point korleo 
//vehicle er print function call hoye jabe. 
//tai Virtual add korle ami Car class er print function call korte parbo.
// but Car a kono print na thakle virtual Print call hobe. 
  virtual void print() { 
    cout << "Vehicle" << endl;
  }
};

class Car : public Vehicle {
  public :
    int numGears;

    void print() {
      cout << "Car" << endl;
    }
};

int main() {

  Vehicle *v1 = new Vehicle;
  v1 -> print();

  // if no Print function in Car class then It will 
  //go to the Vehical class print funciton.
  Car c; 
  Vehicle *v2;
  v2 = &c;
  v2 -> print();
}
//OUTPUT
Vehicle
Car

Virtual functions & Abstract classes

Pure function thaklei sheta Abstract class hoye jay.  Pure function create kore Child class ke force kortesi oi function ta initialize korte. Example: amra Employee Class a calculateSalary Pure function create korbo then  HR, SWE or other sector er class a calculateSalary function ke force korbo new kore calculateSalary function call korte or initialize korte.

image

// Pure virtual function
class Vehicle {
    public :
        string color;

    // Pure virtual function 
    virtual void print() = 0; // pure function create an Abstract class
};

Friend Functions & Classes

#include <iostream>
using namespace std;

class Bus {
    public :
        void print();
};

void test();

class Truck {
    private :
        int x;

    protected :
        int y;

    public :
        int z;

        friend class Bus; // friend class and and the class should be call before this class

    friend void Bus :: print(); // friend function inside a Class and this Class+Function should be call before this class

    friend void test(); // direct friend Function and the function should be call before this class

};

void Bus :: print() {
    Truck t;
    t.x = 10;
    t.y = 20;
    cout << t.x << " " << t.y << endl;
}

void test() {
    // Access truck private
    Truck t;
    t.x = 10;
    t.y = 20;
    cout << t.x << " " << t.y << endl;
}

int main() {
    Bus b;
    b.print();

    test();

}

Lecture 22 : OOPS 3  END HERE