+ 1

Classes c#

Creating classes : Create a class called ship Properties incl. name - a string that holds ship name Year - a int that holds the year that the ship was born Create a constructor to initialize both instance variables, all data in the class must be encapsulated. Create a overload constructor that receives 2 arguments ,the values of which are assigned to the instance variable Based on the above is this correct - Please advise if anything needs fixing. Class Ship { //constructor that is encapsulated Private ShipInitialize{ Private string name {get;set;} Private int year {get;set;} } //overload constructor Private ShipInitialize(string year, int year){ } console.Write("Enter the Ship year: "); year = Convert.ToInt32(Console.Readline()); return year; console.Write("Enter the Ship name: "); name = Console.Readline(); return name; static void Main (string []args){ } ShipInitialize(); // not sure about parameters when calling }

12th Jun 2019, 2:50 PM
Tracy Mulder
Tracy Mulder - avatar
1 Answer
+ 2
Create a class called ship Check! Properties incl. name - a string that holds ship name Year - a int that holds the year that the ship was born A property must be public to usefull as interface from one software module to another. (There are advance situation where private properties can be used but that is not the case in this case). Encapsulation is not making everything you see private. The problem of making everything private is that you get a house without doors. It is a beautifull house but you can get in so you need a gate/door. private string name; //this is the encapsulated variable public string Name //this is the door to the encapsulated value { get { return name;} set { name = Value;} } https://code.sololearn.com/cpyQEn6h7goY A constructor are a special method that is run when the object is create. The constructor must have the same name as the class Class Ship { public void Ship() { //this is a constuctor } } (Please add C# to your tags)
13th Jun 2019, 8:29 PM
sneeze
sneeze - avatar