473,387 Members | 1,540 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Base Class and Inheritance

Hi there all,

Im curently working on my course work and got stuck on of the questions.

I have three classes : car, minibus and truck.
I want the car to be my base and minibus, truck to inherit the property of the car class.

I keep getting an error message :

'xxxx.Car' does not contain the constructor that takes 0 arguments. This is for both minibus and truck.

Here is my car class :
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Windows.Forms;
  11.  
  12. namespace SD2coursework
  13. {
  14.     /*
  15.      * Used as a GUI to create or edit instances of Car.
  16.      * The Car being created or set is stored in the 'car' propety
  17.      * 
  18.      * To edit a car, instanciate this class, set the car property to the car object
  19.      * being edited, then use the ShowDialog() method to display the form.
  20.      * 
  21.      * After calling showDialog() the newly created/modified car object can be accesed 
  22.      * via the car property
  23.      * 
  24.      */
  25.  
  26.     public partial class FrmCar : Form
  27.     {
  28.        private Car theCar = null; //Stores the value of the car property
  29.  
  30.        public Car car  //The car property
  31.        {
  32.            get
  33.            {
  34.                return theCar;
  35.            }
  36.            set
  37.            {   /*
  38.                 * Used when editing an existing car
  39.                 */ 
  40.                theCar = value;
  41.                txtColour.Text = theCar.colour;
  42.                txtMake.Text = theCar.make;
  43.                txtRegNumber.Text = theCar.regnumber;
  44.                txtModel.Text = theCar.model; 
  45.            }
  46.        }
  47.  
  48.         public FrmCar()
  49.         {
  50.             InitializeComponent();
  51.         }
  52.  
  53.         private void btnClose_Click(object sender, EventArgs e)
  54.         {
  55.             //Close the form. Causes FrmCar_FormClosed() to be called.
  56.             this.Close();
  57.         }
  58.  
  59.         private void FrmCar_FormClosed(object sender, FormClosedEventArgs e)
  60.         {
  61.             /*
  62.             * When the form is closed, update the car property
  63.             * If the car property was not set previously (ie this is a new car) then 
  64.             * create a new car object
  65.              */
  66.             if (theCar == null) //If new car...
  67.                 theCar = new Car(txtMake.Text, txtColour.Text, txtRegNumber.Text, txtModel.Text);
  68.             else
  69.             {   //if editing an existing car
  70.                 theCar.colour = txtColour.Text;
  71.                 theCar.make = txtMake.Text;
  72.                 theCar.regnumber = txtRegNumber.Text;
  73.                 theCar.model = txtModel.Text; 
  74.             }
  75.         }
  76.  
  77.         private void FrmCar_Load(object sender, EventArgs e)
  78.         {
  79.  
  80.         }
  81.  
  82.         private void txtMake_TextChanged(object sender, EventArgs e)
  83.         {
  84.  
  85.         }
  86.     }
  87. }
  88.  
Minibus class :

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7.  
  8. namespace SD2coursework
  9. {
  10.        public class MiniBus : Car 
  11.         /*This class represents a MiniBus. The MiniBus has three properties, make, colour and seat capacity.
  12.         *Note that the constructor requires values for make, colour and seat capacity */  
  13.       {
  14.  
  15.         private string theMake;   //Holds the value of the make property
  16.         private string theColour; //Holds the value of the colour property
  17.         private string theSeatCap;   //Holds the value of the seat capacity property
  18.  
  19.             //Default constructor 
  20.         public MiniBus(string aMake, string aColour, string aSeatCap)
  21.         {
  22.             theMake = aMake;
  23.             theColour = aColour;
  24.             theSeatCap = aSeatCap;
  25.  
  26.         }
  27.  
  28.         //Make property
  29.         public string make2
  30.         {
  31.             get
  32.             {
  33.                 return theMake;
  34.             }
  35.             set
  36.             {
  37.                 theMake = value;
  38.             }
  39.         }
  40.         //Colour property
  41.         public string colour2
  42.         {
  43.             get
  44.             {
  45.                 return theColour;
  46.             }
  47.             set
  48.             {
  49.                 theColour = value;
  50.             }
  51.         }
  52.         //Seat capacity property 
  53.         public string seatcap
  54.         {
  55.             get
  56.             {
  57.                 return theSeatCap;
  58.             }
  59.             set
  60.             {
  61.                 theSeatCap = value;
  62.             }
  63.         }
  64.  
  65.     }
  66. }
  67.  
