-
Notifications
You must be signed in to change notification settings - Fork 19
/
Classes.cs
executable file
·96 lines (80 loc) · 2.75 KB
/
Classes.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//Rextester.Program.Main is the entry point for your code. Don't change it.
//Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Rextester
{
//what are classes?
//blueprints to creating an object
//they allow the user to create their own type as objects
public class Monster
{
//what can a class contain
//data members - constants/variables
//methods/functions
//public class - can be accessed anywhere on this project
//public data members/methods- can be accessed anywhere where the class
//was instantiated
public string name_;
public int size_;
public const int legs_ = 2;
//private data members/methods - can ONLY be accessed in the class
private int scare_;
//static data members/methods- apply to the entire class rather than
//an instance of it
public static int nMonsters_;
//default constructor -intiated all values
public Monster()
{
name_ = "default";
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("Monsters name: " + name_);
Console.WriteLine("Monsters size: " + size_);
Console.WriteLine("Monsters scare: " + scare_);
}
}
public class Program
{
public static void Main(string[] args)
{
Monster Dinosour = new Monster();
//public members
Console.WriteLine(Dinosour.name_);
//can be changed
Dinosour.name_ = "Rex";
Console.WriteLine(Dinosour.name_);
//change a const cannot be changed
// exmaple -- Dinosour.legs_ = 3;
//private members cannot be accessed here
// example -- Console.WriteLine(Dinosour.scare_);
Console.WriteLine(Monster.nMonsters_);
//using non-default-constructor
Monster Dragon = new Monster("Drako", 30, 25);
Console.WriteLine(Dragon.name_);
//static check
Console.WriteLine(Monster.nMonsters_);
Monster Zombie = new Monster("Zombie", 6, 8);
//call print
Zombie.print();
Dragon.print();
Dinosour.print();
Console.WriteLine(Monster.nMonsters_);
}
}
}