Dependency: It is a relationship between two classes, where one class is dependent on other class. It is represented by dashed arrowed line and arrow will point to dependent class.
Example:
- Car dependent on “air filling machine”.
Technically: One class will call API of another class from inside the function, object of another class will be created inside function or will be passed via parameter but another class object or pointer or reference will not be part(data member) of first class.
Class Diagram:
source code with example:
/* Program:Dependency Author: Alpha Master Date: 31 March 2021 */ //Header File #include<iostream> class AirFillingMachine { public: void GetAir(int air){std::cout<<"Filled the air:"<<air<<std::endl;} void RemoveAir(int air){std::cout<<"Removed the air:"<<air<<std::endl;} }; class Car { int airInTyre; public: Car(int air):airInTyre(air){std::cout<<"Car Constructor"<<std::endl;} ~Car(){std::cout<<"Car Destructor"<<std::endl;} void InitialCheck() { CheckAirPressure(); } void CheckAirPressure() { //Calling a another class API from inside function //Creating a object inside or getting via parameter //This is dependency relationship. AirFillingMachine obj; if(airInTyre < 33) { obj.GetAir(33-airInTyre); } else if (airInTyre > 33) { obj.RemoveAir(airInTyre - 33); } else {std::cout<<"Air is not required"<<std::endl;} } }; int main() { std::cout<<"Dependency"<<std::endl; Car carObj(30); carObj.InitialCheck(); return 0; }
Output:
Dependency Car Constructor Filled the air:3 Car Destructor