Truck class :

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7.  
  8. namespace SD2coursework
  9. {
  10.     public class Truck : Car
  11.       /*This class represents a Truck. The truck has three properties, make, colour and gross weight.
  12.      *Note that the constructor requires values for make, colour and gross weight */
  13.     {
  14.  
  15.         private string theMake;   //Holds the value of the make property
  16.         private string theColour; //Holds the value of the colour property
  17.         private string theGrossW; //Holds the value of the gross weight property
  18.  
  19.         //Default constructor
  20.         public Truck(string aMake, string aColour, string aGrossW)
  21.         {
  22.             theMake = aMake;
  23.             theColour = aColour;
  24.             theGrossW = aGrossW;
  25.         }
  26.  
  27.         //Make property
  28.         public string make3
  29.         {
  30.             get
  31.             {
  32.                 return theMake;
  33.             }
  34.             set
  35.             {
  36.                 theMake = value;
  37.             }
  38.         }
  39.         //Colour property
  40.         public string colour3
  41.         {
  42.             get
  43.             {
  44.                 return theColour;
  45.             }
  46.             set
  47.             {
  48.                 theColour = value;
  49.             }
  50.         }
  51.         //Gross weight property 
  52.         public string grossw
  53.         {
  54.             get
  55.             {
  56.                 return theGrossW;
  57.             }
  58.             set
  59.             {
  60.                 theGrossW = value;
  61.             }
  62.         }
  63.  
  64.     }
  65.  
I was wondering if Im doing the Inheritance right and what's this error message.

Thanks,
Mike
Nov 17 '10 #1
11 2116
Leito
58
Your car class is called FrmCar :
Expand|Select|Wrap|Line Numbers
  1. public partial class FrmCar : Form
So why do you call it by the name "Car" ?
Expand|Select|Wrap|Line Numbers
  1. public class MiniBus : Car
Nov 18 '10 #2
FrmCar is my form in which i create/add new cars.
Car.cs is the car class with properites ect.

Am I doing it the wrong way?
Thanks for you reply,

Mike

p.s Im using VS if that makes a difference.
Nov 18 '10 #3
Hi again,

I posted in the wrong code. It was not car.cs you were right it was FrmCar : )

Here is my code for the car class :

Expand|Select|Wrap|Line Numbers
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. namespace SD2coursework
  8. {
  9.      public class Car 
  10.      /*This class represents a Car. The car has four properties, make, colour, registration number and model.
  11.       *Note that the constructor requires values for make, colour, registration number and model */
  12.  
  13.     {
  14.         private string theMake;   //Holds the value of the make property
  15.         private string theColour; //Holds the value of the colour property
  16.         private string theRegNumber; //Holds the value of the registration number property
  17.         private string theModel;     //Holds the value of the model property   
  18.  
  19.             //Default constructor
  20.         public Car(string aMake, string aColour, string aRegNumber, string aModel)
  21.         {
  22.             theMake = aMake;
  23.             theColour = aColour;
  24.             theRegNumber = aRegNumber; 
  25.             theModel = aModel;
  26.         }
  27.  
  28.         //Make property
  29.         public string make
  30.         {
  31.             get
  32.             {
  33.                 return theMake;
  34.             }
  35.             set
  36.             {
  37.                 theMake = value;
  38.             }
  39.         }
  40.         //Colour property
  41.         public string colour
  42.         {
  43.             get
  44.             {
  45.                 return theColour;
  46.             }
  47.             set
  48.             {
  49.                 theColour = value;
  50.             }
  51.         }
  52.         //Registration number property 
  53.         public string regnumber 
  54.         { 
  55.             get 
  56.             { 
  57.                 return theRegNumber;
  58.             } 
  59.             set 
  60.             {  
  61.                 theRegNumber = value; 
  62.             } 
  63.         }
  64.         //Model property 
  65.         public string model
  66.         { 
  67.             get 
  68.             { 
  69.                 return theModel; 
  70.             } 
  71.             set 
  72.             {  
  73.                 theModel = value; 
  74.             } 
  75.         } 
  76.     } 
  77. }
  78.  
I keep getting the error regarding the constructor which I mentioned erlier.
Sorry for the confusion and hopefuly you will be able to help me now.

