Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Classes-and-Structures.cs
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
//Rextester.Program.Main is the entry point for your code. Don't change it. //Microsoft (R) Visual C# Compiler version 2.9.0.63208 (958f2354) // Homework Assignment #3 /* Question 1 : What is the difference between a class and a structure? Answer 1 : Differences between class and structure are as follows: 1. Classes are of reference types while structures are of value types. 2. A Class can inherit from another class whereas a structure cannot. 3. A Class supports 'polymorphism' where as structure does not. 4. Data members in classes are by default private whereas data members in structures are by default public. 5. Class has limitless features whereas structures have limited features. 6. Class is generally used in large program whereas struct is generally used in small programs. */ /* Question 2 : When would you want to use one versus the other? Answer 2: When a user wants more freedom in terms of what he wants to do, class is the best option as it has limitless features whereas if the user wants a quicker and easier program without having to worry about polymorphism or inheritance structure is the best option. */ using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Monster { public string name_; public int size_; public int scare_; public int nMonsters_; //default consturctor -initiated all values public Monster() { name_="Test"; size_=20; scare_=10; nMonsters_++; //increment number of monsters } //Constructors public Monster( string name,int size,int scare) { name_=name; size_=size; scare_=scare; nMonsters_++; } // Methods public void print() { Console.WriteLine("Monster Name :" + name_); Console.WriteLine ("Monster Scare:" + scare_); Console.WriteLine("Monster Size:" + size_); } } public class Program { public static void Main(string[] args) { Monster Dinosaur = new Monster(); // Public Members Console.WriteLine(Dinosaur.name_); Monster Dragon = new Monster("Drako",30,24); Monster TRex = new Monster("Rex",45,41); Monster WereWolf = new Monster("Wolf",34,21); Console.WriteLine(Dragon.name_); Console.WriteLine(TRex.name_); Console.WriteLine(TRex.nMonsters_); WereWolf.print(); TRex.print(); Dragon.print(); } } }
Show compiler warnings
[
+
]
Show input
Compilation time: 0,22 sec, absolute running time: 0,09 sec, cpu time: 0,08 sec, average memory usage: 18 Mb, average nr of threads: 3, absolute service time: 0,34 sec
edit mode
|
history
|
discussion
Test Drako Rex 1