Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
if we have a program like this
Public class chair{
Private No_Of_legs as int;
private Back_rest as string;
private Base as string;}
Public class sofa() : Chair
{ private hand_rest as string;
forsitting();
}Public static void main(string[] args)
{
chair ch=new sofa();
ch forsitting();
}
wat is the result of this. its not working.safinaz

if we have a program like this
Public class chair{
Private No_Of_legs as int;
private Back_rest as string;
private Base as string;}
Public class sofa() : Chair
{ private hand_rest as string;
forsitting();
}Public static void main(string[] args)
{
chair ch=new sofa();
ch forsitting();
}
wat is the result of this. its not working.Your code cannot work because of the class instantiated. You instantiated the chair class instead of the sofa class, which inherits from the chair class. The chair class does not have a method forsitting(). Ensure you instantiate the sofa class and make sure there is an implementation of the method, not just a definition. You will have to inherit the abstract class (sofa) and implement the defined methods.
Try this:Public static void main(string[] args)
{
sofa sf=new sofa();
sf.forsitting();//note that forsitting() method doesn't have an implementation, hence you have to inherit the class and implement it.
}
Contact me through my e-mail for more details.OGOLLA ONYANGO FREDRICK
INTEREST RATES ADVISORY CENTRE - IRAC
CREDIT ANALYST/SYSTEM DEVELOPER

First things first: this is not C#. Nor is it VB. It's some freakish mix of the two. It won't compile in a VB or c# compiler. I've seen a script engine that might be able to handle it...but if you're using the one i saw, you would not have issues with this simple an example.
Properly translated:
<code>
public class chair
{
private int No_Of_legs;
private string Back_rest;
private string Base;
}public class sofa : chair
{
private string hand_rest;
void forsitting();
}public static void Main(string[] args)
{
chair ch = new sofa();
ch.forsitting();
}// end
</code>Now, this still isn't going to compile. Let's ignore the warnings for a second (cause i know there's at least 4), and deal with the errors:
(1) chair.forsitting() is not defined. Since you're dealing with a chair (which may or may not be a sofa, but .net won't check on its own), any methods you use have to be defined for the chair class. the forsitting() function should be moved to the chair class, if you intend for chairs to have a forsitting() function as well (which i'm going to infer from the names of your classes). And if that's not the intention, you'll have to either cast ch to a sofa, as in
(sofa) ch.forsitting();
or declare ch as a sofa to start with.
(2) sofa.forsitting() is not defined. Sure, there's a "void forsitting();" in the class definition, but there's no body. As it stands, this class would have to be marked as "abstract", at which point you couldn't create a "new sofa()". The simplest, if most boring, fix is to insert an empty method body { } in place of the semicolon.
(3) your Main function isn't in a class. Unless C# is different than i remember it, functions have to be in classes.
(4) There's no access level set on forsitting(). In C#, if no access level is set for stuff inside a class, it defaults to "private". You'll need to at least make it "internal" in order to use it as you intend to, but "public" works too.
With those errors taken care of, we have:public class chair
{
private int No_Of_legs;
private string Back_rest;
private string Base;
internal void forsitting() { }
}public class sofa : chair
{
private string hand_rest;
}public class playing_with_furniture
{
public static void Main(string[] args)
{
chair ch = new sofa();
ch.forsitting();
}
}// end
Now, this will probably compile. (I'm not even bothering to start up VS, cause...heh.) Course, even though it might compile and even run, it does nothing because forsitting() is about the most boring function there is.
Let's stop for a second, though, and take a look at what we've done. Semantically, that is. Forget that this was a C# question; there's a bigger picture here that could use looking at.
We've defined a class "chair", with a "forsitting()" method. I've made it void, because you gave no hint about the return value, but the name semi suggests you intended it to say whether the chair (or sofa) is "for sitting", meaning whether it can be sat on. That'd mean changing the function so that it returns a boolean (representing whether we can sit on it). Unless you don't intend for people to sit on some chairs, though, that function is useless -- if we know we have at least a chair, we already know it can be sat on. And if we don't know that this is a chair (or a subclass of chair), we can't even call the forsitting() function. (Extrapolate from error #1 above. Assume we make a "furniture" class. Unless there's a forsitting() function in the furniture class, we can't call fu.forsitting().)
We've also made "sofa" a subclass of "chair". In other words, we're saying that a sofa is a kind of chair. While the two do have a couple of similarities, are they really so similar that you can say a sofa is a chair? Probably not -- they're both different kinds of furniture, or seats if we want to go there.
Restructuring a bit...
public class seat
{
// Yes, this is useless. For now.
internal bool forsitting() { return true; }
}public class chair : seat
{
private int No_Of_legs;
private string Back_rest;
private string Base;
}public class sofa : seat
{
private int No_Of_legs;
private string Back_rest;
private string Base;
private string hand_rest;
}public class playing_with_furniture
{
public static void Main(string[] args)
{
seat s = new sofa();
s.forsitting();
}
}// end
Now this is more in the spirit of what inheritance was meant for. One bad thing is that we've duplicated the chair's variables in the sofa class, but if you intend for multiple classes to use those same variables, they can be moved into the seat class.
Since we're talking about inheritance, though, i need to point out something. All those private variables...as of yet, no one will ever see them! "private" means "only accessible by functions inside the class". And since there aren't any functions using them at the moment, you'll get those compiler warnings i mentioned earlier. If you intend for the subclasses to use them, you'll need to either pick some other access level for them ("protected", "internal", and "protected internal" would all be acceptable in this case), or define properties that retrieve and possibly set the values of those variables (which is probably the better idea, considering you probably want the world to know how many legs your chair has).

![]() |
Regarding perl reg exp
|
Search with batch file
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |