public void SetAttributes(string n, int h, int w, Constants.HairColor hc, decimal ss, int cs) // cannot set eyecolor { name = n; // public so we can access it here SetHeight(h); // private must use human function to set it weight = w; // private so we can access it here HairColor = hc; // property so this ends up calling the set function shoeSize = ss; coatSize = cs; }
public void GetAttributes(out string n, out int h, out int w, out Constants.HairColor hc, out Constants.EyeColor ec, out decimal ss, out int cs) { n = name; // public so can access directly h = GetHeight(); // private so need access function w = weight; // protected so can access directly hc = HairColor; // property ends up calling the get function ec = EyeColor; // property ends up calling the get function ss = shoeSize; cs = coatSize; }
// constructor with default values public Human(string n = "noname", int h = 68, int w = 140, Constants.HairColor hc = Constants.HairColor.Brunette, Constants.EyeColor ec = Constants.EyeColor.Brown) { name = n; height = h; weight = w; HairColor = hc; // uses the property set function to validate data // validate eye color is valid if (ec == Constants.EyeColor.Blue || ec == Constants.EyeColor.Brown || ec == Constants.EyeColor.Green) { EyeColor = ec; } else { throw new Exception("Invalid eye color"); } }
// the constructor calls the base class constructor then sets the object specific fields public Male(string n, int h, int w, Constants.HairColor hc, Constants.EyeColor ec, decimal ss = 8, int cs = 42) : base(n, h, w, hc, ec) { // make sure shoe size if either a whole number or includes only .5 for half sizes decimal d = ss - Math.Truncate(ss); if (d == 0 || d == .5M) // validate shoe size is even or half size { shoeSize = ss; } else { throw new Exception("Invalid shoe size"); } coatSize = cs; }
public void SetAttributes(string n, int h, int w, Constants.HairColor hc, decimal ss, int dscs, int nk, bool m) { // call gender specific set function, human does not have get and set attribute functions so the gender // specific versions must be called unlike the WhatAmI and Print functions Female f = gender as Female; if (f != null) { f.SetAttributes(n, h, w, hc, ss, dscs); } else { Male ma = gender as Male; ma.SetAttributes(n, h, w, hc, ss, dscs); } // set person specific fields numKids = nk; married = m; }
public void GetAttributes(out string n, out int h, out int w, out Constants.HairColor hc, out Constants.EyeColor ec, out decimal ss, out int dscs, out int nk, out bool m) { // call gender specific get function, human does not have get and set attribute functions so the gender // specific versions must be called unlike the WhatAmI and Print functions which are virtual functions in human Female f = gender as Female; if (f != null) { f.GetAttributes(out n, out h, out w, out hc, out ec, out ss, out dscs); } else { Male ma = gender as Male; ma.GetAttributes(out n, out h, out w, out hc, out ec, out ss, out dscs); } // get person specific fields nk = numKids; m = married; }
private object gender; // will be female or male set at instantiation public Person(Constants.Gender g, string n, int h, int w, Constants.HairColor hc, Constants.EyeColor ec, decimal ss, int dscs, bool m = false, int nk = 0) { // create a gender based object switch (g) { case Constants.Gender.Female: gender = new Female(n, h, w, hc, ec, ss, dscs); break; case Constants.Gender.Male: gender = new Male(n, h, w, hc, ec, ss, dscs); break; default: throw new Exception("invalid gender"); } // set person specific fields married = m; numKids = nk; // get a new id idNum = nextIdNum++; }