Thanks Mike
Nov 18 '10 #4
Hi Michael,

A few things are wrong, both code and setup.

Lets start with the setup. A minibus is not a type of car, its a type of bus. A truck is also not a car.

What you want is something along of:

Expand|Select|Wrap|Line Numbers
  1. public interface IVehicle
  2. {
  3.  Color bodycolor;
  4.  DateTime MakeYear;
  5.  //etc..
  6. }
  7.  
  8. public class Car : IVehicle
  9. {
  10.  {implement IVehicle members}
  11. }
Because a minibus, a car and a truck are a type of vehicle.

Now for the error:
If you inherit a class, and you define a constructor in your base class with arguments, you need to define that too in the child class like so:

Expand|Select|Wrap|Line Numbers
  1. public abstract Vehicle
  2. {
  3.  Color bodycolor;
  4.  DateTime MakeYear;
  5.  public Vehicle(Color color, DateTime creationDate)
  6.  {
  7.   bodycolor = color;
  8.   MakeYear = creationDate;
  9.  }
  10. }
  11.  
  12. public class Car : Vehicle
  13. {
  14.  int AmountDoors;
  15.  public Car(Color color, DateTime creationDate, int amountDoors) : base(Color color, DateTime creationDate)
  16.  {
  17.   AmountDoors = amountDoors;
  18.  }
  19. }
The base class needs its arguments and is given by the child class through ": base ()".

-Bryan
Nov 18 '10 #5
Firstly, thanks for your help Bryan.
Now, Do I need to create a new class called vehicle which is going to be my base class and car, truck, minibus to inherit the properties of vehicle. By the way, this makes more sense: )

Now all three classes have two properties in common : make and colour. Which basically means my vehicle will only have these two right?

The only thing I am not to sure about is, how do I make it work. Let's say I have vehicle class with make and colour. How do I make the car, truck and minibus to use it and at the same time create the right vehicles?
I know it sounds confusing but that's where I am now :)

Thanks,
Mike
Nov 18 '10 #6
Hey Michael,

Its actually easier to do these things if you think of making sense, then of how the technique works ;)

Your Vehicle could be a abstract class or a interface, depends on what responsibilty it should have(thinking process).

If you want to make it like a blueprint you choose Interface because when you implement a interface, each implementing class must have the members the interface has. But a abstract class could already have the member so you would not need to implement it in the child classes. Also remember interface members are always public!

With interface:
Expand|Select|Wrap|Line Numbers
  1. public interface IVehicle
  2. {
  3.     Color bodyColor;
  4.     DateTime makeDate;
  5. }
  6.  
  7. public class Car : IVehicle
  8. {
  9.     Color bodyColor;
  10.     DateTime makeDate;
  11.     public Car(Color bodycolor, DateTime makedate)
  12.     {
  13.         bodyColor = bodycolor;
  14.         makeDate = makedate;
  15.     }
  16. }
  17.  
Call:
Expand|Select|Wrap|Line Numbers
  1. Car yourcar = new Car(Color.Red, DateTime.Now);
With abstraction:
Expand|Select|Wrap|Line Numbers
  1. public abstract class Vehicle
  2. {
  3.     Color bodyColor;
  4.     DateTime makeDate;
  5.  
  6.     public Vehicle(Color bodyColor, DateTime makedate)
  7.     {
  8.         bodyColor = bodycolor;
  9.         makeDate = makedate;
  10.     }
  11. }
  12.  
  13. public class Car : Vehicle
  14. {
  15.     int amountDoors;
  16.     public Car(Color bodycolor, DateTime makedate, int amountDoors) 
  17.         : base (bodycolor, makedate)
  18.     {
  19.         amountDoors = amountDoors;
  20.     }
  21. }
Call:
Expand|Select|Wrap|Line Numbers
  1. Car yourcar = new Car(Color.Red, DateTime.Now, 4);// no vehicle call, the codes gets it because of the inheritence
Notice that with abstract you dont repeat the member because its already in the abstract base class. You just have to pass it. Now the next example is also a possibility:

Expand|Select|Wrap|Line Numbers
  1. public abstract class Vehicle
  2. {
  3.     Color bodyColor;
  4.     DateTime makeDate;
  5.  
  6.     public Vehicle()
  7.     {
  8.     }
  9. }
  10.  
  11. public class Car : Vehicle
  12. {
  13.     int amountDoors;
  14.     public Car(Color bodycolor, DateTime makedate, int amountDoors)
  15.     {
  16.         amountDoors = amountDoors;
  17.         bodyColor = bodycolor;
  18.         makeDate = makedate;
  19.     }
  20. }
