Given below is an UML diagram of phones; implement this UML diagram using the de
ID: 3631332 • Letter: G
Question
Given below is an UML diagram of phones; implement this UML diagram using the definition of Inheritance, also use the constructors.
In Generalization and Specialization we define the parent-child relationship between the classes. In many instance you will see some of the classes have same properties and operation these classes are called super class and later you can inherit from super class and make sub classes which have their own custom properties. In the below figure there are three classes to show Generalization and Specialization relationship. All phone types have phone number as a generalized property but depending upon landline or mobile you can have wired or simcard connectivity as specialized property. In this diagram the clsphone represent Generalization whereas clslandline and clsmobile represents specialization
Note: generalization and specialization are same thing Base and Derived classes.
Explanation / Answer
#include <iostream>
using namespace std;
//base class
class Phone
{
protected:
int number; //phone number
public:
Phone(){} //default constructor
//argunmented constructor
Phone(int number)
{
setNumber(number);
}
//getter method
int getNumber()
{
return number;
}
//setter method
void setNumber(int num)
{
number = num;
}
};
//child class of Phone
class Landline : public Phone
{
private:
int areaCode; //area code
public:
//argumented constructor
Landline(int areaCode, int number):Phone(number)//calling base class constructor
{
setAreaCode(areaCode);
}
//getter method
int getAreaCode()
{
return areaCode;
}
//setter method
void setAreaCode(int code)
{
areaCode = code;
}
//displasy the data of object
void display()
{
cout<<"Number: "<<getNumber()<<endl;
cout<<"Area Code: "<<areaCode<<endl;
}
};
//another child class of Phone
class Mobile: public Phone
{
private:
int simID; //sim identity number
public:
//argumented constructor
Mobile(int number, int simID):Phone(number)//calling base class constructor
{
setSimID(simID);
}
//getter method
int getSimID()
{
return simID;
}
//setter method
void setSimID(int id)
{
simID = id;
}
//displasy the data of object
void display()
{
cout<<"Number: "<<getNumber()<<endl;
cout<<"Sim ID: "<<simID<<endl;
}
};
//test function
int _tmain(int argc, _TCHAR* argv[])
{
//lanline object
Landline landline(91,245768);
//mobile object
Mobile mobile(1234,65478124);
//displaying data
landline.display();
mobile.display();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.