The constructor of the child class "caesar" gives an error. It says that name, type is inaccessible due to its protection level. How come? As this is a child class derived from "Cipher" class it shouldn't give an error like this. How can I overcome this situation. But I want those variables to be private. I don't want to change them as public.
***The second code example works. Can anybody see a difference?
namespace Encrypter
{
class Cipher
{
public Cipher(string name, string type)
{
setName(name);
setType(type);
}
private string name;
private string type;
public void setName(string newName)
{
name = newName;
}
public string getName()
{
return name;
}
public void setType(string newType)
{
type = newType;
}
public string getType()
{
return type;
}
public string encrypt(string text)
{
return text;
}
public string decrypt(string text)
{
return text;
}
}
}
namespace Encrypter
{
class Caesar : Cipher
{
private int shiftamount;
private string shiftdirection;
public Caesar(int shiftamount, string shiftdirection) : base(name, type)
{
setShiftamount(shiftamount);
setShiftdirection(shiftdirection);
}
public void setShiftamount(int newShiftamount)
{
shiftamount = newShiftamount;
}
public int getShiftamount()
{
return shiftamount;
}
public void setShiftdirection(string newShiftdirection)
{
shiftdirection = newShiftdirection;
}
public string getShiftdirection()
{
return shiftdirection;
}
}
}
----------------------------- New Edit ----------------
class MyFile
{
public MyFile(int id, string name, int size, string type)
{
setId(id);
setName(name);
setSize(size);
setType(type);
}
private int id;
private string name;
private string type;
private int size;
class Movie : MyFile
{
private string director;
private int release_year;
public Movie(string director, int release_year, int id, string name, int size) : base( id, name, size, "m")
{
setDirector(director);
setRelease_year(release_year);
}
It looks like you have made a mistake in defining the derived class constructor. If you want to get name
and type
values to the superclass, you'll have to pass them in as additional constructor arguments (for a total of 4 arguments in the derived class constructor.) For example, changing it to this should work:
public Caesar(int shiftamount,
string shiftdirection,
string name,
string type)
: base(name, type)
There are a number of other strategies you could take.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With