Because Car inherits the members of Vehicle, the field bodyColor and makeDate is also accesible(unless set private).

First think of the setup and the responsibility of each object before deciding you want to use abstract or interface :) depending on the responsibility you choose your technique.

-Bryan
Nov 18 '10 #7
Hi again everyone,

Last error :D

Finishing with FrmHire, please help!

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Windows.Forms;
  11.  
  12. namespace SD2coursework
  13. {
  14.     public partial class FrmHireCo : Form
  15.         /*
  16.          * This is the main form for the car hire system.
  17.          * It allows the adding to new cars to the system, and displaying them in the fleet list
  18.          * 
  19.          */
  20.     {
  21.         private Fleet myFleet = new Fleet();
  22.         //Fleet object used to store cars
  23.  
  24.         public FrmHireCo()
  25.         {
  26.             //Default constructor
  27.             InitializeComponent();
  28.         }
  29.  
  30.  
  31.         private void updateFleetList()
  32.         {   //Used to update the fleet displayed on the form
  33.             lstFleet.Items.Clear();
  34.             foreach (Car c in myFleet.fleet)
  35.             {
  36.                 string line = "Car: " + c.make+" " + c.colour+" " + c.regnumber+" " + c.model;
  37.                 lstFleet.Items.Add(line);
  38.             }
  39.             lstFleet.Items.Clear();
  40.             foreach (MiniBus b in myFleet.fleet)
  41.             {
  42.                 string line = "MiniBus: " + b.make + " " + b.colour +" " + b.seatcap;
  43.                 lstFleet.Items.Add(line);
  44.             }
  45.             lstFleet.Items.Clear();
  46.             foreach (Truck t in myFleet.fleet)
  47.             {
  48.                 string line = "Truck: " + t.make + " " + t.colour + " " + t.grossw;
  49.                 lstFleet.Items.Add(line);
  50.             } 
  51.         }
  52.  
  53.         private void btnAddCar_Click(object sender, EventArgs e)
  54.         {
  55.             //Add a new car
  56.             FrmCar carGui = new FrmCar(); //Form used to add new car
  57.             carGui.ShowDialog();
  58.             Car myCar = carGui.car;         //Get new car from form
  59.             myFleet.addToFleet(myCar);      //Add to fleet list
  60.             updateFleetList();              //Uodate fleet list
  61.  
  62.         }
  63.  
  64.         private void lstFleet_SelectedIndexChanged(object sender, EventArgs e)
  65.         {
  66.             /*
  67.              * This method is used to control the list box
  68.              * It is called when a row is selected by the user, it then displays frmCar
  69.              * with the car details
  70.              */ 
  71.             if (lstFleet.SelectedIndex > -1)
  72.             {
  73.                 int index = lstFleet.SelectedIndex;
  74.                 Car myCar = myFleet.fleet.ElementAt(index);
  75.                 FrmCar carGui = new FrmCar();
  76.                 carGui.car = myCar;
  77.                 carGui.Show();
  78.             }
  79.         }
  80.  
  81.         private void btnAddTruck_Click(object sender, EventArgs e)
  82.         {
  83.             //Add a new truck
  84.             FrmTruck truckGui = new FrmTruck(); //Form used to add new car
  85.             truckGui.ShowDialog();
  86.             Truck myTruck = truckGui.truck;         //Get new car from form
  87.             myFleet.addToFleet(myTruck);      //Add to fleet list
  88.             updateFleetList();              //Uodate fleet list
  89.  
  90.         }
  91.  
  92.         private void lstFleet_SelectedIndexChanged(object sender, EventArgs e)
  93.         {
  94.             /*
  95.              * This method is used to control the list box
  96.              * It is called when a row is selected by the user, it then displays frmCar
  97.              * with the car details
  98.              */
  99.             if (lstFleet.SelectedIndex > -1)
  100.             {
  101.                 int index = lstFleet.SelectedIndex;
  102.                 Truck myTruck = myFleet.fleet.ElementAt(index);
  103.                 FrmTruck truckGui = new FrmTruck();
  104.                 truckGui.truck = myTruck;
  105.                 truckGui.Show();
  106.             }
  107.         }
  108.  
  109.         private void btnAddMiniBus_Click(object sender, EventArgs e)
  110.         {
  111.             //Add a new minibus
  112.             FrmBus minibusGui = new FrmBus(); //Form used to add new car
  113.             minibusGui.ShowDialog();
  114.             MiniBus myMiniBus = minibusGui.minibus;         //Get new car from form
  115.             myFleet.addToFleet(myMiniBus);      //Add to fleet list
  116.             updateFleetList();              //Uodate fleet list
  117.  
  118.         }
  119.  
  120.         private void lstFleet_SelectedIndexChanged(object sender, EventArgs e)
  121.         {
  122.             /*
  123.              * This method is used to control the list box
  124.              * It is called when a row is selected by the user, it then displays frmCar
  125.              * with the car details
  126.              */
  127.             if (lstFleet.SelectedIndex > -1)
  128.             {
  129.                 int index = lstFleet.SelectedIndex;
  130.                 MiniBus myMiniBus = myFleet.fleet.ElementAt(index);
  131.                 FrmBus minibusGui = new FrmBus();
  132.                 minibusGui.minibus = myMiniBus;
  133.                 minibusGui.Show();
  134.             }
  135.         }
  136.  
  137.  
  138.  
  139.     }
  140. }
  141.  
