Home > Mobile >  Converting UML to code c . Problem with inheritence. Do constructors of all classes run when object
Converting UML to code c . Problem with inheritence. Do constructors of all classes run when object

Time:10-27

I have this UML diagram

UML code -> C   code

Ad this is the corresponding C code

//Parent class Flight

class Flight
{
    private:
        int callNumber;
        Airplane plane;
        vector<Passenger> passengers;
    
    public:

    //Constructor

        Flight();

    //Functions

        int getCallNum();
        void setCallNum();
        Airplane getPlane();

//What parameters are taken in these functions. 

//I know they are of type Airplane and passenger but are they vectors?

        void setPlane(Airplane);            
        void addPassenger(Passenger);
        void removePassenger(Passenger);    
};

//Airplane class, child of Flight

class Airplane : public Flight
{
    private:
        int firstClassSeats;
        int economySeats;

    public:

    //Constructor

        Airplane();

    //Functions;

        int getFirstClassSeats();
        int getEconomySeats();
        void setFirstClassSeats();
        void setEconomySeats();
};

//Passenger class, child of FLight

class Passenger : public Flight
{
    private:
        string name;
        int age;
        string address;

    public:

    //Constructor

        Passenger();

    //Functions

        string getName();
        int getAge();
        string getAddress();
        void setName(string);
        void setAge(int);
        void setAddress(string);    
};

I wonder:

  1. do constructors of all classes run when an object of either parent or base class is created?
  2. Can base class access functions or data of child classes?
  3. I do not know how set plane function in parent class would look like. Would it take an object of type Airplane as an argument? Similarly, will addpassenger function in parent class take a vector of type Passenger as an argument?

CodePudding user response:

In short

If A inherits B (or A specializes B), then you should be able to say A is a (kind of) B. When in doubt, prefer object composition over inheritance.

More details

  1. The parameters taken by the member functions, are the parameters that you indicate for the operations in the diagram. No parameter in the diagram leads to no parameters in the code.

  2. The inheritance here is ambigous. There is no inheritance in your diagram. There is some in your code, but it does not make so much sense: is a passenger really a flight? E.g. can a passenger fly, have a crew, etc.?

    If the inheritance would be suitable, as a general rule in C : the constructor of an object is always called when the object is created. In case of inheritance, all the constructors of the class hierarchy are invoked, starting with the base constructor, until the most derived constructor (the rules can be more tricky, for example in case of multiple inheritance). In UML, the rules on constructors are not fully specified as far as I know.

  3. By default, a class can only access public members of another class. If a class is derived from a base class (in UML: if a class is a specialisation of a more general class), the derived class has only access to the public and protected members of the base class. Try to avoid protected, since it's a frequent cause of nasty bugs.

  4. WHen implementing in C an UML class diagram, there is a tricky issue about the types of the properties and arguments, because C has a value semantic: if you pass an Airplane as argument, the original airplane object is copied. Same if you have an Airplane property. However, in UML, properties and associations have a reference semantic (except for datatypes), meaning that the airplane argument would still refer to the same original airplane. So in your specific case, you'd probably want to pass a reference or a (smart) pointer to an Airplane.

  • Related