473,408 Members | 1,676 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,408 software developers and data experts.

making a virtual object in c#

4
I'm working on very simple lessons to learn c #.
Can anyone give an example of how to make a virtual object such as these


Thank You.

Expand|Select|Wrap|Line Numbers
  1. namespace critter_menu
  2. {
  3.     class Program
  4.     {
  5.         static void Main(string[] args)
  6.         {
  7.             bool keepgoing = true;
  8.             int choice;
  9.             critter mycritter=new critter();
  10.             mycritter.name = "George";
  11.  
  12.             while (keepgoing)
  13.             {
  14.                 mycritter.age();
  15.                 choice = showmenu();
  16.                 switch (choice)
  17.                 {
  18.                     case 0:
  19.                         keepgoing = false;
  20.                         break;
  21.                     case 1:
  22.                         mycritter.talk();
  23.                         break;
  24.                     case 2:
  25.                         mycritter.eat();
  26.                         Console.WriteLine("You have fed the critter");
  27.                         break;
  28.                     case 3:
  29.                         mycritter.play();
  30.                         Console.WriteLine("You have played with the critter");
  31.                         break;
  32.                     case 4:
  33.                         Console.WriteLine("Current name: {0}",mycritter.name);
  34.                         Console.WriteLine("Change name to: ");
  35.                         mycritter.name = Console.ReadLine();
  36.                         break;
  37.                     default:
  38.                         Console.WriteLine("That was not a valid input");
  39.                         break;
  40.                 }
  41.             }
  42.  
  43.         }
  44.         static int showmenu()
  45.         {
  46.             int input = 1;
  47.             Console.WriteLine();
  48.             Console.WriteLine();
  49.             Console.WriteLine();
  50.             Console.WriteLine("0) Exit");
  51.             Console.WriteLine("1) Listen to Critter");
  52.             Console.WriteLine("2) Feed Critter");
  53.             Console.WriteLine("3) Play with Critter");
  54.             Console.WriteLine("4) Rename Critter");
  55.             try
  56.             {
  57.                 input = Convert.ToInt32(Console.ReadLine());
  58.             }
  59.             catch (FormatException)
  60.             {
  61.                 Console.WriteLine("Incorrect Input");
  62.                 input = 1;
  63.             }
  64.             return input;
  65.         }
  66.     }
  67.     class critter
  68.     {
  69.         int phappy = 10;
  70.         int pfull = 10;
  71.         int page = 0;
  72.         string pName;
  73.         public string name
  74.         {
  75.             get
  76.             {
  77.                 return pName;
  78.             }
  79.             set
  80.             {
  81.                 if (value.Length > 8)
  82.                 {
  83.                     value = value.Substring(0, 8);
  84.                     pName = value;
  85.                     Console.WriteLine("The name can't be longer than 8 chacters");
  86.                     Console.WriteLine("The critter name has been set to:{0}", pName);
  87.                 }
  88.                 else
  89.                     pName = value;
  90.             }
  91.         }
  92.         public void talk()
  93.         {
  94.             Console.WriteLine("The critter says \"My name is {0}\"", pName);
  95.             {
  96.                 if (pfull < 1)
  97.                     Console.WriteLine("Feed me or i'll die");
  98.                 else if (pfull < 4)
  99.                     Console.WriteLine("I'm kinda getting hungry");
  100.                 else
  101.                     Console.WriteLine("I'm not hungry right now");
  102.             }
  103.             if (phappy < 5)
  104.                 Console.WriteLine("Play with me.");
  105.  
  106.         }
  107.         public void age()
  108.         {
  109.             page++;
  110.             pfull--;
  111.             phappy--;
  112.             //if hungry gets un happy faster
  113.             if (pfull < 3)
  114.                 phappy--;
  115.         }
  116.         public void eat()
  117.         {
  118.             pfull += 4;
  119.         }
  120.         public void play()
  121.         {
  122.             phappy += 3;
  123.         }
  124.     }
  125. }
Jul 26 '07 #1
1 4031
RoninZA
78
myburt - the problem with these 'Learn C# in 21 Days' kinda tutorials actually irritate me slightly. They maybe teach you the language, but you are never taught WHY you would want to use certain functions, so I'll explain why you would want to use a virtual operator.

With inheritance, the virtual operator allows you to define a method for which code can be defined in the base class, but this method can then also be overridden in the inheriting class, as follows:
Expand|Select|Wrap|Line Numbers
  1. class Person
  2. {
  3.     public string Name = string.Empty;
  4.     public bool IsFemale = false;
  5.  
  6.     public virtual string PersonGender()
  7.     {
  8.         if (IsFemale)
  9.             return "This person is female.";
  10.         else
  11.             return "This person is male.";
  12.     }
  13. }
  14.  
  15. class Female : Person
  16. {
  17.     public Female()
  18.     {
  19.          this.IsFemale = true;
  20.     }
  21.  
  22.     public override string PersonGender()
  23.     {
  24.         return "This person is female.";
  25.     }
  26. }
  27.  
  28. class Male : Person
  29. {
  30.     public Female()
  31.     {
  32.          this.IsFemale = false;
  33.     }
  34. }
  35.  
If you understand what I'm trying to illustrate above is that, a virtual method CAN be overridden, but doesn't HAVE to be overridden. If you compile the code above, you'll see all three classes have the PersonGender() method, but depending on which class is called, methods are called from different locations.

virtual is different to abstract in that an abstract method can not contain code in the base class, and MUST be overridden in the inheriting classes. Also, abstract methods must be contained in an abstract class. But that's a lesson for another day... I hope that this has shed some light on the matter for you :)
Jul 26 '07 #2

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

Similar topics

0
by: Simon Elliott | last post by:
I have a class which uses a singleton to output progress messages for logging or display. It's typically used deep within a hierarchy of aggregations, inheritances and containers. The singleton...
23
by: heted7 | last post by:
Hi, Most of the books on C++ say something like this: "A virtual destructor should be defined if the class contains at least one virtual member function." My question is: why is it only for...
10
by: Jeff Grills | last post by:
I am an experienced C++ programmer with over 12 years of development, and I think I know C++ quite well. I'm changing jobs at the moment, and I have about a month between leaving my last job and...
12
by: sojin | last post by:
Hi all, I'm a new to c++... and I've some doubts on "virtual" topics. 1) Can abstract base class have V-table? Here's the way,, Class CTemp{
2
by: 4zumanga | last post by:
In my program I have a number of classes which implement the same functionality, I use them in templated functions and classes. For example: struct A { int foo() { return 1; } }; struct B {...
8
by: daniel | last post by:
I wonder if there is any reasonable reason to make virtual destructor protected within a derived class purposely?
12
by: mijobee | last post by:
I'm very new to c++ and just writing some code to learn. I've run into a problem, with a javaish design, and want to know if there is any possible solution without modifying the design. I've read...
10
by: JurgenvonOerthel | last post by:
Consider the classes Base, Derived1 and Derived2. Both Derived1 and Derived2 derive publicly from Base. Given a 'const Base &input' I want to initialize a 'const Derived1 &output'. If the...
7
by: desktop | last post by:
This page: http://www.eptacom.net/pubblicazioni/pub_eng/mdisp.html start with the line: "Virtual functions allow polymorphism on a single argument". What does that exactly mean? I guess it...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.