The error message : Error 1 Type 'SD2coursework.FrmHireCo' already defines a member called 'lstFleet_SelectedIndexChanged' with the same parameter types xxxxxx3\assessment\Assessment\WindowsFormsApplicat ion1\FrmHire.cs 90 22 SD2coursework
Nov 22 '10 #8
You should read the error messages more often :( (no disrespect meant).

What the error message says is: the type(class) SD2coursework.FrmHireCo has two members called the same(lstFleet_SelectedIndexChanged), your first step should be, check if 'lstFleet_SelectedIndexChanged' is defined twice. And it is(line 64 and line 92).

I see that in the first case its a Car and second case its a Truck. You should make a check on type and use maybe a switch?

-Bryan
Nov 22 '10 #9
Thanks Bryan will work on that.
Nov 22 '10 #10
Michael, a little help, here is where Vehicle can come in:
Expand|Select|Wrap|Line Numbers
  1. Vehicle myVehicle = myFleet.fleet.ElementAt(index);
  2.  
  3. switch(typeof(myVehicle).ToString().ToLower())
  4. {
  5.     case "car":
  6.         FrmCar carGui = new FrmCar();
  7.         carGui.car = myCar;
  8.         carGui.Show();
  9.     break;
  10.     case "truck":
  11.         FrmTruck truckGui = new FrmTruck();
  12.         truckGui.truck = myTruck;
  13.                truckGui.Show();
  14.     break;
  15. }
Nov 23 '10 #11
Thanks Bryan it works now : )
On to the next one,
Cheers,
Mike
Nov 23 '10 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Dave Theese | last post by:
Hello all, The example below demonstrates proper conformance to the C++ standard. However, I'm having a hard time getting my brain around which language rules make this proper... The error...
8
by: Bryan Parkoff | last post by:
I find an interesting issue that one base class has only one copy for each derived class. It looks like that one base class will be copied into three base classes while derived class from base...
2
by: dumboo | last post by:
hi there i m having a base class class base { ... }; and two derived class, which have inherited 'base' but the inheritance is not virtual
13
by: Walt Karas | last post by:
The following gives an error in the declaration of the member function x() of the class template Tpl, compiliing with a recent version of GCC under Solaris: class A { }; class B { }; ...
3
by: hazz | last post by:
The following classes follow from the base class ' A ' down to the derived class ' D ' at the bottom of the inheritance chain. I am calling the class at the bottom, "public class D" from a client...
1
by: Doug | last post by:
Can someone point me to a very simple example in VB.NET/ASP.NET of inheritance from a base class? I would like to define a BasePage class from which all of my pages can inherit. I would like to...
3
by: hurcan solter | last post by:
Consider the code fragment; class A { public: A(){} A(int prm):mprm(prm){} int mprm; }; class B:public A {
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
3
by: Ravi | last post by:
Is this the correct way to think of "base class"? The "base class" is a class from which other classes are derived. The "base class" will never be derived from another class.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.